/** * @test */ public function it_can_be_safely_encoded_to_json() { $user = new User(); $user->id = 1; $user->nick = 'foo'; $user->mbox = '*****@*****.**'; $follower = new \CultureFeed_Pages_Follower(); $page = new \CultureFeed_Cdb_Item_Page(); $page->setId(10); $page->setName('Page'); $follower->page = $page; $follower->user = $user; $user->following = array($follower); $encoded = json_encode($user); $this->assertJsonEquals($encoded, 'User/data/user.json'); }
public function testParsePage() { $xml = $this->loadSample('page.xml'); $page = CultureFeed_Cdb_Item_Page::parseFromCdbXml($xml); $address = $page->getAddress(); $this->assertInstanceOf('CultureFeed_Cdb_Data_Address_PhysicalAddress', $address); $geo = $address->getGeoInformation(); $this->assertInstanceOf('CultureFeed_Cdb_Data_Address_GeoInformation', $geo); $this->assertEquals(5.3537689, $geo->getXCoordinate()); $this->assertEquals(50.9216401, $geo->getYCoordinate()); }
/** * Parse a message from an xml element. * @param unknown $xmlElement */ public static function parseFromXml(CultureFeed_SimpleXMLElement $xmlElement) { $message = new self(); // General properties. $message->id = $xmlElement->xpath_str('id'); $message->type = $xmlElement->xpath_str('type'); $message->status = $xmlElement->xpath_str('status'); $message->creationDate = $xmlElement->xpath_time('creationDate'); $message->lastReply = $xmlElement->xpath_time('lastReply'); $message->subject = $xmlElement->xpath_str('subject'); $message->body = $xmlElement->xpath_str('body'); $message->role = $xmlElement->xpath_str('role'); // Parse sender. $user = new CultureFeed_User(); $user->id = $xmlElement->xpath_str('sender/rdf:id'); $user->nick = $xmlElement->xpath_str('sender/foaf:nick'); $user->depiction = $xmlElement->xpath_str('sender/foaf:depiction'); $message->sender = $user; // Parse recipients. $message->recipients = array(); $recipientElements = $xmlElement->xpath('recipients/recipient'); if ($recipientElements) { foreach ($recipientElements as $recipientElement) { $recipient = new CultureFeed_User(); $recipient->id = $recipientElement->xpath_str('rdf:id'); $recipient->nick = $recipientElement->xpath_str('foaf:nick'); $recipient->depiction = $recipientElement->xpath_str('foaf:depiction'); $message->recipients[$recipient->id] = $recipient; } } $senderPageId = $xmlElement->xpath_str('senderPageId'); if ($senderPageId) { $page = new CultureFeed_Cdb_Item_Page(); $page->setId($senderPageId); $page->setName($xmlElement->xpath_str('senderPageName')); $message->senderPage = $page; } $recipientPageId = $xmlElement->xpath_str('recipientPageId'); if ($recipientPageId) { $page = new CultureFeed_Cdb_Item_Page(); $page->setId($recipientPageId); $page->setName($xmlElement->xpath_str('recipientPageName')); $message->recipientPage = $page; } // If this is a thread, parse also the child messages. $message->children = array(); $childElements = $xmlElement->xpath('children/message'); if ($childElements) { foreach ($childElements as $childElement) { $message->children[] = CultureFeed_Messages_Message::parseFromXml($childElement); } } return $message; }
protected static function parseUser(CultureFeed_SimpleXMLElement $element) { $user = new CultureFeed_User(); $user->id = $element->xpath_str('/foaf:person/rdf:id'); $user->preferredLanguage = $element->xpath_str('/foaf:person/preferredLanguage'); $user->nick = $element->xpath_str('/foaf:person/foaf:nick'); $user->givenName = $element->xpath_str('/foaf:person/foaf:givenName'); $user->familyName = $element->xpath_str('/foaf:person/foaf:familyName'); $user->mbox = $element->xpath_str('/foaf:person/foaf:mbox'); $user->mboxVerified = $element->xpath_bool('/foaf:person/mboxVerified'); $user->gender = $element->xpath_str('/foaf:person/foaf:gender'); $user->dob = $element->xpath_time('/foaf:person/foaf:dob'); $user->hasChildren = $element->xpath_bool('/foaf:person/hasChildren'); $user->depiction = $element->xpath_str('/foaf:person/foaf:depiction'); $user->bio = $element->xpath_str('/foaf:person/bio'); $user->street = $element->xpath_str('/foaf:person/homeAddress/street'); $user->zip = $element->xpath_str('/foaf:person/homeAddress/zip'); $user->city = $element->xpath_str('/foaf:person/homeAddress/city'); $user->country = $element->xpath_str('/foaf:person/homeAddress/country'); $user->lifestyleProfile = $element->xpath_str('/foaf:person/lifestyleProfile'); $user->status = $element->xpath_str('/foaf:person/status'); $user->points = $element->xpath_str('/foaf:person/points'); $user->calendarId = $element->xpath_str('/foaf:person/calendarId'); if ($user->status) { $user->status = strtolower($user->status); } $user->openid = $element->xpath_str('/foaf:person/foaf:openid'); $lat = $element->xpath_float('/foaf:person/homeLocation/geo:lat'); $lng = $element->xpath_float('/foaf:person/homeLocation/geo:long'); if ($lat && $lng) { $user->homeLocation = new CultureFeed_Location($lat, $lng); } $lat = $element->xpath_float('/foaf:person/currentLocation/geo:lat'); $lng = $element->xpath_float('/foaf:person/currentLocation/geo:long'); if ($lat && $lng) { $user->currentLocation = new CultureFeed_Location($lat, $lng); } $accounts = array(); $objects = $element->xpath('/foaf:person/foaf:holdsAccount/foaf:onlineAccount'); foreach ($objects as $object) { $account = new CultureFeed_OnlineAccount(); $account->accountType = $object->xpath_str('accountType'); $account->accountServiceHomepage = $object->xpath_str('foaf:accountServiceHomepage'); $account->accountName = $object->xpath_str('foaf:accountName'); $account->accountNick = $object->xpath_str('accountNick'); $account->accountDepiction = $object->xpath_str('accountDepiction'); $account->private = $object->xpath_bool('private'); $account->publishActivities = $object->xpath_bool('publishActivities'); $accounts[] = $account; } if ($element->xpath_str('/foaf:person/privateNick') !== NULL) { $privacy_config = new CultureFeed_UserPrivacyConfig(); $vars = array('nick', 'givenName', 'familyName', 'mbox', 'gender', 'dob', 'depiction', 'bio', 'homeAddress', 'homeLocation', 'currentLocation', 'openId', 'calendarId'); foreach ($vars as $var) { $privacy = $element->xpath_bool('/foaf:person/private' . ucfirst($var)); if (is_bool($privacy)) { $privacy_config->{$var} = $privacy ? CultureFeed_UserPrivacyConfig::PRIVACY_PRIVATE : CultureFeed_UserPrivacyConfig::PRIVACY_PUBLIC; } } $user->privacyConfig = $privacy_config; } if (!empty($accounts)) { $user->holdsAccount = $accounts; } // Add memberships. $memberships = $element->xpath('/foaf:person/pageMemberships/pageMembership'); $user_memberships = array(); foreach ($memberships as $membership) { $pageId = $membership->xpath_str('page/uid'); if (empty($pageId)) { continue; } $user_membership = new CultureFeed_Pages_Membership(); $page = new CultureFeed_Cdb_Item_Page(); $page->setId($pageId); $page->setName($membership->xpath_str('page/name')); // Set categories $categories_element = $membership->xpath('page/categoryIds/categoryId'); $categories = array(); foreach ($categories_element as $category) { $categories[] = (string) $category; } $page->setCategories($categories); $user_membership->page = $page; $user_membership->validated = $membership->xpath_bool('validated'); $user_membership->role = $membership->xpath_str('role'); $user_membership->relation = $membership->xpath_str('relation'); $user_membership->creationDate = $membership->xpath_time('creationDate'); $user_memberships[] = $user_membership; } if (!empty($user_memberships)) { $user->pageMemberships = $user_memberships; $adminPagesCount = 0; foreach ($user->pageMemberships as $membership) { if ($membership->role == CultureFeed_Pages_Membership::MEMBERSHIP_ROLE_ADMIN) { $adminPagesCount++; } } $user->adminPagesCount = $adminPagesCount; } // Add following pages. $following = $element->xpath('/foaf:person/following/page'); foreach ($following as $object) { $pageId = $object->xpath_str('uid'); if (empty($pageId)) { continue; } $follower = new CultureFeed_Pages_Follower(); $page = new CultureFeed_Cdb_Item_Page(); $page->setId($pageId); $page->setName($object->xpath_str('name')); // Set categories $categories_element = $object->xpath('categoryIds/categoryId'); $categories = array(); foreach ($categories_element as $category) { $categories[] = (string) $category; } $page->setCategories($categories); $follower->page = $page; $follower->user = $user; $follower->creationDate = $object->xpath_time('dateCreated'); $followers[] = $follower; } if (!empty($followers)) { $user->following = $followers; } return $user; }
/** * Create an extended entity based on a given xmlElement. * @param \SimpleXMLElement $xmlElement * Pages search xml element. */ public static function fromPagesXml(\SimpleXMLElement $xmlElement) { $attributes = $xmlElement->attributes(); $type = (string) $attributes['type']; if ($type != 'page') { return NULL; } $cdbItem = \CultureFeed_Cdb_Item_Page::parseFromCdbXml($xmlElement->page); $extendedEntity = new static(); $extendedEntity->type = (string) $attributes['type']; $extendedEntity->id = $cdbItem->getId(); // Add the different activity counts. if (isset($xmlElement->activity)) { foreach ($xmlElement->activity as $activity) { $activityType = (string) $activity->attributes()->type; $extendedEntity->activityCounts[$activityType] = (int) $activity->attributes()->count; } } $extendedEntity->entity = $cdbItem; return $extendedEntity; }
/** * @see CultureFeed_Pages::getPage() */ public function getPage($id) { $result = $this->oauth_client->consumerGetAsXml('page/' . $id); try { $xml = new CultureFeed_SimpleXMLElement($result); } catch (Exception $e) { throw new CultureFeed_ParseException($result); } return CultureFeed_Cdb_Item_Page::parseFromCdbXml($xml); }