Skip to content

Commit 6ec6352

Browse files
committed
Mention hydrator
1 parent 458a04b commit 6ec6352

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ $params = ['id' => 1];
4646
$remap->map(Todo::class, $sql, $params); // returns \Generator of Todo objects
4747
```
4848

49+
## Hydration
50+
51+
Using [yiisoft/hydrator](https://github.com/yiisoft/hydrator) brings benefits of the `Hydrator` library:
52+
[Attributes](https://github.com/yiisoft/hydrator/blob/master/docs/guide/en/mapping.md#using-attributes) to modify the behavior of the hydration process.
53+
54+
```php
55+
use Yiisoft\Hydrator\Attribute\Parameter\Data;
56+
use Yiisoft\Hydrator\Attribute\SkipHydration;
57+
58+
final class TodoModelHydrator
59+
{
60+
#[Data('id')]
61+
public string $identifier;
62+
#[Data('title')]
63+
public string $text;
64+
public string $status;
65+
#[Data('created_at')]
66+
public string $date_created;
67+
}
68+
```
69+
70+
Or any other attributes compatible with the `Hydrator` library.
71+
72+
4973
### Tips
5074

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

78102
```php
79-
$remap->map(Todo::class, ...Todo::selectOne(1));
80-
$remap->map(Todo::class, Todo::selectOne(1)); // or shorter
103+
$remap->map(Todo::class, ...Todo::selectOne(1)); // selectOne may return ['SELECT ...', ['id' => 1]]
104+
$remap->map(Todo::class, Todo::selectOne(1)); // or shorter, without unpacking
81105
$remap->map(Todo::class, Todo::selectAll());
106+
$remap->map(...Todo::selectAll()); // selectAll may return [Todo::class, "SELECT ..."]
82107
```
83108

84109
#### Unpack and store the result

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
failOnWarning="false"
66
stopOnFailure="false"
77
executionOrder="random"
8+
displayDetailsOnTestsThatTriggerWarnings="true"
89
resolveDependencies="true"
910
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
1011
cacheDirectory=".phpunit.cache"

tests/RemapTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Xepozz\Remap\Remap;
1010
use Xepozz\Remap\Tests\Support\NullCache;
1111
use Xepozz\Remap\Tests\Support\TodoModel;
12+
use Xepozz\Remap\Tests\Support\TodoModelHydrator;
1213
use Yiisoft\Db\Cache\SchemaCache;
1314
use Yiisoft\Db\Sqlite\Connection;
1415
use Yiisoft\Db\Sqlite\Driver;
@@ -138,6 +139,31 @@ public static function dataSelectOne(): iterable
138139
];
139140
}
140141

142+
public function testHydrator(): void
143+
{
144+
$connection = $this->createConnection();
145+
$remap = $this->createRemap($connection);
146+
$todo = [
147+
'id' => 1,
148+
'title' => 'First todo',
149+
'status' => 0,
150+
'created_at' => strtotime('2021-01-01 00:00:00'),
151+
];
152+
$this->prepareTodos($connection, [$todo]);
153+
154+
$result = [...$remap->map(TodoModelHydrator::class, TodoModel::selectOne(1))];
155+
156+
$this->assertCount(1, $result);
157+
$item = $result[0];
158+
159+
$this->assertInstanceOf(TodoModelHydrator::class, $item);
160+
161+
$this->assertEquals('1', $item->identifier);
162+
$this->assertEquals('First todo', $item->text);
163+
$this->assertEquals('0', $item->status);
164+
$this->assertEquals(strtotime('2021-01-01 00:00:00'), $item->date_created);
165+
}
166+
141167
private function createRemap(Connection $connection): Remap
142168
{
143169
return new Remap(new Hydrator(), $connection);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Xepozz\Remap\Tests\Support;
6+
7+
use Yiisoft\Hydrator\Attribute\Parameter\Data;
8+
9+
final class TodoModelHydrator
10+
{
11+
#[Data('id')]
12+
public string $identifier;
13+
#[Data('title')]
14+
public string $text;
15+
public string $status;
16+
#[Data('created_at')]
17+
public string $date_created;
18+
}

0 commit comments

Comments
 (0)