Completed
Push — master ( 754f5c...d85481 )
by Joschi
05:03
created

PropertyList   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 218
c 0
b 0
f 0
wmc 24
lcom 1
cbo 4
ccs 62
cts 62
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 14 4
A offsetUnset() 0 8 1
A count() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A offsetExists() 0 11 3
A handleUnknownName() 0 7 1
A offsetSet() 0 9 2
A offsetGet() 0 7 2
A getProfiledPropertyCursor() 0 11 2
A getPropertyCursor() 0 11 3
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Domain\Item
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\ErrorException;
40
use Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException;
41
use Jkphl\Micrometa\Domain\Factory\IriFactory;
42
43
/**
44
 * Property list
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
48
 */
49
class PropertyList implements PropertyListInterface
50
{
51
    /**
52
     * Property values
53
     *
54
     * @var array[]
55
     */
56
    protected $values = [];
57
    /**
58
     * Property names
59
     *
60
     * @var \stdClass[]
61
     */
62
    protected $names = [];
63
    /**
64
     * Name cursor mapping
65
     *
66
     * @var int[]
67
     */
68
    protected $nameToCursor = [];
69
    /**
70
     * Internal cursor
71
     *
72
     * @var int
73
     */
74
    protected $cursor = 0;
75
76
    /**
77
     * Unset a property
78
     *
79
     * @param \stdClass|string $iri IRI
80
     * @throws ErrorException
81
     */
82 1
    public function offsetUnset($iri)
83
    {
84 1
        throw new ErrorException(
85 1
            sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri),
86 1
            ErrorException::CANNOT_UNSET_PROPERTY,
87 1
            E_WARNING
88
        );
89
    }
90
91
    /**
92
     * Return the number of properties
93
     *
94
     * @return int Number of properties
95
     */
96 2
    public function count()
97
    {
98 2
        return count($this->values);
99
    }
100
101
    /**
102
     * Return the current property values
103
     *
104
     * @return array Property values
105
     */
106 8
    public function current()
107
    {
108 8
        return $this->values[$this->cursor];
109
    }
110
111
    /**
112
     * Move forward to next element
113
     */
114 8
    public function next()
115
    {
116 8
        ++$this->cursor;
117 8
    }
118
119
    /**
120
     * Return the current IRI key
121
     *
122
     * @return \stdClass IRI key
123
     */
124 8
    public function key()
125
    {
126 8
        return $this->names[$this->cursor];
127
    }
128
129
    /**
130
     * Checks if current position is valid
131
     *
132
     * @return boolean The current position is valid
133
     */
134 17
    public function valid()
135
    {
136 17
        return isset($this->values[$this->cursor]);
137
    }
138
139
    /**
140
     * Rewind the Iterator to the first element
141
     */
142 17
    public function rewind()
143
    {
144 17
        $this->cursor = 0;
145 17
    }
146
147
    /**
148
     * Add a property
149
     *
150
     * @param \stdClass|Iri $property Property
151
     */
152 37
    public function add($property)
153
    {
154 37
        $iri = IriFactory::create($property);
155 37
        $values = (is_object($property) && isset($property->values)) ? (array)$property->values : [];
156
157
        // Create the property values list if necessary
158 37
        if (!$this->offsetExists($iri)) {
159 37
            $this->offsetSet($iri, $values);
160 37
            return;
161
        }
162
163 2
        $propertyValues =& $this->offsetGet($iri);
164 2
        $propertyValues = array_merge($propertyValues, $values);
165 2
    }
166
167
    /**
168
     * Return whether a property exists
169
     *
170
     * @param \stdClass|Iri|string $iri IRI
171
     * @return boolean Property exists
172
     */
173 37
    public function offsetExists($iri)
174
    {
175 37
        $iri = IriFactory::create($iri);
176
        try {
177 37
            ($iri->profile !== '') ?
178 37
                $this->getProfiledPropertyCursor($iri) : $this->getPropertyCursor($iri->name);
179 3
            return true;
180 37
        } catch (OutOfBoundsException $exception) {
181 37
            return false;
182
        }
183
    }
184
185
    /**
186
     * Get a particular property cursor by its profiled name
187
     *
188
     * @param Iri $iri IRI
189
     * @return int Property cursor
190
     * @throws OutOfBoundsException If the property name is unknown
191
     */
192 31
    protected function getProfiledPropertyCursor($iri)
193
    {
194 31
        $iriStr = strval($iri);
195
196
        // If the property name is unknown
197 31
        if (!isset($this->nameToCursor[$iriStr])) {
198 31
            $this->handleUnknownName($iriStr);
199
        }
200
201 6
        return $this->nameToCursor[$iriStr];
202
    }
203
204
    /**
205
     * Handle an unknown property name
206
     *
207
     * @param string $name Property name
208
     * @throws OutOfBoundsException If the property name is unknown
209
     */
210 40
    protected function handleUnknownName($name)
211
    {
212 40
        throw new OutOfBoundsException(
213 40
            sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
214 40
            OutOfBoundsException::UNKNOWN_PROPERTY_NAME
215
        );
216
    }
217
218
    /**
219
     * Get a particular property cursor by its name
220
     *
221
     * @param string $name Property name
222
     * @return int Property cursor
223
     */
224 10
    protected function getPropertyCursor($name)
225
    {
226
        // Run through all property names
227 10
        foreach ($this->names as $cursor => $iri) {
228 5
            if (in_array($name, [$iri->name, strval($iri)])) {
229 5
                return $cursor;
230
            }
231
        }
232
233 8
        return $this->handleUnknownName($name);
234
    }
235
236
    /**
237
     * Set a particular property
238
     *
239
     * @param \stdClass|Iri|string $iri IRI
240
     * @param array $value Property values
241
     */
242 9
    public function offsetSet($iri, $value)
243
    {
244 9
        $iri = IriFactory::create($iri);
245 9
        $iriStr = strval($iri);
246 9
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ?
247 9
            $this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor));
248 9
        $this->names[$cursor] = $iri;
249 9
        $this->values[$cursor] = $value;
250 9
    }
251
252
    /**
253
     * Get a particular property
254
     *
255
     * @param \stdClass|Iri|string $iri IRI
256
     * @return array Property values
257
     * @throws OutOfBoundsException If the property name is unknown
258
     */
259 15
    public function &offsetGet($iri)
260
    {
261 15
        $iri = IriFactory::create($iri);
262 15
        $cursor = ($iri->profile !== '') ?
263 15
            $this->getProfiledPropertyCursor($iri) : $this->getPropertyCursor($iri->name);
264 12
        return $this->values[$cursor];
265
    }
266
}
267