1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Application |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Domain\Model\Properties; |
38
|
|
|
|
39
|
|
|
use Apparat\Object\Domain\Factory\RelationFactory; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Relation\RelationInterface; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Object resource relations |
45
|
|
|
* |
46
|
|
|
* @package Apparat\Object |
47
|
|
|
* @subpackage Apparat\Object\Domain |
48
|
|
|
*/ |
49
|
|
|
class Relations extends AbstractProperties |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Collection name |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
const COLLECTION = 'relations'; |
57
|
|
|
/** |
58
|
|
|
* Relations |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $relations = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Relations constructor |
66
|
|
|
* |
67
|
|
|
* @param array $data Property data |
68
|
|
|
* @param ObjectInterface $object Owner object |
69
|
|
|
*/ |
70
|
45 |
|
public function __construct(array $data, ObjectInterface $object) |
71
|
|
|
{ |
72
|
45 |
|
parent::__construct($data, $object); |
73
|
|
|
|
74
|
|
|
// Run through all registered relation type collections |
75
|
|
|
/** |
76
|
|
|
* @var string $relationType |
77
|
|
|
* @var RelationInterface[] $relations |
78
|
|
|
*/ |
79
|
45 |
|
foreach ($this->data as $relationType => $relations) { |
80
|
|
|
// If the relation type collection is invalid or empty |
81
|
38 |
|
if (!is_array($relations) || !count($relations)) { |
82
|
|
|
// TODO Trigger warning |
83
|
2 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Run through all (serialized) relations |
87
|
38 |
|
foreach ($relations as $serializedRelation) { |
88
|
38 |
|
$this->addRelationInstance(RelationFactory::createFromString( |
89
|
|
|
$relationType, |
90
|
|
|
$serializedRelation, |
91
|
38 |
|
$this->object->getRepositoryLocator()->getRepository() |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
45 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add a relation |
99
|
|
|
* |
100
|
|
|
* @param RelationInterface $relation Relation |
101
|
|
|
*/ |
102
|
38 |
|
protected function addRelationInstance(RelationInterface $relation) |
103
|
|
|
{ |
104
|
38 |
|
$this->relations[$relation->getSignature()] = $relation; |
105
|
38 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Unserialize and add a relation |
109
|
|
|
* |
110
|
|
|
* @param string|RelationInterface $relation Serialized or instantiated object relation |
111
|
|
|
* @param string|null $relationType Relation type |
112
|
|
|
* @return Relations Self reference |
113
|
|
|
* @throws InvalidArgumentException If the relation is not a valid relation instance |
114
|
|
|
*/ |
115
|
4 |
|
public function addRelation($relation, $relationType = null) |
116
|
|
|
{ |
117
|
|
|
// If a new relation is to be added |
118
|
4 |
|
$relation = $this->getRelationInstance($relation, $relationType); |
119
|
4 |
|
if (!array_key_exists($relation->getSignature(), $this->relations)) { |
120
|
4 |
|
$relations = clone $this; |
121
|
4 |
|
$relations->addRelationInstance($relation); |
122
|
4 |
|
return $relations; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Else: Return this |
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Instantiate a relation |
131
|
|
|
* |
132
|
|
|
* @param string|RelationInterface $relation Serialized or instantiated object relation |
133
|
|
|
* @param string|null $relationType Relation type |
134
|
|
|
* @return RelationInterface Relation instance |
135
|
|
|
* @throws InvalidArgumentException If the relation is not a valid relation instance |
136
|
|
|
*/ |
137
|
4 |
|
protected function getRelationInstance($relation, $relationType = null) |
138
|
|
|
{ |
139
|
|
|
// Unserialize and instantiate the relation if it's given in serialized form |
140
|
4 |
|
if (is_string($relation)) { |
141
|
|
|
// Validate the relation type |
142
|
4 |
|
RelationFactory::validateRelationType($relationType); |
143
|
|
|
|
144
|
|
|
// Create the relation instance |
145
|
4 |
|
$relation = RelationFactory::createFromString( |
146
|
|
|
$relationType, |
147
|
|
|
$relation, |
148
|
4 |
|
$this->object->getRepositoryLocator()->getRepository() |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// If the relation is not a valid relation instance |
153
|
4 |
|
if (!($relation instanceof RelationInterface)) { |
154
|
1 |
|
throw new InvalidArgumentException( |
155
|
1 |
|
'Invalid object relation', |
156
|
1 |
|
InvalidArgumentException::INVALID_OBJECT_RELATION |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
return $relation; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Delete an object relation |
165
|
|
|
* |
166
|
|
|
* @param RelationInterface $relation Object relation |
167
|
|
|
* @return Relations Self reference |
168
|
|
|
*/ |
169
|
3 |
|
public function deleteRelation(RelationInterface $relation) |
170
|
|
|
{ |
171
|
|
|
// If a new relation is to be added |
172
|
3 |
|
if (array_key_exists($relation->getSignature(), $this->relations)) { |
173
|
3 |
|
$relations = clone $this; |
174
|
3 |
|
unset($relations->relations[$relation->getSignature()]); |
175
|
3 |
|
return $relations; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Else: Return this |
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get all relations (optional: Of a particular type) |
184
|
|
|
* |
185
|
|
|
* @param string|null $relationType Optional: Relation type |
186
|
|
|
* @return RelationInterface[] Relations |
187
|
|
|
*/ |
188
|
3 |
|
public function getRelations($relationType = null) |
189
|
|
|
{ |
190
|
|
|
|
191
|
|
|
// Return all relations in case no particular type was requested |
192
|
3 |
|
if ($relationType === null) { |
193
|
1 |
|
return array_values($this->relations); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Return all relations matching the requested type |
197
|
2 |
|
return $this->findRelations([RelationInterface::FILTER_TYPE => $relationType]); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Find and return particular relations |
202
|
|
|
* |
203
|
|
|
* @param array $criteria Relation criteria |
204
|
|
|
* @return RelationInterface[] Relations |
205
|
|
|
*/ |
206
|
10 |
|
public function findRelations(array $criteria) |
207
|
|
|
{ |
208
|
|
|
// Validate the relation type (if given as a criteria) |
209
|
10 |
|
if (array_key_exists(RelationInterface::FILTER_TYPE, $criteria)) { |
210
|
9 |
|
RelationFactory::validateRelationType($criteria[RelationInterface::FILTER_TYPE]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Find and return the relations matching the criteria |
214
|
10 |
|
return array_values(array_filter($this->relations, function (RelationInterface $relation) use ($criteria) { |
215
|
6 |
|
foreach ($criteria as $property => $value) { |
216
|
|
|
switch ($property) { |
217
|
6 |
|
case RelationInterface::FILTER_TYPE: |
218
|
5 |
|
if ($relation->getRelationType() != $value) { |
219
|
3 |
|
return false; |
220
|
|
|
} |
221
|
5 |
|
break; |
222
|
3 |
|
case RelationInterface::FILTER_URL: |
223
|
|
|
// print_r($relation->getUrl()); |
224
|
3 |
|
if (strpos($relation->getUrl(), $value) === false) { |
225
|
3 |
|
return false; |
226
|
|
|
} |
227
|
3 |
|
break; |
228
|
2 |
|
case RelationInterface::FILTER_LABEL: |
229
|
1 |
|
if (strpos($relation->getLabel(), $value) === false) { |
230
|
1 |
|
return false; |
231
|
|
|
} |
232
|
1 |
|
break; |
233
|
2 |
|
case RelationInterface::FILTER_EMAIL: |
234
|
2 |
|
if (strpos($relation->getEmail(), $value) === false) { |
235
|
2 |
|
return false; |
236
|
|
|
} |
237
|
2 |
|
break; |
238
|
1 |
|
case RelationInterface::FILTER_COUPLING: |
239
|
1 |
|
if ($relation->getCoupling() !== intval(!!$value)) { |
240
|
1 |
|
return false; |
241
|
|
|
} |
242
|
1 |
|
break; |
243
|
|
|
default: |
244
|
6 |
|
return false; |
245
|
|
|
} |
246
|
|
|
} |
247
|
6 |
|
return true; |
248
|
10 |
|
})); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return the property values as array |
253
|
|
|
* |
254
|
|
|
* @param bool $serialize Serialize property objects |
255
|
|
|
* @return array Property values |
256
|
|
|
*/ |
257
|
10 |
|
public function toArray($serialize = true) |
258
|
|
|
{ |
259
|
10 |
|
$relations = []; |
260
|
|
|
/** @var RelationInterface $relation */ |
261
|
10 |
|
foreach ($this->relations as $relation) { |
262
|
4 |
|
$relationType = $relation->getRelationType(); |
263
|
4 |
|
if (!array_key_exists($relationType, $relations)) { |
264
|
4 |
|
$relations[$relationType] = []; |
265
|
|
|
} |
266
|
4 |
|
$relations[$relationType][] = $serialize ? strval($relation) : $relation; |
267
|
|
|
} |
268
|
10 |
|
ksort($relations); |
269
|
10 |
|
return $this->toSerializedArray($serialize, $relations); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|