|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\IdGenerators; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Exception\PrimeException; |
|
7
|
|
|
use Bdf\Prime\Mapper\Mapper; |
|
8
|
|
|
use Bdf\Prime\ServiceLocator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* AbstractGenerator |
|
12
|
|
|
* |
|
13
|
|
|
* A basic property generator. Let inheritance set its generation algorithm. |
|
14
|
|
|
* |
|
15
|
|
|
* @template C as ConnectionInterface |
|
16
|
|
|
* @implements GeneratorInterface<C> |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractGenerator implements GeneratorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The associated mapper |
|
22
|
|
|
* |
|
23
|
|
|
* @var Mapper |
|
24
|
|
|
*/ |
|
25
|
|
|
private $mapper; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The active connection |
|
29
|
|
|
* |
|
30
|
|
|
* @var C |
|
31
|
|
|
*/ |
|
32
|
|
|
private $connection; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The last generated id |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $lastGeneratedId; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Le primary attribute n'est effacé que s'il est vide. |
|
43
|
|
|
* La valeur du last insert ID sera injecté dans entity que si |
|
44
|
|
|
* son attribut primary aura été vidé |
|
45
|
|
|
* |
|
46
|
|
|
* @var bool |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
protected $hasBeenErased = true; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Mapper $mapper |
|
52
|
|
|
*/ |
|
53
|
94 |
|
public function __construct(Mapper $mapper = null) |
|
54
|
|
|
{ |
|
55
|
|
|
// TODO: reference cyclique |
|
|
|
|
|
|
56
|
94 |
|
$this->mapper = $mapper; |
|
57
|
94 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the mapper |
|
61
|
|
|
* |
|
62
|
|
|
* @return Mapper |
|
63
|
|
|
*/ |
|
64
|
19 |
|
public function mapper() |
|
65
|
|
|
{ |
|
66
|
19 |
|
return $this->mapper; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get connection |
|
71
|
|
|
* |
|
72
|
|
|
* @return C |
|
73
|
|
|
*/ |
|
74
|
98 |
|
public function connection() |
|
75
|
|
|
{ |
|
76
|
98 |
|
return $this->connection; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
|
|
|
|
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
426 |
|
public function setCurrentConnection(ConnectionInterface $connection) |
|
83
|
|
|
{ |
|
84
|
426 |
|
$this->connection = $connection; |
|
|
|
|
|
|
85
|
426 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
|
|
|
|
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
363 |
|
public function generate(array &$data, ServiceLocator $serviceLocator) |
|
91
|
|
|
{ |
|
92
|
363 |
|
$this->hasBeenErased = false; |
|
93
|
|
|
|
|
94
|
363 |
|
$property = $this->getPropertyToHydrate(); |
|
95
|
|
|
|
|
96
|
363 |
|
if (empty($data[$property])) { |
|
97
|
100 |
|
$this->lastGeneratedId = $this->doGenerate($property, $data, $serviceLocator); |
|
|
|
|
|
|
98
|
100 |
|
$this->hasBeenErased = true; |
|
99
|
|
|
} |
|
100
|
363 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Do the ID generation |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $property The aimed property |
|
106
|
|
|
* @param array $data By reference |
|
107
|
|
|
* @param ServiceLocator $serviceLocator |
|
108
|
|
|
* |
|
109
|
|
|
* @return string|null Returns the generated id |
|
|
|
|
|
|
110
|
|
|
* @throws PrimeException |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function doGenerate($property, array &$data, ServiceLocator $serviceLocator) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
// to overload |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
|
|
|
|
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
361 |
|
public function postProcess($entity) |
|
121
|
|
|
{ |
|
122
|
361 |
|
if (!$this->hasBeenErased) { |
|
123
|
273 |
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
98 |
|
$propertyName = $this->getPropertyToHydrate(); |
|
127
|
98 |
|
$propertyMetadata = $this->mapper->metadata()->attributes[$propertyName]; |
|
128
|
98 |
|
$value = $this->lastGeneratedId(); |
|
129
|
|
|
|
|
130
|
98 |
|
if (empty($propertyMetadata['phpOptions']['ignore_generator'])) { |
|
131
|
97 |
|
$value = $this->connection->fromDatabase($value, $propertyMetadata['type'], $propertyMetadata['phpOptions'] ?? []); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
98 |
|
$this->mapper->hydrateOne($entity, $propertyName, $value); |
|
135
|
98 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the last generated id |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
20 |
|
protected function lastGeneratedId() |
|
143
|
|
|
{ |
|
144
|
20 |
|
return $this->lastGeneratedId; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the property name to hydrate |
|
149
|
|
|
* |
|
150
|
|
|
* Returns by default the primary property |
|
|
|
|
|
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
363 |
|
protected function getPropertyToHydrate() |
|
155
|
|
|
{ |
|
156
|
363 |
|
return $this->mapper->metadata()->primary['attributes'][0]; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|