-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFql.php
More file actions
405 lines (347 loc) · 10.8 KB
/
Fql.php
File metadata and controls
405 lines (347 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php //-->
/*
* This file is part of the Eden package.
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
namespace Eden\Facebook;
use Eden\Facebook\Argument;
use Eden\Facebook\Auth;
use Eden\Facebook\Base;
use Eden\Facebook\Fql\Search;
use Eden\Facebook\Fql\Select;
use Eden\Collection\Base as Collection;
use Eden\Curl\Base as Curl;
use Eden\Model\Base as Model;
/**
* Abstractly defines a layout of available methods to
* connect to and query a database. This class also lays out
* query building methods that auto renders a valid query
* the specific database will understand without actually
* needing to know the query language.
*
* @vendor Eden
* @package Facebook
* @author Ian Mark Muninio <ianmuninio@openovate.com>
*/
class Fql extends Base
{
const INSTANCE = 0; // set to multiton
const SELECT = 'Select';
const FQL_URL = 'https://graph.facebook.com/fql';
const FIRST = 'first';
const LAST = 'last';
protected $queries = array();
protected $token = null;
/**
* Preloads the access token.
*
* @param string $token
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Gets the result of the query and parse it to collection/model.
*
* @param string $table
* @param string|array|null $filters [optional]
* @param array $sort
* @param int|float $start
* @param int|float $range
* @param int|float|null $index [optional]
* @return \Eden\Model\Base|\Eden\Collection\Base
*/
public function getCollection(
$table,
$filters = null,
array $sort = array(),
$start = 0,
$range = 0,
$index = null
) {
Argument::i()
->test(1, 'string')
->test(2, 'string', 'array', 'null')
->test(3, 'array')
->test(4, 'int', 'float')
->test(5, 'int', 'float')
->test(6, 'int', 'float', 'null');
$results = $this->getRows($table, $filters, $sort, $start, $range, $index);
$collection = Collection::i();
// checks if results is null
if (is_null($results)) {
return $collection; // return empty collection
}
// checks if index is not null
if (!is_null($index)) {
return $this->model($results); // return model with results
}
return $collection->set($results); // return collection with results
}
/**
* Gets a model from the result.
*
* @param string $table
* @param string $name
* @param string|int|float $value
* @return \Eden\Model\Base
*/
public function getModel($table, $name, $value)
{
Argument::i()
->test(1, 'string')
->test(2, 'string')
->test(3, 'string', 'int', 'float');
$result = $this->getRow($table, $name, $value);
$model = Model::i();
if (is_null($result)) {
return $model;
}
return $model->set($result);
}
/**
* Gets a single result of the query.
*
* @param string $table
* @param string $name
* @param string|int|float $value
* @return array
*/
public function getRow($table, $name, $value)
{
Argument::i()
->test(1, 'string')
->test(2, 'string')
->test(3, 'string', 'int', 'float');
// select query
$query = $this->select()
->from($table)
->where($name . ' = ' . $value)
->limit(0, 1);
$results = $this->query($query);
return isset($results[0]) ? $results[0] : array();
}
/**
* Gets the results of the query.
*
* @param string $table
* @param string|array|null $filters [optional]
* @param array $sort
* @param int|float $start
* @param int|float $range
* @param int|float|null $index [optional]
* @return array|null
*/
public function getRows(
$table,
$filters = null,
array $sort = array(),
$start = 0,
$range = 0,
$index = null
) {
Argument::i()
->test(1, 'string')
->test(2, 'string', 'array', 'null')
->test(3, 'array')
->test(4, 'int', 'float')
->test(5, 'int', 'float')
->test(6, 'int', 'float', 'null');
$query = $this->select()->from($table);
// if filters is an array
if (is_array($filters)) {
// for each filter
foreach ($filters as $i => $filter) {
// skip if filter is not an array
if (!is_array($filter)) {
continue;
}
// we want to transform it into a string
// array('post_id=%s AND post_title IN %s', 123, array('asd'));
$format = array_shift($filter);
$filters[$i] = vsprintf($format, $filter);
}
}
// at this point filters is either a string or null
// if it's a string
if (!is_null($filters)) {
// add it to the where
$query->where($filters);
}
// if we have sorting
if (!empty($sort)) {
// for each sort
foreach ($sort as $key => $value) {
// if it's a valid string
if (is_string($key) && trim($key)) {
// add sort
$query->sortBy($key, $value);
}
}
}
// if there is a range
if ($range) {
// add the pagination
$query->limit($start, $range);
}
// run the query
$results = $this->query($query);
// if index is set
if (!is_null($index)) {
// if there are no results anyways
if (empty($results)) {
// return null
return null;
}
// if index equals string first
if ($index == self::FIRST) {
$index = 0;
// else if index equals string last
} elseif ($index == self::LAST) {
$index = count($results) - 1;
}
// if the index is not set in results
if (!isset($results[$index])) {
return null;
}
$results = $results[$index];
}
return $results;
}
/**
* Gets the total count of the query.
*
* @param string $table
* @param string|array|null $filters [optional]
* @return int
*/
public function getRowsCount($table, $filters = null)
{
Argument::i()
->test(1, 'string')
->test(2, 'string', 'array', 'null');
$query = $this->select('COUNT(*)')->from($table);
// if filters is an array
if (is_array($filters)) {
// for each filter
foreach ($filters as $i => $filter) {
// skip if filter is not an array
if (!is_array($filter)) {
continue;
}
// we want to transform it into a string
// array('post_id=%s AND post_title IN %s', 123, array('asd'));
$format = array_shift($filter);
$filters[$i] = vsprintf($format, $filter);
}
}
// at this point filters is either a string or null
// if it's a string
if (!is_null($filters)) {
// add it to the where
$query->where($filters);
}
// run the query
$results = $this->query($query);
if (isset($results)) {
return sizeOf($results);
}
return 0;
}
/**
* Gets the fql queries.
*
* @param string|int|null $index [optional]
* @return array
*/
public function getQueries($index = null)
{
Argument::i()->test(1, 'string', 'int', 'null');
if (is_null($index)) {
return $this->queries;
}
if ($index == self::FIRST) {
$index = 0; // gets the first query
}
if ($index == self::LAST) {
$index = count($this->queries) - 1; // gets the last query
}
// if the query index exists
if (isset($this->queries[$index])) {
return $this->queries[$index];
}
return array();
}
/**
* Gets the response with specified query.
*
* @param string|array $query
* @return array
* @throws \Eden\Facebook\Exception if response message is error
*/
public function query($query)
{
Argument::i()->test(1, 'string', 'array', self::SELECT);
// if query is a string
if (!is_array($query)) {
$query = array('q' => (string) $query);
// query is an array
} else {
// for the q
foreach ($query as $key => $select) {
$query[$key] = (string) $select;
}
$query = array('q' => json_encode($query));
}
// add the access token
$query['access_token'] = $this->token;
$url = self::FQL_URL . '?' . http_build_query($query);
// get the results
$results = Curl::i()
->setUrl($url)
->setConnectTimeout(10)
->setFollowLocation(true)
->setTimeout(60)
->verifyPeer(false)
->setUserAgent(Auth::USER_AGENT)
->setHeaders('Expect')
->getJsonResponse();
// store a historical data on it
$this->queries[] = array(
'query' => $query['q'],
'results' => $results);
// if there is an error
if (isset($results['error']['message'])) {
// throw an exception
Exception::i()
->setMessage($results['error']['message'])
->trigger();
}
return $results['data'];
}
/**
* Returns a new instance of Search class.
*
* @return \Eden\Facebook\Fql\Search
*/
public function search()
{
return Search::i($this);
}
/**
* Returns a new instance of Select class.
*
* @param string|array $select columns to be selected (Default: *)
* @return \Eden\Facebook\Fql\Select
*/
public function select($select = '*')
{
Argument::i()->test(1, 'string', 'array');
return Select::i($select);
}
}