/** * The vast majority of the custom find types actually follow the same format * so there was little point explicitly writing them all out. Instead, if the * method corresponding to the custom find type doesn't exist, the options are * applied to the model's request property here and then we just call * parent::find('all') to actually trigger the request and return the response * from the API. * * In addition, if you try to fetch a timeline that supports paging, but you * don't specify paging params, you really want all tweets in that timeline * since time imemoriam. But twitter will only return a maximum of 200 per * request. So, we make multiple calls to the API for 200 tweets at a go, for * subsequent pages, then merge the results together before returning them. * * Twitter's API uses a count parameter where in CakePHP we'd normally use * limit, so we also copy the limit value to count so we can use our familiar * params. * * @param string $type * @param array $options * @return mixed */ public function find($type = 'first', $options = array()) { if (method_exists($this, '_find' . Inflector::camelize($type))) { return parent::find($type, $options); } $this->_setupRequest($type, $options); return parent::find('all', $options); }
/** * The vast majority of the custom find types actually follow the same format * so there was little point explicitly writing them all out. Instead, if the * method corresponding to the custom find type doesn't exist, the options are * applied to the model's request property here and then we just call * parent::find('all') to actually trigger the request and return the response * from the API. * * In addition, if you try to fetch a timeline that supports paging, but you * don't specify paging params, you really want all tweets in that timeline * since time imemoriam. But twitter will only return a maximum of 200 per * request. So, we make multiple calls to the API for 200 tweets at a go, for * subsequent pages, then merge the results together before returning them. * * Twitter's API uses a count parameter where in CakePHP we'd normally use * limit, so we also copy the limit value to count so we can use our familiar * params. * * @param string $type * @param array $options * @return mixed */ public function find($type = 'first', $options = array()) { if (!empty($options['limit']) && empty($options['count'])) { $options['count'] = $options['limit']; } if ($type != 'list' && method_exists($this, '_find' . Inflector::camelize($type))) { return parent::find($type, $options); } $this->_setupRequest($type, $options); return parent::find('all', $options); }
/** * The vast majority of the custom find types actually follow the same format * so there was little point explicitly writing them all out. Instead, if the * method corresponding to the custom find type doesn't exist, the options are * applied to the model's request property here and then we just call * parent::find('all') to actually trigger the request and return the response * from the API. * * In addition, if you try to fetch a timeline that supports paging, but you * don't specify paging params, you really want all tweets in that timeline * since time imemoriam. But twitter will only return a maximum of 200 per * request. So, we make multiple calls to the API for 200 tweets at a go, for * subsequent pages, then merge the results together before returning them. * * Twitter's API uses a count parameter where in CakePHP we'd normally use * limit, so we also copy the limit value to count so we can use our familiar * params. * * @param string $type * @param array $options * @return mixed * @throws RuntimeException */ public function find($type = 'first', $options = array()) { if ($type === 'all') { $type = 'receipt'; } if (in_array('count', $this->allowedFindOptions[$type])) { $defaults = array('count' => $this->maxCount, 'strict' => false); $options = array_merge($defaults, $options); if (!empty($options['limit']) && $options['limit'] <= $this->maxCount) { $options['count'] = $options['limit']; } } if (empty($options['page']) && array_key_exists($type, $this->allowedFindOptions) && in_array('page', $this->allowedFindOptions[$type]) && in_array('count', $this->allowedFindOptions[$type])) { $options['page'] = 1; $results = array(); try { while (($page = $this->find($type, $options)) != false) { $results = array_merge($results, $page); if (count($page) < $options['count']) { break; } if (!empty($options['limit']) && count($results) >= $options['limit']) { $results = array_slice($results, 0, $options['limit']); break; } // get next page if (isset($this->response['next_page'])) { parse_str(parse_url($this->response['next_page'], PHP_URL_QUERY), $nextPage); $options = am($options, $nextPage); } elseif (in_array('since_id', $this->allowedFindOptions[$type]) && !empty($options['since_id'])) { if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) { $options['since_id'] = bcadd($page[0]['id_str'], '1'); // for 32bit } else { $options['since_id'] = $page[0]['id'] + 1; } } elseif (in_array('max_id', $this->allowedFindOptions[$type])) { if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) { $options['max_id'] = bcsub($page[count($page) - 1]['id_str'], '1'); // for 32bit } else { $options['max_id'] = $page[count($page) - 1]['id'] - 1; } } else { $options['page']++; } // adjust count if (!empty($options['limit']) && $options['limit'] < count($results) + $options['count']) { $options['count'] = $options['limit'] - count($results); } } } catch (RuntimeException $e) { if ($options['strict']) { throw $e; } $this->log($e->getMessage(), LOG_DEBUG); } return $results; } if (method_exists($this, '_find' . Inflector::camelize($type))) { return parent::find($type, $options); } $this->_setupRequest($type, $options); if (in_array($type, array('all', 'receipt'))) { $this->request['uri']['path'] = '1.1/direct_messages'; } return parent::find('all', $options); }
/** * * @param string $type * @param array $options * @return array * @throws InvalidArgumentException */ public function find($type = 'first', $options = array()) { if (is_string($type) && empty($options)) { $options = $type; $type = 'tweets'; } if ($type === 'search') { $type = 'tweets'; } if (is_string($options)) { $options = array('q' => $options); } if (empty($options['q'])) { throw new InvalidArgumentException(__d('twim', 'You must enter a query.')); } $defaults = array('count' => $this->maxCount, 'limit' => $this->resultLimit, 'strict' => false); $options = array_merge($defaults, $options); if (!empty($options['limit']) && $options['limit'] <= $this->maxCount) { $options['count'] = $options['limit']; } if (empty($options['page'])) { $options['page'] = 1; $results = array(); try { while (($page = $this->find($type, $options)) != false) { $results = array_merge($results, $page); if (count($page) < $options['count']) { break; } if (!empty($options['limit']) && count($results) >= $options['limit']) { $results = array_slice($results, 0, $options['limit']); break; } // get next page if (isset($this->response['search_metadata']['next_results'])) { parse_str(parse_url($this->response['search_metadata']['next_results'], PHP_URL_QUERY), $nextPage); $options = am($options, $nextPage); } elseif (in_array('since_id', $this->allowedFindOptions[$type]) && !empty($options['since_id'])) { if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) { $options['since_id'] = bcadd($page[0]['id_str'], '1'); // for 32bit } else { $options['since_id'] = $page[0]['id'] + 1; } } elseif (in_array('max_id', $this->allowedFindOptions[$type])) { if (PHP_INT_SIZE === 4 && extension_loaded('bcmath')) { $options['max_id'] = bcsub($page[count($page) - 1]['id_str'], '1'); // for 32bit } else { $options['max_id'] = $page[count($page) - 1]['id'] - 1; } } // adjust count if (!empty($options['limit']) && $options['limit'] < count($results) + $options['count']) { $options['count'] = $options['limit'] - count($results); } } } catch (RuntimeException $e) { if ($options['strict']) { throw $e; } $this->log($e->getMessage(), LOG_DEBUG); } return $results; } $this->_setupRequest($type, $options); $results = parent::find('all', $options); $results = isset($results['statuses']) ? $results['statuses'] : $results; return $results; }