1 | <?php |
||
21 | class Chirp |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * The Database object |
||
26 | * |
||
27 | * @var \MongoDB\Database |
||
28 | */ |
||
29 | private $db = null; |
||
30 | |||
31 | /** |
||
32 | * The instance of TwitterOAuth |
||
33 | * |
||
34 | * @var \Abraham\TwitterOAuth\TwitterOAuth |
||
35 | */ |
||
36 | private $twitter; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * Initialize TwitterAPIExchange and MongoDB connection |
||
41 | * |
||
42 | * $twitterAuthConf must contain |
||
43 | * - consumer_key |
||
44 | * - consumer_secret |
||
45 | * - oauth_access_token |
||
46 | * - oauth_access_token_secret |
||
47 | * |
||
48 | * $mongoConf must contain |
||
49 | * - db: the name of mongo database to use |
||
50 | * |
||
51 | * and can contain |
||
52 | * - uri |
||
53 | * - uriOptions |
||
54 | * - driverOptions |
||
55 | * |
||
56 | * used for MongoDB connection |
||
57 | * |
||
58 | * @see \MongoDB\Client for $mongoConf |
||
59 | * @param array $twitterAuthConf |
||
60 | * @param array $mongoConf |
||
61 | */ |
||
62 | public function __construct(array $twitterAuthConf, array $mongoConf = array()) |
||
88 | |||
89 | /** |
||
90 | * Return TwitterOAuth instance |
||
91 | * |
||
92 | * @return \Abraham\TwitterOAuth\TwitterOAuth |
||
93 | */ |
||
94 | public function getTwitterConnection() |
||
98 | |||
99 | /** |
||
100 | * Return the instance of MongoDB database |
||
101 | * |
||
102 | * @return \MongoDB\Database |
||
103 | */ |
||
104 | public function getDb() |
||
108 | |||
109 | /** |
||
110 | * Starting from twitter endpoint return the related collection |
||
111 | * It replaces "/" with "-" after removing trailing "/", for example: |
||
112 | * |
||
113 | * endpoint "statuses/user_timeline" corresponds to "statuses-user_timeline" collection |
||
114 | * |
||
115 | * @param string $endpoint [description] |
||
116 | * @return \MongoDB\Collection |
||
117 | */ |
||
118 | public function getCollection($endpoint) |
||
124 | |||
125 | /** |
||
126 | * Perform a Twitter request |
||
127 | * |
||
128 | * @param string $endpoint the endpoint for example 'statuses/user_timeline' |
||
129 | * @param array $query an array that specify query string to use with the endpoint |
||
130 | * @param string $requestMethod the http request method |
||
131 | * @return array |
||
132 | */ |
||
133 | public function request($endpoint, $query = [], $requestMethod = 'get') |
||
140 | |||
141 | /** |
||
142 | * Read results from a collection (default self::collection) |
||
143 | * using $filter to filter results |
||
144 | * |
||
145 | * If $options['limit'] = 1 return a \MongoDB\BSONDocument object |
||
146 | * else return an array or a cursor depending from $options['returnType'] |
||
147 | * |
||
148 | * @param string $endpoint |
||
149 | * @param array $filter |
||
150 | * @param array $options |
||
151 | * @return object|array |
||
152 | */ |
||
153 | public function read($endpoint, array $filter = [], array $options = []) |
||
164 | |||
165 | /** |
||
166 | * Perform twitter request and save results in db. |
||
167 | * Only requests returing tweets are persisted. |
||
168 | * |
||
169 | * Possible $options are: |
||
170 | * - query: an array used for compose query string |
||
171 | * - grep: a string to search in 'text' field. Result containing that string will be saved |
||
172 | * - require: only results with that field will be saved |
||
173 | * |
||
174 | * @param string $endpoint the twitter endpoint for example 'statuses/user_timeline' |
||
175 | * @param array $options |
||
176 | * @return array |
||
177 | */ |
||
178 | public function write($endpoint, array $options = []) |
||
209 | |||
210 | /** |
||
211 | * Given an array of tweets and a collection |
||
212 | * return the tweets that missing from the collection |
||
213 | * |
||
214 | * @see self::write() for $options |
||
215 | * @param array $tweets |
||
216 | * @param \MongoDB\Collection $collection |
||
217 | * @param array $options |
||
218 | * @return array |
||
219 | */ |
||
220 | private function filterToSave(array $tweets, \MongoDB\Collection $collection, array $options) |
||
241 | } |
||
242 |