Completed
Push — v2 ( 3135e6...5d3e3a )
by Joschi
04:42 queued 10s
created

Item   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 178
rs 10
c 0
b 0
f 0
ccs 55
cts 55
cp 1
wmc 20
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A validateTypes() 0 14 2
B validateProperties() 0 25 5
B validatePropertyValues() 0 28 5
A getType() 0 4 1
A getId() 0 4 1
A getProperties() 0 4 1
A getProperty() 0 12 2
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Domain\Miom
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\Exceptions\OutOfBoundsException;
41
42
/**
43
 * Micro information item
44
 *
45
 * @package Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Domain
47
 */
48
class Item implements ItemInterface
49
{
50
    /**
51
     * Item type(s)
52
     *
53
     * @var string[]
54
     */
55
    protected $type;
56
57
    /**
58
     * Item properties
59
     *
60
     * @var array[]
61
     */
62
    protected $properties;
63
64
    /**
65
     * Item ID
66
     *
67
     * @var string
68
     */
69
    protected $itemId;
70
71
    /**
72
     * Item constructor
73
     *
74
     * @param string|array $type Item type(s)
75
     * @param array[] $properties Item properties
76
     * @param string|null $itemId Item id
77
     */
78 16
    public function __construct($type, array $properties = [], $itemId = null)
79
    {
80 16
        $this->type = $this->validateTypes(is_array($type) ? $type : [$type]);
81 15
        $this->properties = $this->validateProperties($properties);
82 13
        $this->itemId = trim($itemId) ?: null;
83 13
    }
84
85
    /**
86
     * Validate and sanitize the item types
87
     *
88
     * @param array $types Item types
89
     * @return array Validated item types
90
     * @throws InvalidArgumentException If there are no valid types
91
     */
92 16
    protected function validateTypes(array $types)
93
    {
94 16
        $nonEmptyTypes = array_filter(array_map('trim', $types));
95
96
        // If there are no valid types
97 16
        if (!count($nonEmptyTypes)) {
98 1
            throw new InvalidArgumentException(
99 1
                InvalidArgumentException::EMPTY_TYPES_STR,
100
                InvalidArgumentException::EMPTY_TYPES
101 1
            );
102
        }
103
104 15
        return array_values($nonEmptyTypes);
105
    }
106
107
    /**
108
     * Validate the item properties
109
     *
110
     * @param array $properties Item properties
111
     * @return array Validated item properties
112
     * @throws InvalidArgumentException If the property name is empty
113
     */
114 15
    protected function validateProperties(array $properties)
115
    {
116 15
        $nonEmptyProperties = [];
117
118
        // Run through all properties
119 15
        foreach ($properties as $name => $values) {
120 9
            if ($values) {
121 9
                $propertyName = trim($name);
122
123
                // If the property name is empty
124 9
                if (!strlen($propertyName)) {
125 1
                    throw new InvalidArgumentException(
126 1
                        InvalidArgumentException::EMPTY_PROPERTY_NAME_STR,
127
                        InvalidArgumentException::EMPTY_PROPERTY_NAME
128 1
                    );
129
                }
130
131 8
                $nonEmptyProperties[$propertyName] = $this->validatePropertyValues(
132 8
                    is_array($values) ? $values : [$values]
133 8
                );
134 7
            }
135 13
        }
136
137 13
        return $nonEmptyProperties;
138
    }
139
140
    /**
141
     * Validate a list of property values
142
     *
143
     * @param array $values Property values
144
     * @return array Validated property values
145
     * @throws InvalidArgumentException If the value is not a nested item
146
     */
147 8
    protected function validatePropertyValues(array $values)
148
    {
149 8
        $nonEmptyPropertyValues = [];
150
151
        // Run through all property values
152 8
        foreach ($values as $value) {
153
            // If it's a string value
154 8
            if (is_string($value)) {
155 6
                $nonEmptyValue = trim($value);
156 6
                if (strlen($nonEmptyValue)) {
157 6
                    $nonEmptyPropertyValues[] = $nonEmptyValue;
158 6
                }
159 6
                continue;
160
            }
161
162
            // If the value is not a nested item
163 2
            if (!($value instanceof ItemInterface)) {
164 1
                throw new InvalidArgumentException(
165 1
                    sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)),
166
                    InvalidArgumentException::INVALID_PROPERTY_VALUE
167 1
                );
168
            }
169
170 1
            $nonEmptyPropertyValues[] = $value;
171 7
        }
172
173 7
        return $nonEmptyPropertyValues;
174
    }
175
176
    /**
177
     * Return the item types
178
     *
179
     * @return string[] Item types
180
     */
181 11
    public function getType()
182
    {
183 11
        return $this->type;
184
    }
185
186
    /**
187
     * Return the item ID (if any)
188
     *
189
     * @return string|null Item id
190
     */
191 11
    public function getId()
192
    {
193 11
        return $this->itemId;
194
    }
195
196
    /**
197
     * Return all item properties
198
     *
199
     * @return array[] Item properties list
200
     */
201 11
    public function getProperties()
202
    {
203 11
        return $this->properties;
204
    }
205
206
    /**
207
     * Return the values of a particular property
208
     *
209
     * @param string $name Property name
210
     * @return array Item property values
211
     * @throws OutOfBoundsException If the property is unknown
212
     */
213 2
    public function getProperty($name)
214
    {
215
        // If the property is unknown
216 2
        if (!isset($this->properties[$name])) {
217 1
            throw new OutOfBoundsException(
218 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
219
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
220 1
            );
221
        }
222
223 1
        return $this->properties[$name];
224
    }
225
}
226