1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Domain |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Domain\Item; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException; |
40
|
|
|
use Jkphl\Micrometa\Domain\Factory\IriFactory; |
41
|
|
|
use Jkphl\Micrometa\Domain\Factory\PropertyListFactoryInterface; |
42
|
|
|
use Jkphl\Micrometa\Domain\Value\ValueInterface; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Item setup methods |
46
|
|
|
* |
47
|
|
|
* @package Jkphl\Micrometa |
48
|
|
|
* @subpackage Jkphl\Micrometa\Domain |
49
|
|
|
*/ |
50
|
|
|
trait ItemSetupTrait |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Property list factory |
54
|
|
|
* |
55
|
|
|
* @var PropertyListFactoryInterface |
56
|
|
|
*/ |
57
|
|
|
protected $propertyListFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Item type(s) |
61
|
|
|
* |
62
|
|
|
* @var \stdClass[] |
63
|
|
|
*/ |
64
|
|
|
protected $type; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Item properties |
68
|
|
|
* |
69
|
|
|
* @var PropertyListInterface |
70
|
|
|
*/ |
71
|
|
|
protected $properties; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Item ID |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $itemId; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Item language |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
protected $itemLanguage; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setup the item |
89
|
|
|
* |
90
|
|
|
* @param PropertyListFactoryInterface $propertyListFactory Property list factory |
91
|
|
|
* @param string[]|\stdClass[] $type Item type(s) |
92
|
|
|
* @param \stdClass[] $properties Item properties |
93
|
|
|
* @param string $itemId Item ID |
94
|
|
|
* @param string $itemLanguage Item language |
95
|
|
|
*/ |
96
|
54 |
|
protected function setup( |
97
|
|
|
PropertyListFactoryInterface $propertyListFactory, |
98
|
|
|
array $type, |
99
|
|
|
array $properties, |
100
|
|
|
$itemId, |
101
|
|
|
$itemLanguage |
102
|
|
|
) { |
103
|
54 |
|
$this->propertyListFactory = $propertyListFactory; |
104
|
54 |
|
$this->type = $this->valTypes($type); |
105
|
52 |
|
$this->properties = $this->valProperties($properties); |
106
|
49 |
|
$this->itemId = $itemId ?: null; |
107
|
49 |
|
$this->itemLanguage = $itemLanguage ?: null; |
108
|
49 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate and sanitize the item types |
112
|
|
|
* |
113
|
|
|
* @param string[]|\stdClass[] $types Item types |
114
|
|
|
* @return array Validated item types |
115
|
|
|
* @throws InvalidArgumentException If there are no valid types |
116
|
|
|
*/ |
117
|
54 |
|
protected function valTypes(array $types) |
118
|
|
|
{ |
119
|
54 |
|
$nonEmptyTypes = array_filter(array_map([$this, 'valType'], $types)); |
120
|
|
|
|
121
|
|
|
// If there are no valid types |
122
|
53 |
|
if (!count($nonEmptyTypes)) { |
123
|
1 |
|
throw new InvalidArgumentException( |
124
|
1 |
|
InvalidArgumentException::EMPTY_TYPES_STR, |
125
|
1 |
|
InvalidArgumentException::EMPTY_TYPES |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
52 |
|
return array_values($nonEmptyTypes); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Validate the item properties |
134
|
|
|
* |
135
|
|
|
* @param array $properties Item properties |
136
|
|
|
* @return PropertyListInterface Validated item properties |
137
|
|
|
* @throws InvalidArgumentException If the property name is empty |
138
|
|
|
*/ |
139
|
52 |
|
protected function valProperties(array $properties) |
140
|
|
|
{ |
141
|
52 |
|
$validatedProperties = $this->propertyListFactory->create(); |
142
|
|
|
|
143
|
|
|
// Run through all validated properties |
144
|
52 |
|
foreach (array_filter(array_map([$this, 'valProp'], $properties)) as $property) { |
145
|
36 |
|
$validatedProperties->add($property); |
146
|
|
|
} |
147
|
|
|
|
148
|
49 |
|
return $validatedProperties; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Validate a single property |
153
|
|
|
* |
154
|
|
|
* @param \stdClass $property Property |
155
|
|
|
* @return \stdClass Validated property |
156
|
|
|
*/ |
157
|
41 |
|
protected function valProp($property) |
158
|
|
|
{ |
159
|
|
|
// Validate the property structure |
160
|
41 |
|
$this->valPropStructure($property); |
161
|
|
|
|
162
|
|
|
// If the property has values |
163
|
40 |
|
if (count($property->values)) { |
164
|
|
|
// Validate the property name |
165
|
39 |
|
$property->name = $this->valPropName($property); |
166
|
|
|
|
167
|
|
|
// Validate the property values |
168
|
38 |
|
$property->values = $this->valPropValues($property->values); |
169
|
|
|
|
170
|
|
|
// If the property has significant values |
171
|
37 |
|
if (count($property->values)) { |
172
|
36 |
|
return $property; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
return null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Validate the structure of a property object |
181
|
|
|
* |
182
|
|
|
* @param \stdClass $property Property object |
183
|
|
|
* @throws InvalidArgumentException If the property object is invalid |
184
|
|
|
*/ |
185
|
41 |
|
protected function valPropStructure($property) |
186
|
|
|
{ |
187
|
|
|
// If the property object is invalid |
188
|
41 |
|
if (!is_object($property) || !$this->valPropProperties($property)) { |
189
|
1 |
|
throw new InvalidArgumentException( |
190
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_STR, |
191
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY |
192
|
|
|
); |
193
|
|
|
} |
194
|
40 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Validate the properties of a property |
198
|
|
|
* |
199
|
|
|
* @param \stdClass $property Property |
200
|
|
|
* @return bool Property properties are valid |
201
|
|
|
*/ |
202
|
41 |
|
protected function valPropProperties($property) |
203
|
|
|
{ |
204
|
41 |
|
return isset($property->profile) |
205
|
41 |
|
&& isset($property->name) |
206
|
41 |
|
&& isset($property->values) |
207
|
41 |
|
&& is_array($property->values); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Validate a property name |
212
|
|
|
* |
213
|
|
|
* @param \stdClass $property Property |
214
|
|
|
* @return string Property name |
215
|
|
|
*/ |
216
|
39 |
|
protected function valPropName($property) |
217
|
|
|
{ |
218
|
39 |
|
$propertyName = trim($property->name); |
219
|
|
|
|
220
|
|
|
// If the property name is empty |
221
|
39 |
|
if (!strlen($propertyName)) { |
222
|
1 |
|
throw new InvalidArgumentException( |
223
|
1 |
|
InvalidArgumentException::EMPTY_PROPERTY_NAME_STR, |
224
|
1 |
|
InvalidArgumentException::EMPTY_PROPERTY_NAME |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
38 |
|
return $propertyName; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Validate a list of property values |
233
|
|
|
* |
234
|
|
|
* @param array $values Property values |
235
|
|
|
* @return array Validated property values |
236
|
|
|
* @throws InvalidArgumentException If the value is not a nested item |
237
|
|
|
*/ |
238
|
38 |
|
protected function valPropValues(array $values) |
239
|
|
|
{ |
240
|
38 |
|
$validPropertyValues = []; |
241
|
|
|
|
242
|
|
|
// Run through all property values |
243
|
|
|
/** @var ValueInterface $value */ |
244
|
38 |
|
foreach ($values as $value) { |
245
|
38 |
|
$this->procPropValue($value, $validPropertyValues); |
246
|
|
|
} |
247
|
|
|
|
248
|
37 |
|
return $validPropertyValues; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Process a (non-empty) property value |
253
|
|
|
* |
254
|
|
|
* @param ValueInterface $value Property value |
255
|
|
|
* @param array $validPropertyValues Non-empty property values |
256
|
|
|
*/ |
257
|
38 |
|
protected function procPropValue($value, array &$validPropertyValues) |
258
|
|
|
{ |
259
|
|
|
// If the value is not a nested item |
260
|
38 |
|
if (!($value instanceof ValueInterface)) { |
261
|
1 |
|
throw new InvalidArgumentException( |
262
|
1 |
|
sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)), |
263
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_VALUE |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// If the value isn't empty |
268
|
37 |
|
if (!$value->isEmpty()) { |
269
|
36 |
|
$validPropertyValues[] = $value; |
270
|
|
|
} |
271
|
37 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Validate a single item type |
275
|
|
|
* |
276
|
|
|
* @param \stdClass|Iri|string $type Item type |
277
|
|
|
* @return Iri|null Validated item type |
278
|
|
|
* @throws InvalidArgumentException If the item type object is invalid |
279
|
|
|
*/ |
280
|
54 |
|
protected function valType($type) |
281
|
|
|
{ |
282
|
54 |
|
$type = IriFactory::create($type); |
283
|
53 |
|
return strlen($type->name) ? $type : null; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|