1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
3
|
|
|
|
4
|
|
|
use Igni\Storage\Storable; |
5
|
|
|
use Igni\Storage\Storage; |
6
|
|
|
use Igni\Storage\Id\GenericId; |
7
|
|
|
use Igni\Storage\Mapping\Annotation\Entity; |
8
|
|
|
use Igni\Storage\Mapping\Annotation\Property; |
9
|
|
|
use Igni\Storage\Id; |
10
|
|
|
use Igni\Storage\Hydration\ObjectHydrator; |
11
|
|
|
use Igni\Storage\Hydration\GenericHydrator; |
12
|
|
|
use Igni\Storage\Driver\Pdo\Connection; |
13
|
|
|
use Igni\Storage\Driver\Pdo\ConnectionOptions; |
14
|
|
|
use Igni\Storage\Driver\Pdo\Repository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is your custom hydrator - it implements decorator pattern. |
18
|
|
|
* $baseHydrator is a default hydrator generated by the framework, |
19
|
|
|
* it contains base hydration logic which can be extended in this class. |
20
|
|
|
*/ |
21
|
|
|
final class TrackHydrator implements ObjectHydrator |
22
|
|
|
{ |
23
|
|
|
private $baseHydrator; |
24
|
|
|
|
25
|
|
|
public function __construct(GenericHydrator $baseHydrator) |
26
|
|
|
{ |
27
|
|
|
$this->baseHydrator = $baseHydrator; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function hydrate(array $data) |
31
|
|
|
{ |
32
|
|
|
/** @var Track $entity */ |
33
|
|
|
$entity = $this->baseHydrator->hydrate($data); |
34
|
|
|
$entity->setAlbum('Unknown album'); |
|
|
|
|
35
|
|
|
// Here do custom hydration |
36
|
|
|
return $entity; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function extract($entity): array |
40
|
|
|
{ |
41
|
|
|
$data = $this->baseHydrator->extract($entity); |
42
|
|
|
// Here extract additional properties |
43
|
|
|
return $data; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This entity will be hydrated with our custom hydrator class. |
49
|
|
|
* Please take a careful look at the annotation below- it is using hydrator |
50
|
|
|
* property so framework knows which class should be used during the hydration. |
51
|
|
|
* |
52
|
|
|
* @Entity(source="tracks", hydrator=TrackHydrator::class) |
53
|
|
|
*/ |
54
|
|
|
class Track implements Storable |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @Property(type="id", name="TrackId", class=GenericId::class) |
58
|
|
|
*/ |
59
|
|
|
protected $id; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @Property(type="string", name="Name") |
63
|
|
|
*/ |
64
|
|
|
protected $name; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Please note: that this property is not hydrated by generic hydrator. |
68
|
|
|
* It gets hydrated by TrackHydrator. |
69
|
|
|
*/ |
70
|
|
|
protected $album; |
71
|
|
|
|
72
|
|
|
public function __construct(string $name) |
73
|
|
|
{ |
74
|
|
|
$this->name = $name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setAlbum(string $album) |
78
|
|
|
{ |
79
|
|
|
$this->album = $album; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getAlbum() |
83
|
|
|
{ |
84
|
|
|
return $this->album; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getId(): Id |
88
|
|
|
{ |
89
|
|
|
return $this->id; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Below we setup bootstrap; connection and unit of work instance (Storage instance) |
94
|
|
|
|
95
|
|
|
$sqlLiteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions('sqlite')); |
96
|
|
|
|
97
|
|
|
$unitOfWork = new Storage(); |
98
|
|
|
|
99
|
|
|
// Repository has to be registered so framework knows which class is responsible for obtaining given entity from database. |
100
|
|
|
$unitOfWork->addRepository(new class($sqlLiteConnection, $unitOfWork->getEntityManager()) extends Repository { |
|
|
|
|
101
|
|
|
public function getEntityClass(): string |
102
|
|
|
{ |
103
|
|
|
return Track::class; |
104
|
|
|
} |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
// Now database is queried and data gets hydrated with custom hydrator to instance of Track class. |
108
|
|
|
$track = $unitOfWork->get(Track::class, 1); |
109
|
|
|
|
110
|
|
|
$track->getAlbum();// Unknown album. |
|
|
|
|
111
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.