Completed
Push — v2 ( d5d6a3...7fc49e )
by Joschi
05:54
created

PropertyList::offsetGet()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 4
rs 8.7972
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\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
    public function offsetUnset($iri)
83
    {
84
        throw new ErrorException(
85
            sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri),
86
            ErrorException::CANNOT_UNSET_PROPERTY,
87
            E_WARNING
88
        );
89
    }
90
91
    /**
92
     * Return the number of properties
93
     *
94
     * @return int Number of properties
95
     */
96
    public function count()
97
    {
98
        return count($this->nameToCursor);
99
    }
100
101
    /**
102
     * Return the current property values
103
     *
104
     * @return array Property values
105
     */
106
    public function current()
107
    {
108
        return $this->values[$this->cursor];
109
    }
110
111
    /**
112
     * Move forward to next element
113
     */
114
    public function next()
115
    {
116
        ++$this->cursor;
117
    }
118
119
    /**
120
     * Return the current IRI key
121
     *
122
     * @return \stdClass IRI key
123
     */
124
    public function key()
125
    {
126
        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...
127
    }
128
129
    /**
130
     * Checks if current position is valid
131
     *
132
     * @return boolean The current position is valid
133
     */
134
    public function valid()
135
    {
136
        return isset($this->values[$this->cursor]);
137
    }
138
139
    /**
140
     * Rewind the Iterator to the first element
141
     */
142
    public function rewind()
143
    {
144
        $this->cursor = 0;
145
    }
146
147
    /**
148
     * Add a property
149
     *
150
     * @param \stdClass $property Property
151
     */
152 15
    public function add($property)
153
    {
154 15
        $iri = IriFactory::create($property);
155 15
        $values = (is_object($property) && isset($property->values)) ? (array)$property->values : [];
156
157
        // Create the property values list if necessary
158 15
        if (!$this->offsetExists($iri)) {
159 15
            $this->offsetSet($iri, $values);
160 15
            return;
161
        }
162
163 1
        $propertyValues =& $this->offsetGet($iri);
164 1
        $propertyValues = array_merge($propertyValues, $values);
165 1
    }
166
167
    /**
168
     * Return whether a property exists
169
     *
170
     * @param \stdClass|string $iri IRI
171
     * @return boolean Property exists
172
     */
173 15
    public function offsetExists($iri)
174
    {
175 15
        $iri = IriFactory::create($iri);
176 15
        $iriStr = $iri->profile.$iri->name;
177 15
        return array_key_exists($iriStr, $this->nameToCursor);
178
    }
179
180
    /**
181
     * Set a particular property
182
     *
183
     * @param \stdClass|string $iri IRI
184
     * @param array $value Property values
185
     */
186 15
    public function offsetSet($iri, $value)
187
    {
188 15
        $iri = IriFactory::create($iri);
189 15
        $iriStr = $iri->profile.$iri->name;
190 15
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ?
191 15
            $this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor));
192 15
        $this->names[$cursor] = $iri;
193 15
        $this->values[$cursor] = $value;
194 15
    }
195
196
    /**
197
     * Get a particular property
198
     *
199
     * @param \stdClass|string $iri IRI
200
     * @return array Property values
201
     * @throws OutOfBoundsException If the property name is unknown
202
     */
203 5
    public function &offsetGet($iri)
204
    {
205 5
        $iri = IriFactory::create($iri);
206
207
        // If a profiled property was requested
208 5
        if ($iri->profile !== '') {
209 1
            $cursor = $this->getProfiledPropertyCursor($iri);
210 1
            return $this->values[$cursor];
211
        }
212
213
        // Run through all property names
214 4
        foreach ($this->names as $cursor => $nameIri) {
215 3
            if ($iri->name === $nameIri->name) {
216 3
                return $this->values[$cursor];
217
            }
218
        }
219
220
        // If the property name is unknown
221 2
        throw new OutOfBoundsException(
222 2
            sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $iri->name),
223 2
            OutOfBoundsException::UNKNOWN_PROPERTY_NAME
224
        );
225
    }
226
227
    /**
228
     * Get a particular property by its profiled name
229
     *
230
     * @param \stdClass $iri IRI
231
     * @return int Property cursor
232
     * @throws OutOfBoundsException If the property name is unknown
233
     */
234 1
    protected function getProfiledPropertyCursor($iri)
235
    {
236 1
        $iriStr = $iri->profile.$iri->name;
237
238
        // If the property name is unknown
239 1
        if (!isset($this->nameToCursor[$iriStr])) {
240 1
            throw new OutOfBoundsException(
241 1
                sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $iriStr),
242 1
                OutOfBoundsException::UNKNOWN_PROPERTY_NAME
243
            );
244
        }
245
246 1
        return $this->nameToCursor[$iriStr];
247
    }
248
249
    /**
250
     * Return an array form
251
     *
252
     * @return array Array form
253
     */
254 13
    public function toArray()
255
    {
256 13
        $values = $this->values;
257 13
        return array_map(
258 13
            function ($cursor) use ($values) {
259 6
                return $values[$cursor];
260 13
            },
261 13
            $this->nameToCursor
262
        );
263
    }
264
}
265