1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Cycle\ORM\Relation; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\Command\Branch\Condition; |
13
|
|
|
use Cycle\ORM\Command\Branch\Sequence; |
14
|
|
|
use Cycle\ORM\Command\CommandInterface; |
15
|
|
|
use Cycle\ORM\Command\ContextCarrierInterface as CC; |
16
|
|
|
use Cycle\ORM\Heap\Node; |
17
|
|
|
use Cycle\ORM\Promise\Collection\CollectionPromise; |
18
|
|
|
use Cycle\ORM\Promise\PromiseInterface; |
19
|
|
|
use Cycle\ORM\Promise\PromiseMany; |
20
|
|
|
use Cycle\ORM\Promise\ReferenceInterface; |
21
|
|
|
use Cycle\ORM\Relation; |
22
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
23
|
|
|
use Doctrine\Common\Collections\Collection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Provides the ability to own the collection of entities. |
27
|
|
|
*/ |
28
|
|
|
class HasMany extends AbstractRelation |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Init relation state and entity collection. |
32
|
|
|
* |
33
|
|
|
* @param array $data |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function init(array $data): array |
37
|
|
|
{ |
38
|
|
|
$result = []; |
39
|
|
|
foreach ($data as $item) { |
40
|
|
|
$result[] = $this->orm->make($this->target, $item, Node::MANAGED); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return [new ArrayCollection($result), $result]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Convert entity data into array. |
48
|
|
|
* |
49
|
|
|
* @param mixed $data |
50
|
|
|
* @return array|PromiseInterface |
51
|
|
|
*/ |
52
|
|
|
public function extract($data) |
53
|
|
|
{ |
54
|
|
|
if ($data instanceof CollectionPromise && !$data->isInitialized()) { |
55
|
|
|
return $data->getPromise(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($data instanceof Collection) { |
59
|
|
|
return $data->toArray(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return is_array($data) ? $data : []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function initPromise(Node $parentNode): array |
69
|
|
|
{ |
70
|
|
|
if (empty($innerKey = $this->fetchKey($parentNode, $this->innerKey))) { |
71
|
|
|
return [new ArrayCollection(), null]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$p = new PromiseMany( |
75
|
|
|
$this->orm, |
76
|
|
|
$this->target, |
77
|
|
|
[ |
78
|
|
|
$this->outerKey => $innerKey |
79
|
|
|
], |
80
|
|
|
$this->schema[Relation::WHERE] ?? [] |
81
|
|
|
); |
82
|
|
|
$p->setConstrain($this->getConstrain()); |
83
|
|
|
|
84
|
|
|
return [new CollectionPromise($p), $p]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function queue(CC $parentStore, $parentEntity, Node $parentNode, $related, $original): CommandInterface |
91
|
|
|
{ |
92
|
|
|
if ($related instanceof ReferenceInterface) { |
93
|
|
|
$related = $this->resolve($related); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($original instanceof ReferenceInterface) { |
97
|
|
|
$original = $this->resolve($original); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$sequence = new Sequence(); |
101
|
|
|
|
102
|
|
|
foreach ($related as $item) { |
103
|
|
|
$sequence->addCommand($this->queueStore($parentNode, $item)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($this->calcDeleted($related, $original ?? []) as $item) { |
107
|
|
|
$sequence->addCommand($this->queueDelete($item)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $sequence; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return objects which are subject of removal. |
115
|
|
|
* |
116
|
|
|
* @param array $related |
117
|
|
|
* @param array $original |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function calcDeleted(array $related, array $original) |
121
|
|
|
{ |
122
|
|
|
return array_udiff($original ?? [], $related, function ($a, $b) { |
123
|
|
|
return strcmp(spl_object_hash($a), spl_object_hash($b)); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Persist related object. |
129
|
|
|
* |
130
|
|
|
* @param Node $parentNode |
131
|
|
|
* @param object $related |
132
|
|
|
* @return CC |
133
|
|
|
*/ |
134
|
|
|
protected function queueStore(Node $parentNode, $related): CC |
135
|
|
|
{ |
136
|
|
|
$relStore = $this->orm->queueStore($related); |
|
|
|
|
137
|
|
|
$relNode = $this->getNode($related, +1); |
138
|
|
|
$this->assertValid($relNode); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->forwardContext($parentNode, $this->innerKey, $relStore, $relNode, $this->outerKey); |
141
|
|
|
|
142
|
|
|
return $relStore; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove one of related objects. |
147
|
|
|
* |
148
|
|
|
* @param object $related |
149
|
|
|
* @return CommandInterface |
150
|
|
|
*/ |
151
|
|
|
protected function queueDelete($related): CommandInterface |
152
|
|
|
{ |
153
|
|
|
$relNode = $this->getNode($related); |
154
|
|
|
|
155
|
|
|
return new Condition($this->orm->queueDelete($related), function () use ($relNode) { |
|
|
|
|
156
|
|
|
return !$relNode->getState()->hasClaims(); |
157
|
|
|
}); |
158
|
|
|
} |
159
|
|
|
} |