-
-
Save johnnncodes/7c075a63f62661742634 to your computer and use it in GitHub Desktop.
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 | |
$hydrator = new UserHydrator(new BlogPostsHydrator(new BlogPostHydrator())); | |
$user = $em->find('UserEntity', 123); | |
var_dump($hydrator->extract($user)); |
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 | |
class PostEntity | |
{ | |
private $id; | |
private $user; | |
private $title; | |
private $content; | |
private $date; | |
// @todo getters/setters | |
} |
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 | |
use Zend\Stdlib\Hydrator\HydratorInterface; | |
class PostHydrator implements HydratorInterface | |
{ | |
private $postsHydrator; | |
public function __construct(HydratorInterface $postsHydrator) | |
{ | |
$this->postsHydrator = $postsHydrator; | |
} | |
public function hydrate(array $data, $object) | |
{ | |
// @todo won't cover this | |
} | |
public function extract($object) | |
{ | |
if (! $object instanceof PostEntity) { | |
throw new UnsupportedObjectException(); | |
} | |
return array( | |
'id' => $object->getId(), | |
'title' => $object->getTitle(), | |
'content' => $object->getContent(), | |
'date' => $object->getDate()->format('Y-m-d H:i:s'), | |
); | |
} | |
} |
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 | |
use Zend\Stdlib\Hydrator\HydratorInterface; | |
class PostsHydrator implements HydratorInterface | |
{ | |
private $postHydrator; | |
public function __construct(HydratorInterface $postsHydrator) | |
{ | |
$this->postHydrator = $postsHydrator; | |
} | |
public function hydrate(array $data, $object) | |
{ | |
// @todo won't cover this | |
} | |
public function extract($object) | |
{ | |
if (! $object instanceof \Doctrine\Common\Collections\Collection) { | |
throw new UnsupportedObjectException(); | |
} | |
$data = []; | |
foreach ($object as $key => $value) { | |
$data[$key] = $this->postHydrator->extract($value); | |
} | |
return $data; | |
} | |
} |
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 | |
class UserEntity | |
{ | |
private $id; | |
private $name; | |
private $surname; | |
private $posts; | |
public function __construct() | |
{ | |
$this->posts = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getSurname() | |
{ | |
return $this->surname; | |
} | |
public function getPosts() | |
{ | |
return $this->posts; | |
} | |
public function setName($name) { /* ... */ } | |
public function setSurname($surname) { /* ... */ } | |
} |
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 | |
use Zend\Stdlib\Hydrator\HydratorInterface; | |
class UserHydrator implements HydratorInterface | |
{ | |
private $postsHydrator; | |
public function __construct(HydratorInterface $postsHydrator) | |
{ | |
$this->postsHydrator = $postsHydrator; | |
} | |
public function hydrate(array $data, $object) | |
{ | |
// @todo won't cover this | |
} | |
public function extract($object) | |
{ | |
if (! $object instanceof UserEntity) { | |
throw new UnsupportedObjectException(); | |
} | |
return array( | |
'id' => $object->getId(), | |
'name' => $object->getName(), | |
'surname' => $object->getSurname(), | |
'posts' => $this->postsHydrator->extract($object->getPosts()), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment