1. Go to this page and download the library: Download elisdn/php-hydrator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
elisdn / php-hydrator example snippets
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;
}
}
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'],
);
}
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.