1 | <?php |
||
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 | 49 | protected function setup( |
|
109 | |||
110 | /** |
||
111 | * Validate and sanitize the item types |
||
112 | * |
||
113 | * @param string[]|\stdClass[] $types Item types |
||
114 | * |
||
115 | * @return array Validated item types |
||
116 | * @throws InvalidArgumentException If there are no valid types |
||
117 | */ |
||
118 | 49 | protected function valTypes(array $types) |
|
119 | { |
||
120 | 49 | $nonEmptyTypes = array_filter(array_map([$this, 'valType'], $types)); |
|
121 | |||
122 | // If there are no valid types |
||
123 | 49 | if (!count($nonEmptyTypes)) { |
|
124 | throw new InvalidArgumentException( |
||
125 | InvalidArgumentException::EMPTY_TYPES_STR, |
||
126 | InvalidArgumentException::EMPTY_TYPES |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | 49 | return array_values($nonEmptyTypes); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Validate the item properties |
||
135 | * |
||
136 | * @param array $properties Item properties |
||
137 | * |
||
138 | * @return PropertyListInterface Validated item properties |
||
139 | * @throws InvalidArgumentException If the property name is empty |
||
140 | */ |
||
141 | 49 | protected function valProperties(array $properties) |
|
152 | |||
153 | /** |
||
154 | * Validate a single property |
||
155 | * |
||
156 | * @param \stdClass $property Property |
||
157 | * |
||
158 | * @return \stdClass Validated property |
||
159 | */ |
||
160 | 38 | protected function valProp($property) |
|
181 | |||
182 | /** |
||
183 | * Validate the structure of a property object |
||
184 | * |
||
185 | * @param \stdClass $property Property object |
||
186 | * |
||
187 | * @throws InvalidArgumentException If the property object is invalid |
||
188 | */ |
||
189 | 38 | protected function valPropStructure($property) |
|
190 | { |
||
191 | // If the property object is invalid |
||
192 | 38 | if (!is_object($property) || !$this->valPropProperties($property)) { |
|
193 | throw new InvalidArgumentException( |
||
194 | InvalidArgumentException::INVALID_PROPERTY_STR, |
||
195 | InvalidArgumentException::INVALID_PROPERTY |
||
196 | ); |
||
197 | } |
||
198 | 38 | } |
|
199 | |||
200 | /** |
||
201 | * Validate the properties of a property |
||
202 | * |
||
203 | * @param \stdClass $property Property |
||
204 | * |
||
205 | * @return bool Property properties are valid |
||
206 | */ |
||
207 | 38 | protected function valPropProperties($property) |
|
214 | |||
215 | /** |
||
216 | * Validate a property name |
||
217 | * |
||
218 | * @param \stdClass $property Property |
||
219 | * |
||
220 | * @return string Property name |
||
221 | */ |
||
222 | 37 | protected function valPropName($property) |
|
223 | { |
||
224 | 37 | $propertyName = trim($property->name); |
|
225 | |||
226 | // If the property name is empty |
||
227 | 37 | if (!strlen($propertyName)) { |
|
228 | throw new InvalidArgumentException( |
||
229 | InvalidArgumentException::EMPTY_PROPERTY_NAME_STR, |
||
230 | InvalidArgumentException::EMPTY_PROPERTY_NAME |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | 37 | return $propertyName; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Validate a list of property values |
||
239 | * |
||
240 | * @param array $values Property values |
||
241 | * |
||
242 | * @return array Validated property values |
||
243 | * @throws InvalidArgumentException If the value is not a nested item |
||
244 | */ |
||
245 | 37 | protected function valPropValues(array $values) |
|
257 | |||
258 | /** |
||
259 | * Process a (non-empty) property value |
||
260 | * |
||
261 | * @param ValueInterface $value Property value |
||
262 | * @param array $validPropertyValues Non-empty property values |
||
263 | */ |
||
264 | 37 | protected function procPropValue($value, array &$validPropertyValues) |
|
265 | { |
||
266 | // If the value is not a nested item |
||
267 | 37 | if (!($value instanceof ValueInterface)) { |
|
268 | throw new InvalidArgumentException( |
||
269 | sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)), |
||
270 | InvalidArgumentException::INVALID_PROPERTY_VALUE |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | // If the value isn't empty |
||
275 | 37 | if (!$value->isEmpty()) { |
|
276 | 36 | $validPropertyValues[] = $value; |
|
277 | } |
||
278 | 37 | } |
|
279 | |||
280 | /** |
||
281 | * Validate a single item type |
||
282 | * |
||
283 | * @param \stdClass|Iri|string $type Item type |
||
284 | * |
||
285 | * @return Iri|null Validated item type |
||
286 | * @throws InvalidArgumentException If the item type object is invalid |
||
287 | */ |
||
288 | 49 | protected function valType($type) |
|
294 | } |
||
295 |