1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* rdfa-lite |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Rdfalite |
8
|
|
|
* @subpackage Jkphl\Rdfalite\Domain |
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\Rdfalite\Domain\Thing; |
38
|
|
|
|
39
|
|
|
use Jkphl\Rdfalite\Domain\Exceptions\OutOfBoundsException; |
40
|
|
|
use Jkphl\Rdfalite\Domain\Exceptions\RuntimeException; |
41
|
|
|
use Jkphl\Rdfalite\Domain\Property\Property; |
42
|
|
|
use Jkphl\Rdfalite\Domain\Property\PropertyInterface; |
43
|
|
|
use Jkphl\Rdfalite\Domain\Vocabulary\VocabularyInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Thing |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\Rdfalite |
49
|
|
|
* @subpackage Jkphl\Rdfalite\Domain |
50
|
|
|
*/ |
51
|
|
|
class Thing implements ThingInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Resource type |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $type; |
59
|
|
|
/** |
60
|
|
|
* Resource vocabulary |
61
|
|
|
* |
62
|
|
|
* @var VocabularyInterface |
63
|
|
|
*/ |
64
|
|
|
protected $vocabulary; |
65
|
|
|
/** |
66
|
|
|
* Resource ID |
67
|
|
|
* |
68
|
|
|
* @var string|null |
69
|
|
|
*/ |
70
|
|
|
protected $resourceId = null; |
71
|
|
|
/** |
72
|
|
|
* Child things |
73
|
|
|
* |
74
|
|
|
* @var ThingInterface[] |
75
|
|
|
*/ |
76
|
|
|
protected $children = []; |
77
|
|
|
/** |
78
|
|
|
* Property |
79
|
|
|
* |
80
|
|
|
* @var array[] |
81
|
|
|
*/ |
82
|
|
|
protected $properties = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Thing constructor |
86
|
|
|
* |
87
|
|
|
* @param string $type Resource type |
88
|
|
|
* @param VocabularyInterface $vocabulary Vocabulary in use |
89
|
|
|
* @param null|string $resourceId Resource id |
90
|
|
|
*/ |
91
|
8 |
|
public function __construct($type, VocabularyInterface $vocabulary, $resourceId = null) |
92
|
|
|
{ |
93
|
8 |
|
$type = trim($type); |
94
|
8 |
|
if (!strlen($type)) { |
95
|
1 |
|
throw new RuntimeException( |
96
|
1 |
|
sprintf(RuntimeException::INVALID_RESOURCE_TYPE_STR, $type, $vocabulary->getUri()), |
97
|
|
|
RuntimeException::INVALID_RESOURCE_TYPE |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$this->vocabulary = $vocabulary; |
102
|
7 |
|
$this->type = $this->vocabulary->expand($type); |
103
|
7 |
|
$this->resourceId = $resourceId; |
104
|
7 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the resource type |
108
|
|
|
* |
109
|
|
|
* @return string Resource type |
110
|
|
|
*/ |
111
|
1 |
|
public function getType() |
112
|
|
|
{ |
113
|
1 |
|
return $this->type; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the vocabulary in use |
118
|
|
|
* |
119
|
|
|
* @return VocabularyInterface Vocabulary |
120
|
|
|
*/ |
121
|
1 |
|
public function getVocabulary() |
122
|
|
|
{ |
123
|
1 |
|
return $this->vocabulary; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the resource ID |
128
|
|
|
* |
129
|
|
|
* @return null|string Resource ID |
130
|
|
|
*/ |
131
|
2 |
|
public function getResourceId() |
132
|
|
|
{ |
133
|
2 |
|
return $this->resourceId; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add a property value |
138
|
|
|
* |
139
|
|
|
* @param PropertyInterface $property Property |
140
|
|
|
* @return Thing Self reference |
141
|
|
|
*/ |
142
|
1 |
|
public function addProperty(PropertyInterface $property) |
143
|
|
|
{ |
144
|
|
|
// Create the property values list if necessary |
145
|
1 |
|
if (!array_key_exists($property->getName(), $this->properties)) { |
146
|
1 |
|
$this->properties[$property->getName()] = []; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
// Register the property value |
150
|
1 |
|
$this->properties[$property->getName()][] = $property; |
151
|
|
|
|
152
|
1 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return all properties |
157
|
|
|
* |
158
|
|
|
* @return array[] Properties |
159
|
|
|
*/ |
160
|
2 |
|
public function getProperties() |
161
|
|
|
{ |
162
|
2 |
|
return $this->properties; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return the values of a single property |
167
|
|
|
* |
168
|
|
|
* @param string $name Property name |
169
|
|
|
* @return array Property values |
170
|
|
|
*/ |
171
|
2 |
|
public function getProperty($name) |
172
|
|
|
{ |
173
|
2 |
|
$name = Property::validatePropertyName($name); |
174
|
|
|
|
175
|
|
|
// If the property name is unknown |
176
|
2 |
|
if (!array_key_exists($name, $this->properties)) { |
177
|
1 |
|
throw new OutOfBoundsException( |
178
|
1 |
|
sprintf(OutOfBoundsException::UNKNOWN_PROPERTY_NAME_STR, $name), |
179
|
|
|
OutOfBoundsException::UNKNOWN_PROPERTY_NAME |
180
|
1 |
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
return $this->properties[$name]; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add a child |
188
|
|
|
* |
189
|
|
|
* @param ThingInterface $child Child |
190
|
|
|
* @return Thing Self reference |
191
|
|
|
*/ |
192
|
2 |
|
public function addChild(ThingInterface $child) |
193
|
|
|
{ |
194
|
2 |
|
$this->children[spl_object_hash($child)] = $child; |
195
|
2 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return all children |
200
|
|
|
* |
201
|
|
|
* @return ThingInterface[] Children |
202
|
|
|
*/ |
203
|
3 |
|
public function getChildren() |
204
|
|
|
{ |
205
|
3 |
|
return array_values($this->children); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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.