elisdn / php-hydrator
Simple object hydrator
Installs: 41 452
Dependents: 0
Suggesters: 0
Security: 0
Stars: 41
Watchers: 4
Forks: 4
Open Issues: 1
Requires
- php: >=5.4.0
Requires (Dev)
- php: >=5.6.0
- phpunit/phpunit: ^6.2
This package is not auto-updated.
Last update: 2024-12-28 00:03:46 UTC
README
The library allows to hydrate objects from an array and extract raw objects's values to an array.
Installation
Install with composer:
composer require elisdn/php-hydrator
Usage samples
Hydrating an object from an associative array:
$post = $this->hydrator->hydrate(Post::class, [ 'id' => $id, 'date' => $date, 'title' => $title, ])
Extracting of object values to an array by list of properies:
$data = $this->hydrator->extract($post, ['id', 'date', 'title'])
For example if we have Post
entity:
class Post { private $id; private $title; private $date; public function __construct($id, $title) { $this->id = $id; $this->title = $title; $this->date = new DateTimeImmutable(); } public function getId() { return $this->id; } public function getTitle() { return $this->title; } }
then we can use the hydrator in any repository class:
class SqlPostRepository implements PostRepository { private $db; private $hydrator; public function get($id): Post { if (!$row = $this->db->selectOne('posts', ['id' => $id])) { throw new NotFoundException('Post not found.'); } return $this->hydrator->hydrate(Post::class, [ 'id' => $row['id'], 'date' => DateTimeImmutable::createFromFormat('Y-m-d', $row['create_date']), 'title' => $row['title'], ]); } public function persist(Post $post): void { $data = $this->hydrator->extract($post, ['id', 'date', 'title']) $this->db->insert('posts', [ 'id' => $data['id'], 'create_date' => $data['date']->format('Y-m-d'), 'title' => $data['title'], ); } }