1 | <?php |
||
50 | trait ItemSetupTrait |
||
51 | { |
||
52 | /** |
||
53 | * Property list factory |
||
54 | * |
||
55 | * @var PropertyListFactoryInterface |
||
56 | */ |
||
57 | protected $propertyListFactory; |
||
58 | |||
59 | /** |
||
60 | * Setup the item |
||
61 | * |
||
62 | * @param PropertyListFactoryInterface $propertyListFactory Property list factory |
||
63 | * @param string[]|\stdClass[] $type Item type(s) |
||
64 | * @param \stdClass[] $properties Item properties |
||
65 | * @param string $itemId Item ID |
||
66 | * @param string $itemLanguage Item language |
||
67 | */ |
||
68 | 52 | protected function setup( |
|
81 | |||
82 | /** |
||
83 | * Validate and sanitize the item types |
||
84 | * |
||
85 | * @param string[]|\stdClass[] $types Item types |
||
86 | * @return array Validated item types |
||
87 | * @throws InvalidArgumentException If there are no valid types |
||
88 | */ |
||
89 | 52 | protected function validateTypes(array $types) |
|
103 | |||
104 | /** |
||
105 | * Validate the item properties |
||
106 | * |
||
107 | * @param array $properties Item properties |
||
108 | * @return PropertyListInterface Validated item properties |
||
109 | * @throws InvalidArgumentException If the property name is empty |
||
110 | */ |
||
111 | 50 | protected function validateProperties(array $properties) |
|
122 | |||
123 | /** |
||
124 | * Validate a single property |
||
125 | * |
||
126 | * @param \stdClass $property Property |
||
127 | * @return \stdClass Validated property |
||
128 | */ |
||
129 | 39 | protected function validateProperty($property) |
|
150 | |||
151 | /** |
||
152 | * Validate the structure of a property object |
||
153 | * |
||
154 | * @param \stdClass $property Property object |
||
155 | * @throws InvalidArgumentException If the property object is invalid |
||
156 | */ |
||
157 | 39 | protected function validatePropertyStructure($property) |
|
167 | |||
168 | /** |
||
169 | * Validate the properties of a property |
||
170 | * |
||
171 | * @param \stdClass $property Property |
||
172 | * @return bool Property properties are valid |
||
173 | */ |
||
174 | 39 | protected function validatePropertyProperties($property) |
|
181 | |||
182 | /** |
||
183 | * Validate a property name |
||
184 | * |
||
185 | * @param \stdClass $property Property |
||
186 | * @return string Property name |
||
187 | */ |
||
188 | 37 | protected function validatePropertyName($property) |
|
202 | |||
203 | /** |
||
204 | * Validate a list of property values |
||
205 | * |
||
206 | * @param array $values Property values |
||
207 | * @return array Validated property values |
||
208 | * @throws InvalidArgumentException If the value is not a nested item |
||
209 | */ |
||
210 | 36 | protected function validatePropertyValues(array $values) |
|
222 | |||
223 | /** |
||
224 | * Process a (non-empty) property value |
||
225 | * |
||
226 | * @param ValueInterface $value Property value |
||
227 | * @param array $nonEmptyPropertyValues Non-empty property values |
||
228 | */ |
||
229 | 36 | protected function processPropertyValue($value, array &$nonEmptyPropertyValues) |
|
244 | |||
245 | /** |
||
246 | * Validate a single item type |
||
247 | * |
||
248 | * @param \stdClass|Iri|string $type Item type |
||
249 | * @return Iri|null Validated item type |
||
250 | * @throws InvalidArgumentException If the item type object is invalid |
||
251 | */ |
||
252 | 52 | protected function validateType($type) |
|
257 | } |
||
258 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: