1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
3
|
|
|
|
4
|
|
|
use Igni\Storage\Driver\Pdo\Connection; |
5
|
|
|
use Igni\Storage\Driver\Pdo\ConnectionOptions; |
6
|
|
|
use Igni\Storage\Driver\Pdo\Repository; |
7
|
|
|
use Igni\Storage\Storable; |
8
|
|
|
use Igni\Storage\Id\AutoGenerateId; |
9
|
|
|
use Igni\Storage\Id\GenericId; |
10
|
|
|
use Igni\Storage\Mapping\Annotation\Entity; |
11
|
|
|
use Igni\Storage\Mapping\Annotation\Property; |
12
|
|
|
use Igni\Storage\Mapping\MappingStrategy; |
13
|
|
|
use Igni\Storage\Mapping\Type; |
14
|
|
|
use Igni\Storage\Storage; |
15
|
|
|
use Igni\Storage\Driver\ConnectionManager; |
16
|
|
|
|
17
|
|
|
class ComposerMapping implements MappingStrategy |
18
|
|
|
{ |
19
|
|
|
public static function hydrate(&$value) |
20
|
|
|
{ |
21
|
|
|
$value = new Composer($value); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function extract(&$value) |
25
|
|
|
{ |
26
|
|
|
$value = (string) $value; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
Type::register('composer', ComposerMapping::class); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class Composer |
34
|
|
|
{ |
35
|
|
|
private $name; |
36
|
|
|
|
37
|
|
|
public function __construct(string $name) |
38
|
|
|
{ |
39
|
|
|
$this->name = $name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function __toString(): string |
43
|
|
|
{ |
44
|
|
|
return $this->name; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Entity(source="tracks") |
50
|
|
|
*/ |
51
|
|
|
class Track implements Storable |
52
|
|
|
{ |
53
|
|
|
use AutoGenerateId; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Property(type="id", name="TrackId", class=GenericId::class) |
57
|
|
|
*/ |
58
|
|
|
protected $id; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @Property(type="string", name="Name") |
62
|
|
|
*/ |
63
|
|
|
protected $name; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Property(name="Composer", type="composer") |
67
|
|
|
*/ |
68
|
|
|
protected $composer; |
69
|
|
|
|
70
|
|
|
public function __construct(string $name, Composer $composer) |
71
|
|
|
{ |
72
|
|
|
$this->name = $name; |
73
|
|
|
$this->composer = $composer; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getComposer(): Composer |
77
|
|
|
{ |
78
|
|
|
return $this->composer; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getName(): string |
82
|
|
|
{ |
83
|
|
|
return $this->name; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
ConnectionManager::registerDefault(new Connection('sqlite:/' . __DIR__ . '/db.db')); |
88
|
|
|
$unitOfWork = new Storage(); |
89
|
|
|
$unitOfWork->addRepository(new class($unitOfWork->getEntityManager()) extends Repository { |
90
|
|
|
public static function getEntityClass(): string |
91
|
|
|
{ |
92
|
|
|
return Track::class; |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$track = $unitOfWork->get(Track::class, 1); |
97
|
|
|
|
98
|
|
|
print_r($track->getComposer());// Instance of composer. |
|
|
|
|
99
|
|
|
|