1 | <?php |
||
22 | class Chirp |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * The Database object |
||
27 | * |
||
28 | * @var \MongoDB\Database |
||
29 | */ |
||
30 | private $db = null; |
||
31 | |||
32 | /** |
||
33 | * The instance of TwitterOAuth |
||
34 | * |
||
35 | * @var \Abraham\TwitterOAuth\TwitterOAuth |
||
36 | */ |
||
37 | private $twitter; |
||
38 | |||
39 | /** |
||
40 | * Constructor |
||
41 | * |
||
42 | * Passing $twitterAuthConf and/or $mongoConf it tries to setup them |
||
43 | * |
||
44 | * $twitterAuthConf must contain |
||
45 | * - consumer_key |
||
46 | * - consumer_secret |
||
47 | * - oauth_access_token |
||
48 | * - oauth_access_token_secret |
||
49 | * |
||
50 | * $mongoConf must contain |
||
51 | * - db: the name of mongo database to use |
||
52 | * |
||
53 | * and can contain |
||
54 | * - uri |
||
55 | * - uriOptions |
||
56 | * - driverOptions |
||
57 | * |
||
58 | * used for MongoDB connection |
||
59 | * |
||
60 | * @see \MongoDB\Client for $mongoConf |
||
61 | * @param array $twitterAuthConf |
||
62 | * @param array $mongoDbConf |
||
63 | */ |
||
64 | public function __construct(array $twitterAuthConf = array(), array $mongoDbConf = array()) |
||
73 | |||
74 | /** |
||
75 | * Setup TwitterOAuth |
||
76 | * |
||
77 | * $twitterAuthConf must contain |
||
78 | * - consumer_key |
||
79 | * - consumer_secret |
||
80 | * - oauth_access_token |
||
81 | * - oauth_access_token_secret |
||
82 | * |
||
83 | * @param array $twitterAuthConf |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setupTwitter(array $twitterAuthConf) |
||
103 | |||
104 | /** |
||
105 | * Setup MongoDB connection |
||
106 | * |
||
107 | * $mongoDbConf must contain |
||
108 | * - db: the name of mongo database to use |
||
109 | * |
||
110 | * and can contain |
||
111 | * - uri |
||
112 | * - uriOptions |
||
113 | * - driverOptions |
||
114 | * |
||
115 | * @see \MongoDB\Client for $mongoConf |
||
116 | * @param array $mongoDbConf |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setupMongoDb(array $mongoDbConf) |
||
131 | |||
132 | /** |
||
133 | * Return TwitterOAuth instance |
||
134 | * |
||
135 | * @return \Abraham\TwitterOAuth\TwitterOAuth |
||
136 | */ |
||
137 | public function getTwitter() |
||
146 | |||
147 | /** |
||
148 | * Return the instance of MongoDB database |
||
149 | * |
||
150 | * @return \MongoDB\Database |
||
151 | */ |
||
152 | public function getDb() |
||
161 | |||
162 | /** |
||
163 | * Starting from twitter endpoint return the related collection |
||
164 | * It replaces "/" with "-" after removing trailing "/", for example: |
||
165 | * |
||
166 | * endpoint "statuses/user_timeline" corresponds to "statuses-user_timeline" collection |
||
167 | * |
||
168 | * @param string $endpoint [description] |
||
169 | * @return \MongoDB\Collection |
||
170 | */ |
||
171 | public function getCollection($endpoint) |
||
177 | |||
178 | /** |
||
179 | * Perform a Twitter request |
||
180 | * |
||
181 | * @param string $endpoint the endpoint for example 'statuses/user_timeline' |
||
182 | * @param array $query an array that specify query string to use with the endpoint |
||
183 | * @param string $requestMethod the http request method |
||
184 | * @return array |
||
185 | */ |
||
186 | public function request($endpoint, $query = [], $requestMethod = 'get') |
||
198 | |||
199 | /** |
||
200 | * Read results from a collection (default self::collection) |
||
201 | * using $filter to filter results |
||
202 | * |
||
203 | * If $options['limit'] = 1 return a \MongoDB\BSONDocument object |
||
204 | * else return an array or a cursor depending from $options['returnType'] |
||
205 | * |
||
206 | * @param string $endpoint |
||
207 | * @param array $filter |
||
208 | * @param array $options |
||
209 | * @return object|array |
||
210 | */ |
||
211 | public function read($endpoint, array $filter = [], array $options = []) |
||
222 | |||
223 | /** |
||
224 | * Perform twitter request and save results in db. |
||
225 | * |
||
226 | * Possible $options are: |
||
227 | * - query: an array used for compose query string |
||
228 | * - grep: an array used for look up. Results matching the grep criteria will be saved. |
||
229 | * Example |
||
230 | * ``` |
||
231 | * [ |
||
232 | * 'key_to_look_up' => ['match1', 'match2'], |
||
233 | * 'other_key_to_look_up' => ['match3'] |
||
234 | * ] |
||
235 | * ``` |
||
236 | * - require: an array of string of keys required. |
||
237 | * Only results with those fields will be saved. |
||
238 | * Use point separtated string to go deep in results, for example |
||
239 | * `'key1.key2.key3'` checks against`[ 'key1' => ['key2' => ['key3' => 'some_value']]] ` |
||
240 | * |
||
241 | * @param string $endpoint the twitter endpoint for example 'statuses/user_timeline' |
||
242 | * @param array $options |
||
243 | * @return array |
||
244 | */ |
||
245 | public function write($endpoint, array $options = []) |
||
268 | |||
269 | /** |
||
270 | * Check and return $options to be used in self::write() |
||
271 | * |
||
272 | * @param array $options |
||
273 | * @return array |
||
274 | */ |
||
275 | private function prepareWriteOptions($options) |
||
290 | |||
291 | /** |
||
292 | * Given an array of tweets and a collection |
||
293 | * return the tweets that missing from the collection |
||
294 | * |
||
295 | * @see self::write() for $options |
||
296 | * @param array $tweets |
||
297 | * @param \MongoDB\Collection $collection |
||
298 | * @param array $options |
||
299 | * @return array |
||
300 | */ |
||
301 | private function filterToSave(array $tweets, \MongoDB\Collection $collection, array $options) |
||
316 | } |
||
317 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.