Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created August 5, 2013 19:47
Show Gist options
  • Save Ocramius/6158927 to your computer and use it in GitHub Desktop.
Save Ocramius/6158927 to your computer and use it in GitHub Desktop.
Example "manual" hydration with ZF2 and a Doctrine loaded entity
<?php
$hydrator = new UserHydrator(new BlogPostsHydrator(new BlogPostHydrator()));
$user = $em->find('UserEntity', 123);
var_dump($hydrator->extract($user));
<?php
class PostEntity
{
private $id;
private $user;
private $title;
private $content;
private $date;
// @todo getters/setters
}
<?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'),
);
}
}
<?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;
}
}
<?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) { /* ... */ }
}
<?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()),
);
}
}
@bitwombat
Copy link

bitwombat commented Mar 22, 2017

Typos - postHydrator shouldn't have any params in its constructor, and postsHydrator should be passed a postHydrator (not a postsHydrator).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment