Skip to content

Commit

Permalink
Mention hydrator
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Mar 16, 2024
1 parent 458a04b commit 6ec6352
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ $params = ['id' => 1];
$remap->map(Todo::class, $sql, $params); // returns \Generator of Todo objects
```

## Hydration

Using [yiisoft/hydrator](https://github.com/yiisoft/hydrator) brings benefits of the `Hydrator` library:
[Attributes](https://github.com/yiisoft/hydrator/blob/master/docs/guide/en/mapping.md#using-attributes) to modify the behavior of the hydration process.

```php
use Yiisoft\Hydrator\Attribute\Parameter\Data;
use Yiisoft\Hydrator\Attribute\SkipHydration;

final class TodoModelHydrator
{
#[Data('id')]
public string $identifier;
#[Data('title')]
public string $text;
public string $status;
#[Data('created_at')]
public string $date_created;
}
```

Or any other attributes compatible with the `Hydrator` library.


### Tips

#### Define query-related methods in the class
Expand Down Expand Up @@ -76,9 +100,10 @@ final class Todo
And now it's easy to use:

```php
$remap->map(Todo::class, ...Todo::selectOne(1));
$remap->map(Todo::class, Todo::selectOne(1)); // or shorter
$remap->map(Todo::class, ...Todo::selectOne(1)); // selectOne may return ['SELECT ...', ['id' => 1]]
$remap->map(Todo::class, Todo::selectOne(1)); // or shorter, without unpacking
$remap->map(Todo::class, Todo::selectAll());
$remap->map(...Todo::selectAll()); // selectAll may return [Todo::class, "SELECT ..."]
```

#### Unpack and store the result
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
failOnWarning="false"
stopOnFailure="false"
executionOrder="random"
displayDetailsOnTestsThatTriggerWarnings="true"
resolveDependencies="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
Expand Down
26 changes: 26 additions & 0 deletions tests/RemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Xepozz\Remap\Remap;
use Xepozz\Remap\Tests\Support\NullCache;
use Xepozz\Remap\Tests\Support\TodoModel;
use Xepozz\Remap\Tests\Support\TodoModelHydrator;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;
Expand Down Expand Up @@ -138,6 +139,31 @@ public static function dataSelectOne(): iterable
];
}

public function testHydrator(): void
{
$connection = $this->createConnection();
$remap = $this->createRemap($connection);
$todo = [
'id' => 1,
'title' => 'First todo',
'status' => 0,
'created_at' => strtotime('2021-01-01 00:00:00'),
];
$this->prepareTodos($connection, [$todo]);

$result = [...$remap->map(TodoModelHydrator::class, TodoModel::selectOne(1))];

$this->assertCount(1, $result);
$item = $result[0];

$this->assertInstanceOf(TodoModelHydrator::class, $item);

$this->assertEquals('1', $item->identifier);
$this->assertEquals('First todo', $item->text);
$this->assertEquals('0', $item->status);
$this->assertEquals(strtotime('2021-01-01 00:00:00'), $item->date_created);
}

private function createRemap(Connection $connection): Remap
{
return new Remap(new Hydrator(), $connection);
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/TodoModelHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Xepozz\Remap\Tests\Support;

use Yiisoft\Hydrator\Attribute\Parameter\Data;

final class TodoModelHydrator
{
#[Data('id')]
public string $identifier;
#[Data('title')]
public string $text;
public string $status;
#[Data('created_at')]
public string $date_created;
}

0 comments on commit 6ec6352

Please sign in to comment.