|
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\Application |
|
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
|
|
|
/** |
|
66
|
|
|
* Relations constructor |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $data Property data |
|
69
|
|
|
* @param ObjectInterface $object Owner object |
|
70
|
|
|
*/ |
|
71
|
20 |
|
public function __construct(array $data, ObjectInterface $object) |
|
72
|
|
|
{ |
|
73
|
20 |
|
parent::__construct($data, $object); |
|
74
|
|
|
|
|
75
|
|
|
// Run through all registered relation type collections |
|
76
|
|
|
/** |
|
77
|
|
|
* @var string $relationType |
|
78
|
|
|
* @var RelationInterface[] $relations |
|
79
|
|
|
*/ |
|
80
|
20 |
|
foreach ($this->data as $relationType => $relations) { |
|
81
|
|
|
// If the relation type collection is invalid or empty |
|
82
|
19 |
|
if (!is_array($relations) || !count($relations)) { |
|
83
|
|
|
// TODO Trigger warning |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Run through all (serialized) relations |
|
88
|
19 |
|
foreach ($relations as $serializedRelation) { |
|
89
|
19 |
|
$this->addRelationInstance(RelationFactory::createFromString( |
|
90
|
|
|
$relationType, |
|
91
|
|
|
$serializedRelation, |
|
92
|
19 |
|
$this->object->getRepositoryPath()->getRepository() |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
20 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add a relation |
|
100
|
|
|
* |
|
101
|
|
|
* @param RelationInterface $relation Relation |
|
102
|
|
|
*/ |
|
103
|
19 |
|
protected function addRelationInstance(RelationInterface $relation) |
|
104
|
|
|
{ |
|
105
|
19 |
|
$this->relations[$relation->getSignature()] = $relation; |
|
106
|
19 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Unserialize and add a relation |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|RelationInterface $relation Serialized or instantiated object relation |
|
112
|
|
|
* @param string|null $relationType Relation type |
|
113
|
|
|
* @return Relations Self reference |
|
114
|
|
|
* @throws InvalidArgumentException If the relation is not a valid relation instance |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function addRelation($relation, $relationType = null) |
|
117
|
|
|
{ |
|
118
|
|
|
// If a new relation is to be added |
|
119
|
1 |
|
$relation = $this->getRelationInstance($relation, $relationType); |
|
120
|
1 |
|
if (!array_key_exists($relation->getSignature(), $this->relations)) { |
|
121
|
1 |
|
$relations = clone $this; |
|
122
|
1 |
|
$relations->addRelationInstance($relation); |
|
123
|
1 |
|
return $relations; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Else: Return this |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Instantiate a relation |
|
132
|
|
|
* |
|
133
|
|
|
* @param string|RelationInterface $relation Serialized or instantiated object relation |
|
134
|
|
|
* @param string|null $relationType Relation type |
|
135
|
|
|
* @return RelationInterface Relation instance |
|
136
|
|
|
* @throws InvalidArgumentException If the relation is not a valid relation instance |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function getRelationInstance($relation, $relationType = null) |
|
139
|
|
|
{ |
|
140
|
|
|
// Unserialize and instantiate the relation if it's given in serialized form |
|
141
|
1 |
|
if (is_string($relation)) { |
|
142
|
|
|
// Validate the relation type |
|
143
|
1 |
|
RelationFactory::validateRelationType($relationType); |
|
144
|
|
|
|
|
145
|
|
|
// Create the relation instance |
|
146
|
1 |
|
$relation = RelationFactory::createFromString( |
|
147
|
|
|
$relationType, |
|
148
|
|
|
$relation, |
|
149
|
1 |
|
$this->object->getRepositoryPath()->getRepository() |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// If the relation is not a valid relation instance |
|
154
|
1 |
|
if (!($relation instanceof RelationInterface)) { |
|
155
|
|
|
throw new InvalidArgumentException( |
|
156
|
|
|
'Invalid object relation', |
|
157
|
|
|
InvalidArgumentException::INVALID_OBJECT_RELATION |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return $relation; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Delete an object relation |
|
166
|
|
|
* |
|
167
|
|
|
* @param RelationInterface $relation Object relation |
|
168
|
|
|
* @return Relations Self reference |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function deleteRelation(RelationInterface $relation) |
|
171
|
|
|
{ |
|
172
|
|
|
// If a new relation is to be added |
|
173
|
1 |
|
if (array_key_exists($relation->getSignature(), $this->relations)) { |
|
174
|
1 |
|
$relations = clone $this; |
|
175
|
1 |
|
unset($relations->relations[$relation->getSignature()]); |
|
176
|
1 |
|
return $relations; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// Else: Return this |
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get all relations (optional: Of a particular type) |
|
185
|
|
|
* |
|
186
|
|
|
* @param string|null $relationType Optional: Relation type |
|
187
|
|
|
* @return RelationInterface[] Relations |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function getRelations($relationType = null) |
|
190
|
|
|
{ |
|
191
|
|
|
|
|
192
|
|
|
// Return all relations in case no particular type was requested |
|
193
|
1 |
|
if ($relationType === null) { |
|
194
|
1 |
|
return array_values($this->relations); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Return all relations matching the requested type |
|
198
|
|
|
return $this->findRelations([RelationInterface::FILTER_TYPE => $relationType]); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Find and return particular relations |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $criteria Relation criteria |
|
205
|
|
|
* @return RelationInterface[] Relations |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function findRelations(array $criteria) |
|
208
|
|
|
{ |
|
209
|
|
|
// Validate the relation type (if given as a criteria) |
|
210
|
1 |
|
if (array_key_exists(RelationInterface::FILTER_TYPE, $criteria)) { |
|
211
|
|
|
RelationFactory::validateRelationType($criteria[RelationInterface::FILTER_TYPE]); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// Find and return the relations matching the criteria |
|
215
|
1 |
|
return array_values(array_filter($this->relations, function (RelationInterface $relation) use ($criteria) { |
|
216
|
1 |
|
foreach ($criteria as $property => $value) { |
|
217
|
|
|
switch ($property) { |
|
218
|
1 |
|
case RelationInterface::FILTER_TYPE: |
|
219
|
|
|
if ($relation->getType() != $value) { |
|
220
|
|
|
return false; |
|
221
|
|
|
} |
|
222
|
|
|
break; |
|
223
|
1 |
|
case RelationInterface::FILTER_URL: |
|
224
|
1 |
|
if (strpos($relation->getUrl(), $value) === false) { |
|
225
|
1 |
|
return false; |
|
226
|
|
|
} |
|
227
|
1 |
|
break; |
|
228
|
1 |
|
case RelationInterface::FILTER_LABEL: |
|
229
|
|
|
if (strpos($relation->getLabel(), $value) === false) { |
|
230
|
|
|
return false; |
|
231
|
|
|
} |
|
232
|
|
|
break; |
|
233
|
1 |
|
case RelationInterface::FILTER_EMAIL: |
|
234
|
1 |
|
if (strpos($relation->getEmail(), $value) === false) { |
|
235
|
1 |
|
return false; |
|
236
|
|
|
} |
|
237
|
1 |
|
break; |
|
238
|
|
|
case RelationInterface::FILTER_COUPLING: |
|
239
|
|
|
if ($relation->getCoupling() !== intval(!!$value)) { |
|
240
|
|
|
return false; |
|
241
|
|
|
} |
|
242
|
|
|
break; |
|
243
|
|
|
default: |
|
244
|
1 |
|
return false; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
1 |
|
return true; |
|
248
|
1 |
|
})); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Return the property values as array |
|
253
|
|
|
* |
|
254
|
|
|
* @return array Property values |
|
255
|
|
|
*/ |
|
256
|
5 |
|
public function toArray() |
|
257
|
|
|
{ |
|
258
|
5 |
|
$relations = []; |
|
259
|
|
|
/** @var RelationInterface $relation */ |
|
260
|
5 |
|
foreach ($this->relations as $relation) { |
|
261
|
4 |
|
$relationType = $relation->getType(); |
|
262
|
4 |
|
if (!array_key_exists($relationType, $relations)) { |
|
263
|
4 |
|
$relations[$relationType] = [strval($relation)]; |
|
264
|
4 |
|
continue; |
|
265
|
|
|
} |
|
266
|
4 |
|
$relations[$relationType][] = strval($relation); |
|
267
|
|
|
} |
|
268
|
5 |
|
ksort($relations); |
|
269
|
5 |
|
return $relations; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|