PHPackages                             samdark/hydrator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. samdark/hydrator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

samdark/hydrator
================

Allows to extract data from an object or create a new object based on data for the purpose of persisting state. Works with private and protected properties.

1.0.5(5mo ago)11476.9k—4.5%8[1 PRs](https://github.com/samdark/hydrator/pulls)MITPHPCI failing

Since Oct 31Pushed 5mo ago8 watchersCompare

[ Source](https://github.com/samdark/hydrator)[ Packagist](https://packagist.org/packages/samdark/hydrator)[ GitHub Sponsors](https://github.com/samdark)[ Patreon](https://www.patreon.com/samdark)[ RSS](/packages/samdark-hydrator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

Hydrator
========

[](#hydrator)

Hydrator can be used for two purposes:

- To extract data from a class to be further stored in a persistent storage.
- To fill an object with data or create a new instance of a class filled with data.

In both cases it is saving and filling protected and private properties without calling any methods which leads to ability to persist state of an object with properly encapsulated data.

[![Latest Stable Version](https://camo.githubusercontent.com/cdd5e3081c91058a733f6ea96fe525048a5a7693b216ee3b602df146109caea1/68747470733a2f2f706f7365722e707567782e6f72672f73616d6461726b2f6879647261746f722f762f737461626c652e706e67)](https://packagist.org/packages/samdark/hydrator)[![Total Downloads](https://camo.githubusercontent.com/239a587c97a32809ead151a450a8b2ce738e19635a90fbac6465848cf23a0113/68747470733a2f2f706f7365722e707567782e6f72672f73616d6461726b2f6879647261746f722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/samdark/hydrator)[![Build Status](https://camo.githubusercontent.com/ce2cbef97f81a4889532349952d33a6e66e45c7ae16bbe29fa80ed3a877957e0/68747470733a2f2f7472617669732d63692e6f72672f73616d6461726b2f6879647261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/samdark/hydrator)

Installation
------------

[](#installation)

The preferred way to install this package is through [composer](http://getcomposer.org/download/).

```
composer require --prefer-dist samdark/hydrator

```

Usage
-----

[](#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:

```
