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 © 2018 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 © 2018 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
|
|
|
* |
81
|
|
|
* @throws ErrorException |
82
|
|
|
*/ |
83
|
1 |
|
public function offsetUnset($iri) |
84
|
|
|
{ |
85
|
1 |
|
throw new ErrorException( |
86
|
1 |
|
sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri), |
87
|
1 |
|
ErrorException::CANNOT_UNSET_PROPERTY, |
88
|
1 |
|
E_WARNING |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the number of properties |
94
|
|
|
* |
95
|
|
|
* @return int Number of properties |
96
|
|
|
*/ |
97
|
2 |
|
public function count() |
98
|
|
|
{ |
99
|
2 |
|
return count($this->values); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the current property values |
104
|
|
|
* |
105
|
|
|
* @return array Property values |
106
|
|
|
*/ |
107
|
8 |
|
public function current() |
108
|
|
|
{ |
109
|
8 |
|
return $this->values[$this->cursor]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Move forward to next element |
114
|
|
|
*/ |
115
|
8 |
|
public function next() |
116
|
|
|
{ |
117
|
8 |
|
++$this->cursor; |
118
|
8 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the current IRI key |
122
|
|
|
* |
123
|
|
|
* @return \stdClass IRI key |
124
|
|
|
*/ |
125
|
8 |
|
public function key() |
126
|
|
|
{ |
127
|
8 |
|
return $this->names[$this->cursor]; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Checks if current position is valid |
132
|
|
|
* |
133
|
|
|
* @return boolean The current position is valid |
134
|
|
|
*/ |
135
|
17 |
|
public function valid() |
136
|
|
|
{ |
137
|
17 |
|
return isset($this->values[$this->cursor]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Rewind the Iterator to the first element |
142
|
|
|
*/ |
143
|
17 |
|
public function rewind() |
144
|
|
|
{ |
145
|
17 |
|
$this->cursor = 0; |
146
|
17 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add a property |
150
|
|
|
* |
151
|
|
|
* @param \stdClass|Iri $property Property |
152
|
|
|
*/ |
153
|
38 |
|
public function add($property) |
154
|
|
|
{ |
155
|
38 |
|
$iri = IriFactory::create($property); |
156
|
38 |
|
$values = (is_object($property) && isset($property->values)) ? (array)$property->values : []; |
157
|
|
|
|
158
|
|
|
// Create the property values list if necessary |
159
|
38 |
|
if (!$this->offsetExists($iri)) { |
160
|
38 |
|
$this->offsetSet($iri, $values); |
161
|
|
|
|
162
|
38 |
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
$propertyValues =& $this->offsetGet($iri); |
166
|
2 |
|
$propertyValues = array_merge($propertyValues, $values); |
167
|
2 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return whether a property exists |
171
|
|
|
* |
172
|
|
|
* @param \stdClass|Iri|string $iri IRI |
173
|
|
|
* |
174
|
|
|
* @return boolean Property exists |
175
|
|
|
*/ |
176
|
38 |
|
public function offsetExists($iri) |
177
|
|
|
{ |
178
|
38 |
|
$iri = IriFactory::create($iri); |
179
|
|
|
try { |
180
|
38 |
|
($iri->profile !== '') ? |
181
|
38 |
|
$this->getProfiledPropertyCursor($iri) : $this->getPropertyCursor($iri->name); |
182
|
|
|
|
183
|
3 |
|
return true; |
184
|
38 |
|
} catch (OutOfBoundsException $exception) { |
185
|
38 |
|
return false; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get a particular property cursor by its profiled name |
191
|
|
|
* |
192
|
|
|
* @param Iri $iri IRI |
193
|
|
|
* |
194
|
|
|
* @return int Property cursor |
195
|
|
|
* @throws OutOfBoundsException If the property name is unknown |
196
|
|
|
*/ |
197
|
32 |
|
protected function getProfiledPropertyCursor($iri) |
198
|
|
|
{ |
199
|
32 |
|
$iriStr = strval($iri); |
200
|
|
|
|
201
|
|
|
// If the property name is unknown |
202
|
32 |
|
if (!isset($this->nameToCursor[$iriStr])) { |
203
|
32 |
|
$this->handleUnknownName($iriStr); |
204
|
|
|
} |
205
|
|
|
|
206
|
6 |
|
return $this->nameToCursor[$iriStr]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Handle an unknown property name |
211
|
|
|
* |
212
|
|
|
* @param string $name Property name |
213
|
|
|
* |
214
|
|
|
* @throws OutOfBoundsException If the property name is unknown |
215
|
|
|
*/ |
216
|
41 |
|
protected function handleUnknownName($name) |
217
|
|
|
{ |
218
|
41 |
|
throw new OutOfBoundsException( |
219
|
41 |
|
sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name), |
220
|
41 |
|
OutOfBoundsException::UNKNOWN_PROPERTY_NAME |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get a particular property cursor by its name |
226
|
|
|
* |
227
|
|
|
* @param string $name Property name |
228
|
|
|
* |
229
|
|
|
* @return int Property cursor |
230
|
|
|
*/ |
231
|
10 |
|
protected function getPropertyCursor($name) |
232
|
|
|
{ |
233
|
|
|
// Run through all property names |
234
|
10 |
|
foreach ($this->names as $cursor => $iri) { |
235
|
5 |
|
if (in_array($name, [$iri->name, strval($iri)])) { |
236
|
4 |
|
return $cursor; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
8 |
|
return $this->handleUnknownName($name); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Set a particular property |
245
|
|
|
* |
246
|
|
|
* @param \stdClass|Iri|string $iri IRI |
247
|
|
|
* @param array $value Property values |
248
|
|
|
*/ |
249
|
9 |
|
public function offsetSet($iri, $value) |
250
|
|
|
{ |
251
|
9 |
|
$iri = IriFactory::create($iri); |
252
|
9 |
|
$iriStr = strval($iri); |
253
|
9 |
|
$cursor = array_key_exists($iriStr, $this->nameToCursor) ? |
254
|
9 |
|
$this->nameToCursor[$iriStr] : ($this->nameToCursor[$iriStr] = count($this->nameToCursor)); |
255
|
9 |
|
$this->names[$cursor] = $iri; |
256
|
9 |
|
$this->values[$cursor] = $value; |
257
|
9 |
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get a particular property |
261
|
|
|
* |
262
|
|
|
* @param \stdClass|Iri|string $iri IRI |
263
|
|
|
* |
264
|
|
|
* @return array Property values |
265
|
|
|
* @throws OutOfBoundsException If the property name is unknown |
266
|
|
|
*/ |
267
|
16 |
|
public function &offsetGet($iri) |
268
|
|
|
{ |
269
|
16 |
|
$iri = IriFactory::create($iri); |
270
|
16 |
|
$cursor = ($iri->profile !== '') ? |
271
|
16 |
|
$this->getProfiledPropertyCursor($iri) : $this->getPropertyCursor($iri->name); |
272
|
|
|
|
273
|
13 |
|
return $this->values[$cursor]; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.