ItemSetupTrait   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 245
c 0
b 0
f 0
wmc 26
lcom 2
cbo 4
ccs 61
cts 61
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 13 3
A valTypes() 0 14 2
A valProperties() 0 11 2
A valProp() 0 21 3
A valPropStructure() 0 10 3
A valPropProperties() 0 7 4
A valPropName() 0 14 2
A valPropValues() 0 12 2
A procPropValue() 0 15 3
A valType() 0 6 2
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 © 2018 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 © 2018 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 56
    protected function setup(
97
        PropertyListFactoryInterface $propertyListFactory,
98
        array $type,
99
        array $properties,
100
        $itemId,
101
        $itemLanguage
102
    ) {
103 56
        $this->propertyListFactory = $propertyListFactory;
104 56
        $this->type                = $this->valTypes($type);
105 54
        $this->properties          = $this->valProperties($properties);
106 51
        $this->itemId              = $itemId ?: null;
107 51
        $this->itemLanguage        = $itemLanguage ?: null;
108 51
    }
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 56
    protected function valTypes(array $types)
119
    {
120 56
        $nonEmptyTypes = array_filter(array_map([$this, 'valType'], $types));
121
122
        // If there are no valid types
123 55
        if (!count($nonEmptyTypes)) {
124 1
            throw new InvalidArgumentException(
125 1
                InvalidArgumentException::EMPTY_TYPES_STR,
126 1
                InvalidArgumentException::EMPTY_TYPES
127
            );
128
        }
129
130 54
        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 54
    protected function valProperties(array $properties)
142
    {
143 54
        $validatedProperties = $this->propertyListFactory->create();
144
145
        // Run through all validated properties
146 54
        foreach (array_filter(array_map([$this, 'valProp'], $properties)) as $property) {
147 37
            $validatedProperties->add($property);
148
        }
149
150 51
        return $validatedProperties;
151
    }
152
153
    /**
154
     * Validate a single property
155
     *
156
     * @param \stdClass $property Property
157
     *
158
     * @return \stdClass Validated property
159
     */
160 42
    protected function valProp($property)
161
    {
162
        // Validate the property structure
163 42
        $this->valPropStructure($property);
164
165
        // If the property has values
166 41
        if (count($property->values)) {
167
            // Validate the property name
168 40
            $property->name = $this->valPropName($property);
169
170
            // Validate the property values
171 39
            $property->values = $this->valPropValues($property->values);
172
173
            // If the property has significant values
174 38
            if (count($property->values)) {
175 37
                return $property;
176
            }
177
        }
178
179 2
        return null;
180
    }
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 42
    protected function valPropStructure($property)
190
    {
191
        // If the property object is invalid
192 42
        if (!is_object($property) || !$this->valPropProperties($property)) {
193 1
            throw new InvalidArgumentException(
194 1
                InvalidArgumentException::INVALID_PROPERTY_STR,
195 1
                InvalidArgumentException::INVALID_PROPERTY
196
            );
197
        }
198 41
    }
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 42
    protected function valPropProperties($property)
208
    {
209 42
        return isset($property->profile)
210 42
               && isset($property->name)
211 42
               && isset($property->values)
212 42
               && is_array($property->values);
213
    }
214
215
    /**
216
     * Validate a property name
217
     *
218
     * @param \stdClass $property Property
219
     *
220
     * @return string Property name
221
     */
222 40
    protected function valPropName($property)
223
    {
224 40
        $propertyName = trim($property->name);
225
226
        // If the property name is empty
227 40
        if (!strlen($propertyName)) {
228 1
            throw new InvalidArgumentException(
229 1
                InvalidArgumentException::EMPTY_PROPERTY_NAME_STR,
230 1
                InvalidArgumentException::EMPTY_PROPERTY_NAME
231
            );
232
        }
233
234 39
        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 39
    protected function valPropValues(array $values)
246
    {
247 39
        $validPropertyValues = [];
248
249
        // Run through all property values
250
        /** @var ValueInterface $value */
251 39
        foreach ($values as $value) {
252 39
            $this->procPropValue($value, $validPropertyValues);
253
        }
254
255 38
        return $validPropertyValues;
256
    }
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 39
    protected function procPropValue($value, array &$validPropertyValues)
265
    {
266
        // If the value is not a nested item
267 39
        if (!($value instanceof ValueInterface)) {
268 1
            throw new InvalidArgumentException(
269 1
                sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)),
270 1
                InvalidArgumentException::INVALID_PROPERTY_VALUE
271
            );
272
        }
273
274
        // If the value isn't empty
275 38
        if (!$value->isEmpty()) {
276 37
            $validPropertyValues[] = $value;
277
        }
278 38
    }
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 56
    protected function valType($type)
289
    {
290 56
        $type = IriFactory::create($type);
291
292 55
        return strlen($type->name) ? $type : null;
293
    }
294
}
295