|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
9
|
|
|
* @since 0.28 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Core\Entity\Collection; |
|
13
|
|
|
|
|
14
|
|
|
use Core\Repository\RepositoryService; |
|
15
|
|
|
use Core\Entity\AttachedEntityReference; |
|
16
|
|
|
use Core\Entity\IdentifiableEntityInterface; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
|
|
|
|
|
19
|
|
|
use Closure; |
|
20
|
|
|
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; |
|
21
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
22
|
|
|
|
|
23
|
|
|
class AttachedEntitiesCollection extends ArrayCollection |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RepositoryService |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $repositories; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AttachedEntityReference |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $referencePrototype; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param RepositoryService $repositories |
|
38
|
|
|
* @param AttachedEntityReference $referencePrototype |
|
|
|
|
|
|
39
|
|
|
* @param array $elements |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(RepositoryService $repositories, AttachedEntityReference $referencePrototype = null, array $elements = []) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct($elements); |
|
44
|
|
|
$this->repositories = $repositories; |
|
45
|
|
|
$this->referencePrototype = $referencePrototype ?: new AttachedEntityReference(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
* @see \Doctrine\Common\Collections\ArrayCollection::set() |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setAttachedEntity($key, $entity) |
|
53
|
|
|
{ |
|
54
|
|
|
if (! $entity instanceof IdentifiableEntityInterface) { |
|
55
|
|
|
throw new InvalidArgumentException(sprintf('$entity must be instance of %s', IdentifiableEntityInterface::class)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$className = get_class($entity); |
|
59
|
|
|
|
|
60
|
|
|
if (null === $key) { |
|
61
|
|
|
$key = $className; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$reference = clone $this->referencePrototype; |
|
65
|
|
|
$entityId = $entity->getId(); |
|
66
|
|
|
|
|
67
|
|
|
// check if entity is not persisted |
|
68
|
|
|
if (! $entityId) { |
|
69
|
|
|
// persist entity & retrieve its ID |
|
70
|
|
|
$this->repositories->getRepository($className) |
|
|
|
|
|
|
71
|
|
|
->store($entity); |
|
72
|
|
|
$entityId = $entity->getId(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$reference->setRepository($className) |
|
76
|
|
|
->setEntityId($entityId); |
|
77
|
|
|
|
|
78
|
|
|
return parent::set($key, $reference); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
* @see \Doctrine\Common\Collections\ArrayCollection::get() |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getAttachedEntity($key) |
|
86
|
|
|
{ |
|
87
|
|
|
$reference = parent::get($key); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if (! $reference) { |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$entity = $this->doGetEntity($reference); |
|
94
|
|
|
|
|
95
|
|
|
if (! $entity) { |
|
96
|
|
|
// remove reference if entity does not exists |
|
97
|
|
|
unset($this->elements[$key]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $entity; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
* @see \Doctrine\Common\Collections\ArrayCollection::remove() |
|
106
|
|
|
*/ |
|
107
|
|
|
public function removeAttachedEntity($key) |
|
108
|
|
|
{ |
|
109
|
|
|
$reference = parent::remove($key); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
if (! $reference) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$entity = $this->doGetEntity($reference); |
|
116
|
|
|
|
|
117
|
|
|
if ($entity) { |
|
118
|
|
|
$this->repositories->getRepository($reference->getRepository()) |
|
|
|
|
|
|
119
|
|
|
->remove($entity); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $reference; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param AttachedEntityReference $reference |
|
127
|
|
|
* @return object|NULL |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function doGetEntity(AttachedEntityReference $reference) |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->repositories->getRepository($reference->getRepository()) |
|
|
|
|
|
|
132
|
|
|
->find($reference->getEntityId()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritDoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function map(Closure $func) |
|
139
|
|
|
{ |
|
140
|
|
|
return new static($this->repositories, $this->referencePrototype, array_map($func, $this->toArray())); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritDoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function filter(Closure $p) |
|
147
|
|
|
{ |
|
148
|
|
|
return new static($this->repositories, $this->referencePrototype, array_filter($this->toArray(), $p)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritDoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function partition(Closure $p) |
|
155
|
|
|
{ |
|
156
|
|
|
$matches = $noMatches = array(); |
|
157
|
|
|
|
|
158
|
|
|
foreach ($this->toArray() as $key => $element) { |
|
159
|
|
|
if ($p($key, $element)) { |
|
160
|
|
|
$matches[$key] = $element; |
|
161
|
|
|
} else { |
|
162
|
|
|
$noMatches[$key] = $element; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return array(new static($this->repositories, $this->referencePrototype, $matches), |
|
167
|
|
|
new static($this->repositories, $this->referencePrototype, $noMatches)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritDoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function matching(Criteria $criteria) |
|
174
|
|
|
{ |
|
175
|
|
|
$expr = $criteria->getWhereExpression(); |
|
176
|
|
|
$filtered = $this->toArray(); |
|
177
|
|
|
|
|
178
|
|
|
if ($expr) { |
|
179
|
|
|
$visitor = new ClosureExpressionVisitor(); |
|
180
|
|
|
$filter = $visitor->dispatch($expr); |
|
181
|
|
|
$filtered = array_filter($filtered, $filter); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if ($orderings = $criteria->getOrderings()) { |
|
185
|
|
|
foreach (array_reverse($orderings) as $field => $ordering) { |
|
186
|
|
|
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
uasort($filtered, $next); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$offset = $criteria->getFirstResult(); |
|
193
|
|
|
$length = $criteria->getMaxResults(); |
|
194
|
|
|
|
|
195
|
|
|
if ($offset || $length) { |
|
|
|
|
|
|
196
|
|
|
$filtered = array_slice($filtered, (int)$offset, $length); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return new static($this->repositories, $this->referencePrototype, $filtered); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: