1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\ORM\Relation; |
||
6 | |||
7 | use Cycle\ORM\Command\StoreCommandInterface; |
||
8 | use Cycle\ORM\Exception\Relation\NullException; |
||
9 | use Cycle\ORM\Heap\Node; |
||
10 | use Cycle\ORM\Heap\State; |
||
11 | use Cycle\ORM\MapperInterface; |
||
12 | use Cycle\ORM\ORMInterface; |
||
13 | use Cycle\ORM\Reference\EmptyReference; |
||
14 | use Cycle\ORM\Reference\Reference; |
||
15 | use Cycle\ORM\Reference\ReferenceInterface; |
||
16 | use Cycle\ORM\SchemaInterface; |
||
17 | use Cycle\ORM\Service\EntityFactoryInterface; |
||
18 | use Cycle\ORM\Service\EntityProviderInterface; |
||
19 | use Cycle\ORM\Service\MapperProviderInterface; |
||
20 | use Cycle\ORM\Transaction\Pool; |
||
21 | use Cycle\ORM\Transaction\Tuple; |
||
22 | |||
23 | /** |
||
24 | * Embeds one object to another. |
||
25 | * |
||
26 | * @internal |
||
27 | */ |
||
28 | final class Embedded implements SameRowRelationInterface |
||
29 | { |
||
30 | private MapperInterface $mapper; |
||
31 | private MapperProviderInterface $mapperProvider; |
||
32 | private EntityProviderInterface $entityProvider; |
||
33 | |||
34 | /** @var string[] */ |
||
35 | private array $primaryKeys; |
||
36 | |||
37 | private array $columns; |
||
38 | |||
39 | 344 | public function __construct( |
|
40 | /** @internal */ |
||
41 | ORMInterface $orm, |
||
42 | private string $name, |
||
43 | private string $target, |
||
44 | ) { |
||
45 | 344 | $this->mapperProvider = $orm->getService(MapperProviderInterface::class); |
|
46 | 344 | $this->entityProvider = $orm->getService(EntityProviderInterface::class); |
|
47 | 344 | $this->mapper = $this->mapperProvider->getMapper($target); |
|
48 | |||
49 | // this relation must manage column association manually, bypassing related mapper |
||
50 | 344 | $this->primaryKeys = (array) $orm->getSchema()->define($target, SchemaInterface::PRIMARY_KEY); |
|
51 | 344 | $this->columns = $orm->getSchema()->define($target, SchemaInterface::COLUMNS); |
|
52 | } |
||
53 | |||
54 | 216 | public function getName(): string |
|
55 | { |
||
56 | 216 | return $this->name; |
|
57 | } |
||
58 | |||
59 | public function getInnerKeys(): array |
||
60 | { |
||
61 | return $this->primaryKeys; |
||
62 | } |
||
63 | |||
64 | public function getTarget(): string |
||
65 | { |
||
66 | return $this->target; |
||
67 | } |
||
68 | |||
69 | public function isCascade(): bool |
||
70 | { |
||
71 | // always cascade |
||
72 | return true; |
||
73 | } |
||
74 | |||
75 | 168 | public function init(EntityFactoryInterface $factory, Node $node, array $data): object |
|
76 | { |
||
77 | 168 | foreach ($this->primaryKeys as $key) { |
|
78 | // ensure proper object reference |
||
79 | 168 | $data[$key] = $node->getData()[$key]; |
|
80 | } |
||
81 | |||
82 | 168 | $item = $factory->make($this->target, $data, Node::MANAGED); |
|
83 | 168 | $node->setRelation($this->getName(), $item); |
|
84 | |||
85 | 168 | return $item; |
|
86 | } |
||
87 | |||
88 | 264 | public function cast(?array $data): ?array |
|
89 | { |
||
90 | 264 | return $data === null |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
91 | ? null |
||
92 | 264 | : $this->mapperProvider->getMapper($this->target)->cast($data); |
|
93 | } |
||
94 | |||
95 | 32 | public function collect(mixed $data): ?object |
|
96 | { |
||
97 | \assert($data === null || \is_object($data)); |
||
98 | 32 | return $data; |
|
99 | } |
||
100 | |||
101 | 120 | public function initReference(Node $node): ReferenceInterface |
|
102 | { |
||
103 | 120 | $scope = $this->getReferenceScope($node); |
|
104 | 120 | if ($scope === null) { |
|
105 | $result = new Reference($this->target, []); |
||
106 | $result->setValue(null); |
||
107 | return $result; |
||
108 | } |
||
109 | 120 | return $scope === [] ? new EmptyReference($this->target, null) : new Reference($this->target, $scope); |
|
110 | } |
||
111 | |||
112 | 120 | public function prepare(Pool $pool, Tuple $tuple, mixed $related, bool $load = true): void |
|
113 | { |
||
114 | 120 | // $related = $tuple->state->getRelation($this->getName()); |
|
115 | 120 | // $pool->attach($related, Tuple::TASK_STORE, false); |
|
116 | 120 | } |
|
117 | 120 | ||
118 | 120 | public function queue(Pool $pool, Tuple $tuple, ?StoreCommandInterface $command = null): void |
|
119 | { |
||
120 | if ($tuple->task !== Tuple::TASK_STORE) { |
||
121 | 120 | return; |
|
122 | } |
||
123 | 120 | ||
124 | if (!$tuple->state->hasRelation($this->getName())) { |
||
125 | $tuple->state->setRelationStatus($this->getName(), RelationInterface::STATUS_RESOLVED); |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | $related = $tuple->state->getRelation($this->getName()); |
||
130 | |||
131 | // Master Node |
||
132 | 168 | $original = $tuple->node->getRelation($this->getName()); |
|
133 | |||
134 | 168 | if ($related instanceof ReferenceInterface) { |
|
135 | if ($related === $original) { |
||
136 | if (!$related->hasValue() || $this->resolve($related, false) === null) { |
||
137 | 168 | // do not update non resolved and non changed promises |
|
138 | return; |
||
139 | } |
||
140 | 168 | $related = $related->getValue(); |
|
141 | } else { |
||
142 | 168 | // do not affect parent embeddings |
|
143 | 56 | $related = clone $this->resolve($related, true); |
|
144 | 32 | } |
|
145 | } |
||
146 | 32 | ||
147 | if ($related === null) { |
||
148 | throw new NullException("Embedded relation `{$this->name}` can't be null."); |
||
149 | } |
||
150 | $tuple->state->setRelation($this->getName(), $related); |
||
151 | 24 | ||
152 | $rTuple = $pool->attach($related, Tuple::TASK_STORE, false); |
||
153 | // calculate embedded node changes |
||
154 | $changes = $this->getChanges($related, $rTuple->state); |
||
155 | 136 | foreach ($this->primaryKeys as $key) { |
|
156 | 16 | if (isset($changes[$key])) { |
|
157 | $rTuple->state->register($key, $changes[$key]); |
||
158 | 120 | } |
|
159 | } |
||
160 | 120 | ||
161 | if ($command !== null) { |
||
162 | 120 | $mapper = $this->mapperProvider->getMapper($this->target); |
|
163 | 120 | $changes = $mapper->uncast($changes); |
|
164 | 120 | foreach ($mapper->mapColumns($changes) as $field => $value) { |
|
165 | $command->registerColumn($field, $value); |
||
166 | } |
||
167 | } |
||
168 | $rTuple->state->setStatus(Node::MANAGED); |
||
169 | 120 | $rTuple->state->updateTransactionData(); |
|
170 | 120 | ||
171 | 120 | $tuple->state->setRelationStatus($this->getName(), RelationInterface::STATUS_RESOLVED); |
|
172 | 120 | $tuple->state->setRelationStatus($rTuple->node->getRole() . ':' . $this->getName(), RelationInterface::STATUS_RESOLVED); |
|
173 | 104 | } |
|
174 | |||
175 | /** |
||
176 | 120 | * Resolve the reference to the object. |
|
177 | 120 | */ |
|
178 | public function resolve(ReferenceInterface $reference, bool $load): ?object |
||
179 | 120 | { |
|
180 | 120 | if ($reference->hasValue()) { |
|
181 | return $reference->getValue(); |
||
182 | } |
||
183 | 120 | ||
184 | $result = $this->entityProvider->get($reference->getRole(), $reference->getScope(), $load); |
||
185 | 120 | if ($load === true || $result !== null) { |
|
186 | $reference->setValue($result); |
||
187 | 120 | } |
|
188 | 120 | return $result; |
|
189 | } |
||
190 | |||
191 | 120 | private function getReferenceScope(Node $node): ?array |
|
192 | { |
||
193 | $scope = []; |
||
194 | $nodeData = $node->getData(); |
||
195 | foreach ($this->primaryKeys as $key) { |
||
196 | $value = $nodeData[$key] ?? null; |
||
197 | 56 | if (empty($value)) { |
|
198 | return null; |
||
199 | 56 | } |
|
200 | 8 | $scope[$key] = $value; |
|
201 | } |
||
202 | return $scope; |
||
203 | 48 | } |
|
204 | 48 | ||
205 | 48 | private function getChanges(object $related, State $state): array |
|
206 | { |
||
207 | 48 | $data = \array_intersect_key($this->mapper->extract($related), $this->columns); |
|
208 | // Embedded entity does not override PK values of the parent |
||
209 | foreach ($this->primaryKeys as $key) { |
||
210 | unset($data[$key]); |
||
211 | } |
||
212 | |||
213 | return \array_udiff_assoc($data, $state->getTransactionData(), [Node::class, 'compare']); |
||
214 | } |
||
215 | } |
||
216 |