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