1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
3
|
|
|
|
4
|
|
|
use Igni\Storage\Entity; |
5
|
|
|
use Igni\Storage\Storage; |
6
|
|
|
use Igni\Storage\Id\GenericId; |
7
|
|
|
use Igni\Storage\Mapping\Annotation\Entity as Storable; |
8
|
|
|
use Igni\Storage\Mapping\Annotation\Property; |
9
|
|
|
use Igni\Storage\Id\AutoGenerateId; |
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
|
|
|
final class TrackHydrator implements ObjectHydrator |
18
|
|
|
{ |
19
|
|
|
private $baseHydrator; |
20
|
|
|
|
21
|
|
|
public function __construct(GenericHydrator $baseHydrator) |
22
|
|
|
{ |
23
|
|
|
$this->baseHydrator = $baseHydrator; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function hydrate(array $data) |
27
|
|
|
{ |
28
|
|
|
/** @var Track $entity */ |
29
|
|
|
$entity = $this->baseHydrator->hydrate($data); |
30
|
|
|
$entity->setAlbum('Unknown album'); |
|
|
|
|
31
|
|
|
// Here do custom hydration |
32
|
|
|
return $entity; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function extract($entity): array |
36
|
|
|
{ |
37
|
|
|
$data = $this->baseHydrator->extract($entity); |
38
|
|
|
// Here extract additional properties |
39
|
|
|
return $data; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @Storable(source="tracks", hydrator=TrackHydrator::class) |
45
|
|
|
*/ |
46
|
|
|
class Track implements Entity |
47
|
|
|
{ |
48
|
|
|
use AutoGenerateId; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Property(type="id", name="TrackId", class=GenericId::class) |
52
|
|
|
*/ |
53
|
|
|
protected $id; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Property(type="string", name="Name") |
57
|
|
|
*/ |
58
|
|
|
protected $name; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @Property(name="Composer") |
62
|
|
|
*/ |
63
|
|
|
protected $composer; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Property(type="int", name="Milliseconds") |
67
|
|
|
*/ |
68
|
|
|
protected $length; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @Property(type="int", name="Bytes") |
72
|
|
|
*/ |
73
|
|
|
protected $size; |
74
|
|
|
|
75
|
|
|
protected $album; |
76
|
|
|
|
77
|
|
|
public function __construct(string $name, string $composer) |
78
|
|
|
{ |
79
|
|
|
$this->name = $name; |
80
|
|
|
$this->composer = $composer; |
81
|
|
|
$this->length = 0; |
82
|
|
|
$this->size = 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getComposer(): string |
86
|
|
|
{ |
87
|
|
|
return $this->composer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getName(): string |
91
|
|
|
{ |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getLength(): int |
96
|
|
|
{ |
97
|
|
|
return $this->length; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getSize(): int |
101
|
|
|
{ |
102
|
|
|
return $this->size; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setAlbum(string $album) |
106
|
|
|
{ |
107
|
|
|
$this->album = $album; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getAlbum() |
111
|
|
|
{ |
112
|
|
|
return $this->album; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$sqlLiteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions('sqlite')); |
117
|
|
|
$sqlLiteConnection->open(); |
118
|
|
|
$unitOfWork = new Storage(); |
119
|
|
|
$unitOfWork->addRepository(new class($sqlLiteConnection, $unitOfWork->getEntityManager()) extends Repository { |
120
|
|
|
public function getEntityClass(): string |
121
|
|
|
{ |
122
|
|
|
return Track::class; |
123
|
|
|
} |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
$track = $unitOfWork->get(Track::class, 1); |
127
|
|
|
|
128
|
|
|
$track->getAlbum();// Unknown album. |
|
|
|
|
129
|
|
|
|
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.