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
|
315 |
|
public function __construct(Mapper $mapper = null) |
54
|
|
|
{ |
55
|
|
|
// TODO: reference cyclique |
56
|
315 |
|
$this->mapper = $mapper; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the mapper |
61
|
|
|
* |
62
|
|
|
* @return Mapper |
63
|
|
|
*/ |
64
|
21 |
|
public function mapper() |
65
|
|
|
{ |
66
|
21 |
|
return $this->mapper; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get connection |
71
|
|
|
* |
72
|
|
|
* @return C |
73
|
|
|
*/ |
74
|
106 |
|
public function connection() |
75
|
|
|
{ |
76
|
106 |
|
return $this->connection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
648 |
|
public function setCurrentConnection(ConnectionInterface $connection): void |
83
|
|
|
{ |
84
|
648 |
|
$this->connection = $connection; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
585 |
|
public function generate(array &$data, ServiceLocator $serviceLocator): void |
91
|
|
|
{ |
92
|
585 |
|
$this->hasBeenErased = false; |
93
|
|
|
|
94
|
585 |
|
$property = $this->getPropertyToHydrate(); |
95
|
|
|
|
96
|
585 |
|
if (empty($data[$property])) { |
97
|
109 |
|
$this->lastGeneratedId = $this->doGenerate($property, $data, $serviceLocator); |
|
|
|
|
98
|
109 |
|
$this->hasBeenErased = true; |
99
|
|
|
} |
100
|
|
|
} |
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
|
583 |
|
public function postProcess($entity): void |
121
|
|
|
{ |
122
|
583 |
|
if (!$this->hasBeenErased) { |
123
|
492 |
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
107 |
|
$propertyName = $this->getPropertyToHydrate(); |
127
|
107 |
|
$propertyMetadata = $this->mapper->metadata()->attributes[$propertyName]; |
128
|
107 |
|
$value = $this->lastGeneratedId(); |
129
|
|
|
|
130
|
107 |
|
if (empty($propertyMetadata['phpOptions']['ignore_generator'])) { |
131
|
106 |
|
$value = $this->connection->fromDatabase($value, $propertyMetadata['type'], $propertyMetadata['phpOptions'] ?? []); |
132
|
|
|
} |
133
|
|
|
|
134
|
107 |
|
$this->mapper->hydrateOne($entity, $propertyName, $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the last generated id |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
22 |
|
protected function lastGeneratedId() |
143
|
|
|
{ |
144
|
22 |
|
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
|
585 |
|
protected function getPropertyToHydrate() |
155
|
|
|
{ |
156
|
585 |
|
return $this->mapper->metadata()->primary['attributes'][0]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..