Completed
Push — v2 ( f15f0a...141320 )
by Joschi
05:08
created

ItemSetupTrait::validatePropertyStructure()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
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
     * 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(
69
        PropertyListFactoryInterface $propertyListFactory,
70
        array $type,
71
        array $properties,
72
        $itemId,
73
        $itemLanguage
74
    ) {
75 52
        $this->propertyListFactory = $propertyListFactory;
76 52
        $this->type = $this->validateTypes($type);
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77 50
        $this->properties = $this->validateProperties($properties);
0 ignored issues
show
Bug introduced by
The property properties does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78 47
        $this->itemId = $itemId ?: null;
0 ignored issues
show
Bug introduced by
The property itemId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79 47
        $this->itemLanguage = $itemLanguage ?: null;
0 ignored issues
show
Bug introduced by
The property itemLanguage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80 47
    }
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)
90
    {
91 52
        $nonEmptyTypes = array_filter(array_map([$this, 'validateType'], $types));
92
93
        // If there are no valid types
94 51
        if (!count($nonEmptyTypes)) {
95 1
            throw new InvalidArgumentException(
96 1
                InvalidArgumentException::EMPTY_TYPES_STR,
97 1
                InvalidArgumentException::EMPTY_TYPES
98
            );
99
        }
100
101 50
        return array_values($nonEmptyTypes);
102
    }
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)
112
    {
113 50
        $validatedProperties = $this->propertyListFactory->create();
114
115
        // Run through all validated properties
116 50
        foreach (array_filter(array_map([$this, 'validateProperty'], $properties)) as $property) {
117 34
            $validatedProperties->add($property);
118
        }
119
120 47
        return $validatedProperties;
121
    }
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)
130
    {
131
        // Validate the property structure
132 39
        $this->validatePropertyStructure($property);
133
134
        // If the property has values
135 38
        if (count($property->values)) {
136
            // Validate the property name
137 37
            $property->name = $this->validatePropertyName($property);
138
139
            // Validate the property values
140 36
            $property->values = $this->validatePropertyValues($property->values);
141
142
            // If the property has significant values
143 35
            if (count($property->values)) {
144 34
                return $property;
145
            }
146
        }
147
148 2
        return null;
149
    }
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)
158
    {
159
        // If the property object is invalid
160 39
        if (!is_object($property) || !$this->validatePropertyProperties($property)) {
161 1
            throw new InvalidArgumentException(
162 1
                InvalidArgumentException::INVALID_PROPERTY_STR,
163 1
                InvalidArgumentException::INVALID_PROPERTY
164
            );
165
        }
166 38
    }
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)
175
    {
176 39
        return isset($property->profile)
177 39
            && isset($property->name)
178 39
            && isset($property->values)
179 39
            && is_array($property->values);
180
    }
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)
189
    {
190 37
        $propertyName = trim($property->name);
191
192
        // If the property name is empty
193 37
        if (!strlen($propertyName)) {
194 1
            throw new InvalidArgumentException(
195 1
                InvalidArgumentException::EMPTY_PROPERTY_NAME_STR,
196 1
                InvalidArgumentException::EMPTY_PROPERTY_NAME
197
            );
198
        }
199
200 36
        return $propertyName;
201
    }
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)
211
    {
212 36
        $nonEmptyPropertyValues = [];
213
214
        // Run through all property values
215
        /** @var ValueInterface $value */
216 36
        foreach ($values as $value) {
217 36
            $this->processPropertyValue($value, $nonEmptyPropertyValues);
218
        }
219
220 35
        return $nonEmptyPropertyValues;
221
    }
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)
230
    {
231
        // If the value is not a nested item
232 36
        if (!($value instanceof ValueInterface)) {
233 1
            throw new InvalidArgumentException(
234 1
                sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)),
235 1
                InvalidArgumentException::INVALID_PROPERTY_VALUE
236
            );
237
        }
238
239
        // If the value isn't empty
240 35
        if (!$value->isEmpty()) {
241 34
            $nonEmptyPropertyValues[] = $value;
242
        }
243 35
    }
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)
253
    {
254 52
        $type = IriFactory::create($type);
255 51
        return strlen($type->name) ? $type : null;
256
    }
257
}
258