Completed
Push — v2 ( 7fc49e...7442ee )
by Joschi
04:33
created

PropertyList::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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\AliasFactoryInterface;
42
use Jkphl\Micrometa\Domain\Factory\IriFactory;
43
44
/**
45
 * Property list
46
 *
47
 * @package Jkphl\Micrometa
48
 * @subpackage Jkphl\RdfaLiteMicrodata\Domain
49
 */
50
class PropertyList implements PropertyListInterface
51
{
52
    /**
53
     * Property values
54
     *
55
     * @var array[]
56
     */
57
    protected $values = [];
58
    /**
59
     * Property names
60
     *
61
     * @var \stdClass[]
62
     */
63
    protected $names = [];
64
    /**
65
     * Name cursor mapping
66
     *
67
     * @var int[]
68
     */
69
    protected $nameToCursor = [];
70
    /**
71
     * Internal cursor
72
     *
73
     * @var int
74
     */
75
    protected $cursor = 0;
76
    /**
77
     * Alias factory
78
     *
79
     * @var AliasFactoryInterface
80
     */
81
    protected $aliasFactory;
82
83
    /**
84
     * Property list constructor
85
     *
86
     * @param AliasFactoryInterface $aliasFactory Alias factory
87
     */
88 32
    public function __construct(AliasFactoryInterface $aliasFactory)
89
    {
90 32
        $this->aliasFactory = $aliasFactory;
91 32
    }
92
93
    /**
94
     * Unset a property
95
     *
96
     * @param \stdClass|string $iri IRI
97
     * @throws ErrorException
98
     */
99 1
    public function offsetUnset($iri)
100
    {
101 1
        throw new ErrorException(
102 1
            sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri),
103 1
            ErrorException::CANNOT_UNSET_PROPERTY,
104 1
            E_WARNING
105
        );
106
    }
107
108
    /**
109
     * Return the number of properties
110
     *
111
     * @return int Number of properties
112
     */
113 1
    public function count()
114
    {
115 1
        return count($this->values);
116
    }
117
118
    /**
119
     * Return the current property values
120
     *
121
     * @return array Property values
122
     */
123 1
    public function current()
124
    {
125 1
        return $this->values[$this->cursor];
126
    }
127
128
    /**
129
     * Move forward to next element
130
     */
131 1
    public function next()
132
    {
133 1
        ++$this->cursor;
134 1
    }
135
136
    /**
137
     * Return the current IRI key
138
     *
139
     * @return \stdClass IRI key
140
     */
141 1
    public function key()
142
    {
143 1
        return $this->names[$this->cursor];
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this->names[$this->cursor]; (stdClass) is incompatible with the return type declared by the interface Iterator::key of type integer|double|string|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
144
    }
145
146
    /**
147
     * Checks if current position is valid
148
     *
149
     * @return boolean The current position is valid
150
     */
151 1
    public function valid()
152
    {
153 1
        return isset($this->values[$this->cursor]);
154
    }
155
156
    /**
157
     * Rewind the Iterator to the first element
158
     */
159 1
    public function rewind()
160
    {
161 1
        $this->cursor = 0;
162 1
    }
163
164
    /**
165
     * Add a property
166
     *
167
     * @param \stdClass $property Property
168
     */
169 16
    public function add($property)
170
    {
171 16
        $iri = IriFactory::create($property);
172 16
        $values = (is_object($property) && isset($property->values)) ? (array)$property->values : [];
173
174
        // Create the property values list if necessary
175 16
        if (!$this->offsetExists($iri)) {
176 16
            $this->offsetSet($iri, $values);
177 16
            return;
178
        }
179
180 2
        $propertyValues =& $this->offsetGet($iri);
181 2
        $propertyValues = array_merge($propertyValues, $values);
182 2
    }
183
184
    /**
185
     * Return whether a property exists
186
     *
187
     * @param \stdClass|string $iri IRI
188
     * @return boolean Property exists
189
     */
190 16
    public function offsetExists($iri)
191
    {
192 16
        $iri = IriFactory::create($iri);
193 16
        $iriStr = $iri->profile.$iri->name;
194 16
        return array_key_exists($iriStr, $this->nameToCursor);
195
    }
196
197
    /**
198
     * Set a particular property
199
     *
200
     * @param \stdClass|string $iri IRI
201
     * @param array $value Property values
202
     */
203 16
    public function offsetSet($iri, $value)
204
    {
205 16
        $iri = IriFactory::create($iri);
206 16
        $iriStr = $iri->profile.$iri->name;
207 16
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ? $this->nameToCursor[$iriStr] : count($this->values);
208
209
        // Run through all name aliases
210 16
        foreach ($this->aliasFactory->createAliases($iri->name) as $alias) {
211 16
            $this->nameToCursor[$iri->profile.$alias] = $cursor;
212
        }
213
214 16
        $this->names[$cursor] = $iri;
215 16
        $this->values[$cursor] = $value;
216 16
    }
217
218
    /**
219
     * Get a particular property
220
     *
221
     * @param \stdClass|string $iri IRI
222
     * @return array Property values
223
     * @throws OutOfBoundsException If the property name is unknown
224
     */
225 8
    public function &offsetGet($iri)
226
    {
227 8
        $iri = IriFactory::create($iri);
228 8
        $cursor = ($iri->profile !== '') ?
229 8
            $this->getProfiledPropertyCursor($iri) : $this->getPropertyCursor($iri->name);
230 5
        return $this->values[$cursor];
231
    }
232
233
    /**
234
     * Get a particular property cursor by its profiled name
235
     *
236
     * @param \stdClass $iri IRI
237
     * @return int Property cursor
238
     * @throws OutOfBoundsException If the property name is unknown
239
     */
240 3
    protected function getProfiledPropertyCursor($iri)
241
    {
242 3
        $iriStr = $iri->profile.$iri->name;
243
244
        // If the property name is unknown
245 3
        if (!isset($this->nameToCursor[$iriStr])) {
246 2
            $this->handleUnknownName($iriStr);
247
        }
248
249 2
        return $this->nameToCursor[$iriStr];
250
    }
251
252
    /**
253
     * Handle an unknown property name
254
     *
255
     * @param string $name Property name
256
     * @throws OutOfBoundsException If the property name is unknown
257
     */
258 5
    protected function handleUnknownName($name)
259
    {
260 5
        throw new OutOfBoundsException(
261 5
            sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name),
262 5
            OutOfBoundsException::UNKNOWN_PROPERTY_NAME
263
        );
264
    }
265
266
    /**
267
     * Get a particular property cursor by its name
268
     *
269
     * @param string $name Property name
270
     * @return int Property cursor
271
     */
272 6
    protected function getPropertyCursor($name)
273
    {
274
        // Run through all property names
275 6
        foreach ($this->names as $cursor => $iri) {
276 4
            if ($name === $iri->name) {
277 4
                return $cursor;
278
            }
279
        }
280
281 3
        return $this->handleUnknownName($name);
282
    }
283
284
    /**
285
     * Return an array form
286
     *
287
     * @return array Array form
288
     */
289 16
    public function toArray()
290
    {
291 16
        $values = $this->values;
292 16
        return array_map(
293 16
            function ($cursor) use ($values) {
294 7
                return $values[$cursor];
295 16
            },
296 16
            $this->nameToCursor
297
        );
298
    }
299
}
300