/** * Make the call * * @param string $url API url to call * @param array $parameters Parameters for the request * @param bool $authenticate Shall we use authentication? * @param bool $usePost Uses POST method instead of GET * * @return mixed * * @throws InvalidArgumentException if the type provided is not supported * @throws TwitterApiServerException if the request fails for any reason * @throws RuntimeException if the xml response is invalid */ protected function doCall($url, $parameters = array(), $authenticate = false, $usePost = true, $type = 'entity') { $response = $this->server->request(sprintf('%s.xml', $url), $parameters, $usePost ? 'POST' : 'GET'); switch ($type) { case 'entity': $dom = new DOMDocument(); $dom->loadXML($response); return TwitterEntity::createFromXml($dom); break; case 'boolean': if (!($xml = @simplexml_load_string($response))) { throw new RuntimeException('XML error'); } return (bool) ((string) $xml === 'true'); break; case 'hash': if (!($xml = @simplexml_load_string($response))) { throw new RuntimeException('XML error'); } return (array) $xml; break; case 'search_results': return TweetCollection::createFromJSON($response); break; default: throw new InvalidArgumentException(sprintf('Type "%s" is not supported', $type)); break; } }
public static function createFromArray(array $array = array()) { $entity = new self(); foreach ($array as $propertyName => $propertyValue) { if (!property_exists($entity, $propertyName)) { // ignore unknown entities like "metadata" continue; } $entity->{$propertyName} = parent::cleanValue($propertyValue); } return $entity; }
<?php require_once dirname(__FILE__) . '/../../vendor/lime/lime.php'; require_once dirname(__FILE__) . '/../../lib/Tweet.class.php'; require_once dirname(__FILE__) . '/../../lib/TweetCollection.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterUser.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterUserCollection.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterDirectMessage.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterDirectMessageCollection.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterEntity.class.php'; $t = new lime_test(6, new lime_output_color()); // createFromXml() $t->diag('createFromXML()'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/statuses/show/2043091669.xml')), 'Tweet', 'createFromXml() converts an XML status to a Tweet'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/statuses/replies.xml')), 'TweetCollection', 'createFromXml() converts an XML statuses to a TweetCollection'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/direct_messages/show/155216447.xml')), 'TwitterDirectMessage', 'createFromXml() converts an XML dm to a TwitterDirectMessage'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/direct_messages.xml')), 'TwitterDirectMessageCollection', 'createFromXml() converts an XML dms to a TwitterDirectMessageCollection'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/users/show/6896142.xml')), 'TwitterUser', 'createFromXml() converts an XML user to a TwitterUser'); $t->isa_ok(TwitterEntity::createFromXML(DOMDocument::load(dirname(__FILE__) . '/xml/server/statuses/friends.xml')), 'TwitterUserCollection', 'createFromXml() converts an XML users to a TwitterUserCollection');
private function getLastArticle(TwitterEntity $twitterStream) { $result = $this->db->execute('SELECT * FROM article WHERE stream_id = ? AND streamType = ? ORDER BY articleDate DESC LIMIT 1', array($twitterStream->getId(), ArticleModel::TWITTER)); $result->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'ArticleEntity'); if ($articleEntity = $result->fetch()) { return $articleEntity; } $articleEntity = new ArticleEntity(); $articleEntity->setArticleDate(time()); return $articleEntity; }