2017 © Pedro Peláez
 

library hydrator

Hydrate objects from an array and extracting data from objects.

image

solbianca/hydrator

Hydrate objects from an array and extracting data from objects.

  • Saturday, January 20, 2018
  • by solbianca
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Hydrator

Build Status Scrutinizer Code Quality, (*1)

Hydrator can be used for two purposes:, (*2)

  • To extract data from a class.
  • 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. Any static properties will be ignored., (*3)

Installation

The preferred way to install this package is through composer., (*4)

composer require --prefer-dist solbianca/hydrator

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., (*5)

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:, (*6)

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

$hydrator = new \SolBianca\Hydrator\Hydrator();

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

  OR

$data = $hydrator->extract($post, ['id', 'title']); // extract id and title form object
save_to_database($data);

Loading post from database:, (*7)

$data = load_from_database();

$hydrator = new \SolBianca\Hydrator\Hydrator();

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

Filling existing post object with data:, (*8)

$data = load_from_database();

$hydrator = new \SolBianca\Hydrator\Hydrator();

$post = get_post();
$post = $hydrator->hydrate($post, $data);
echo $post->getTitle();

The Versions

20/01 2018

dev-master

9999999-dev https://github.com/solbianca/hydrator

Hydrate objects from an array and extracting data from objects.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Alexander Fedorov

extract hydrate hydrator

20/01 2018

v1.0.0

1.0.0.0 https://github.com/solbianca/hydrator

Hydrate objects from an array and extracting data from objects.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Alexander Fedorov

extract hydrate hydrator