github twitter linkedin email rss
Hydrator
May 13, 2017
2 minutes read

November 2016 I’ve implemented and released a Hydrator library but never properly announced it. As far as I know, the “hydrator” term was first used in Hibernate Java ORM. The job of a hydrator is to fill an object with data or extract data from an object without calling constructor or extra getter-setter methods. It allows you to directly work with private properties which should be persisted to database or loaded from database while not exposing these properties thus keeping public interface clean.

Internally it uses PHP reflection.

Usage

Consider we have a Post entity which represents a blog post. It has a title and a text. A unique id is generated to identify it.

class Post
{
    private $id;
    protected $title;
    protected $text;

    public function __construct($title, $text)
    {
        $this->id = uniqid('post_', true);
        $this->title = $title;
        $this->text = $text;
    }
   
    public function getId()
    {
        return $this->id;
    }
    
    public function getTitle()
    {
        return $this->title;
    }
    
    public function setTitle($title)
    {
        $this->title = $title;
    }
    
    public function getText()
    {
        return $this->text;
    }
    
    public function setText()
    {
        return $this->text;
    }
}

Saving a post to database:

$post = new Post('First post', 'Hell, it is a first post.');

$postHydrator = new \samdark\hydrator\Hydrator([
    'id' => 'id',
    'title' => 'title',
    'text' => 'text',
]);

$data = $postHydrator->extract($post);
save_to_database($data);

Loading post from database:

<?php
$data = load_from_database();

$postHydrator = new \samdark\hydrator\Hydrator([
    'id' => 'id',
    'title' => 'title',
    'text' => 'text',
]);

$post = $postHydrator->hydrate($data, Post::class);
echo $post->getId();

Filling existing post object with data:

$data = load_from_database();

$postHydrator = new \samdark\hydrator\Hydrator([
    'title' => 'title',
    'text' => 'text',
]);

$post = get_post();
$post = $postHydrator->hydrateInto($data, $post);
echo $post->getTitle();

Usage in Yii

It’s not currently used in Yii in any way. One may use it to implement his own data mapping in a repository in case of purchasing clean architecture and domain driven design where encapsulation is uber-important.


Back to posts


comments powered by Disqus