1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Domain |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 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 © 2016 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 Apparat\Object\Domain\Model\Object; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Domain\Model\Author\AuthorInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Path\ApparatUrl; |
42
|
|
|
use Apparat\Object\Domain\Model\Path\PathInterface; |
43
|
|
|
use Apparat\Object\Domain\Repository\Service; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Object proxy (lazy loading) |
47
|
|
|
* |
48
|
|
|
* @package Apparat\Object |
49
|
|
|
* @subpackage Apparat\Object\Domain |
50
|
|
|
*/ |
51
|
|
|
class ObjectProxy implements ObjectInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Apparat object URL |
55
|
|
|
* |
56
|
|
|
* @var ApparatUrl |
57
|
|
|
*/ |
58
|
|
|
protected $url = null; |
59
|
|
|
/** |
60
|
|
|
* Object |
61
|
|
|
* |
62
|
|
|
* @var ObjectInterface |
63
|
|
|
*/ |
64
|
|
|
protected $object = null; |
65
|
|
|
|
66
|
|
|
/******************************************************************************* |
67
|
|
|
* PUBLIC METHODS |
68
|
|
|
*******************************************************************************/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Object proxy constructor |
72
|
|
|
* |
73
|
|
|
* @param ApparatUrl $url Apparat object URL |
74
|
|
|
*/ |
75
|
12 |
|
public function __construct(ApparatUrl $url) |
76
|
|
|
{ |
77
|
12 |
|
$this->url = $url; |
78
|
12 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the object repository path |
82
|
|
|
* |
83
|
|
|
* @return PathInterface Object repository path |
84
|
|
|
*/ |
85
|
|
|
public function getRepositoryPath() |
86
|
|
|
{ |
87
|
|
|
// If the object has already been instantiated |
88
|
|
|
if ($this->object instanceof ObjectInterface) { |
89
|
|
|
return $this->object->getRepositoryPath(); |
90
|
|
|
|
91
|
|
|
// Else |
92
|
|
|
} else { |
93
|
|
|
return $this->url->getLocalPath(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the object property data |
99
|
|
|
* |
100
|
|
|
* @return array Object property data |
101
|
|
|
*/ |
102
|
|
|
public function getPropertyData() |
103
|
|
|
{ |
104
|
|
|
return $this->object()->getPropertyData(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the object payload |
109
|
|
|
* |
110
|
|
|
* @return string Object payload |
111
|
|
|
*/ |
112
|
|
|
public function getPayload() |
113
|
|
|
{ |
114
|
|
|
return $this->object()->getPayload(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return the object ID |
119
|
|
|
* |
120
|
|
|
* @return Id Object ID |
121
|
|
|
*/ |
122
|
|
|
public function getId() |
123
|
|
|
{ |
124
|
|
|
return $this->object()->getId(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return the enclosed remote object |
129
|
|
|
* |
130
|
|
|
* @return ObjectInterface Remote object |
131
|
|
|
*/ |
132
|
|
|
protected function object() |
133
|
|
|
{ |
134
|
|
|
// Lazy-load the remote object if necessary |
135
|
|
|
if (!$this->object instanceof ObjectInterface) { |
136
|
|
|
// Instantiate the local object repository, load and return the object |
137
|
|
|
$this->object = Kernel::create(Service::class)->get($this->url)->loadObject($this->url->getLocalPath()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->object; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return the object type |
145
|
|
|
* |
146
|
|
|
* @return Type Object type |
147
|
|
|
*/ |
148
|
|
|
public function getType() |
149
|
|
|
{ |
150
|
|
|
return $this->object()->getType(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return the object revision |
155
|
|
|
* |
156
|
|
|
* @return Revision Object revision |
157
|
|
|
*/ |
158
|
|
|
public function getRevision() |
159
|
|
|
{ |
160
|
|
|
return $this->object()->getRevision(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return the creation date & time |
165
|
|
|
* |
166
|
|
|
* @return \DateTimeImmutable Creation date & time |
167
|
|
|
*/ |
168
|
|
|
public function getCreated() |
169
|
|
|
{ |
170
|
|
|
return $this->object()->getCreated(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return the publication date & time |
175
|
|
|
* |
176
|
|
|
* @return \DateTimeImmutable Publication date & time |
177
|
|
|
*/ |
178
|
|
|
public function getPublished() |
179
|
|
|
{ |
180
|
|
|
return $this->object()->getPublished(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the object hash |
185
|
|
|
* |
186
|
|
|
* @return string Object hash |
187
|
|
|
*/ |
188
|
|
|
public function getHash() |
189
|
|
|
{ |
190
|
|
|
return $this->object()->getHash(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return the object description |
195
|
|
|
* |
196
|
|
|
* @return string Object description |
197
|
|
|
*/ |
198
|
|
|
public function getDescription() |
199
|
|
|
{ |
200
|
|
|
return $this->object()->getDescription(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Return the object abstract |
205
|
|
|
* |
206
|
|
|
* @return string Object abstract |
207
|
|
|
*/ |
208
|
|
|
public function getAbstract() |
209
|
|
|
{ |
210
|
|
|
return $this->object()->getAbstract(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Return all object keywords |
215
|
|
|
* |
216
|
|
|
* @return array Object keywords |
217
|
|
|
*/ |
218
|
|
|
public function getKeywords() |
219
|
|
|
{ |
220
|
|
|
return $this->object()->getKeywords(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return all object categories |
225
|
|
|
* |
226
|
|
|
* @return array Object categories |
227
|
|
|
*/ |
228
|
|
|
public function getCategories() |
229
|
|
|
{ |
230
|
|
|
return $this->object()->getCategories(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return all object authors |
235
|
|
|
* |
236
|
|
|
* @return AuthorInterface[] Authors |
237
|
|
|
*/ |
238
|
|
|
public function getAuthors() |
239
|
|
|
{ |
240
|
|
|
return $this->object()->getAuthors(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Add an object author |
245
|
|
|
* |
246
|
|
|
* @param AuthorInterface $author Author |
247
|
|
|
* @return ObjectInterface Self reference |
248
|
|
|
*/ |
249
|
|
|
public function addAuthor(AuthorInterface $author) |
250
|
|
|
{ |
251
|
|
|
return $this->object()->addAuthor($author); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get a particular property value |
256
|
|
|
* |
257
|
|
|
* Multi-level properties might be traversed by property name paths separated with colons (":"). |
258
|
|
|
* |
259
|
|
|
* @param string $property Property name |
260
|
|
|
* @return mixed Property value |
261
|
|
|
*/ |
262
|
|
|
public function getDomainProperty($property) |
263
|
|
|
{ |
264
|
|
|
return $this->object()->getDomainProperty($property); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Return the absolute object URL |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
11 |
|
public function getAbsoluteUrl() |
273
|
|
|
{ |
274
|
11 |
|
return strval($this->url); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Generic caller |
279
|
|
|
* |
280
|
|
|
* @param string $name Method name |
281
|
|
|
* @param array $arguments Method arguments |
282
|
|
|
*/ |
283
|
|
|
public function __call($name, $arguments) |
284
|
|
|
{ |
285
|
|
|
$object = $this->object(); |
286
|
|
|
if (is_callable(array($object, $name))) { |
287
|
|
|
return $object->$name(...$arguments); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
throw new InvalidArgumentException( |
291
|
|
|
sprintf('Invalid object proxy method "%s"', $name), |
292
|
|
|
InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD |
293
|
|
|
); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Use a specific object revision |
298
|
|
|
* |
299
|
|
|
* @param Revision $revision Revision to be used |
300
|
|
|
* @return ObjectInterface Object |
301
|
|
|
*/ |
302
|
|
|
public function useRevision(Revision $revision) |
303
|
|
|
{ |
304
|
|
|
return $this->object()->useRevision($revision); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
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.