Created
May 16, 2014 16:11
-
-
Save jamsesso/f73f95f4c2572d8bc4b3 to your computer and use it in GitHub Desktop.
Assembler Layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * | |
| */ | |
| App::uses('UserEDM', 'Entity'); | |
| use Facebook\GraphObject; | |
| class UserAssembler implements EntityAssembler { | |
| const FB_FIRST_NAME = 'first_name'; | |
| const FB_LAST_NAME = 'last_name'; | |
| const FB_EMAIL = 'email'; | |
| /** | |
| * Assemble a new entity given an arbitrary object. | |
| * | |
| * @param $object The arbitrary object to attempt to assemble. | |
| * @return UserEDM | |
| * @throws CannotAssembleEntityException if $object is not one of [GraphObject, Array] | |
| */ | |
| public static function toEntity($object) { | |
| $result = new UserEDM(); | |
| if($object instanceof GraphObject) { | |
| self::toEntityFromGraphObject($object, $result); | |
| } | |
| elseif(is_array($object)) { | |
| self::toEntityFromArray($object, $result); | |
| } | |
| else { | |
| throw new CannotAssembleEntityException('UserAssembler expects one of GraphObject, Array'); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * Convert a GraphObject from Facebook into a User entity. | |
| * | |
| * @param GraphObject $graphObject The return object of a Facebook session. | |
| * @param UserEDM $result The resulting entity to set attributes to. | |
| */ | |
| private static function toEntityFromGraphObject(GraphObject $graphObject, UserEDM $result) { | |
| $result->setFirstName($graphObject->getProperty(self::FB_FIRST_NAME)); | |
| $result->setLastName($graphObject->getProperty(self::FB_LAST_NAME)); | |
| $result->setEmail($graphObject->getProperty(self::FB_EMAIL)); | |
| } | |
| /** | |
| * Convert an Array from the repository layer into a User entity. | |
| * | |
| * @param array $user The array that contains user data. | |
| * @param UserEDM $result The resulting user entity object. | |
| */ | |
| private static function toEntityFromArray(Array $user, UserEDM $result) { | |
| $result->setFirstName($user['mbfirstname']); | |
| $result->setLastName($user['mblastname']); | |
| $result->setEmail($user['mbemail']); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment