1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object\Domain |
8
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
9
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/*********************************************************************************** |
14
|
|
|
* The MIT License (MIT) |
15
|
|
|
* |
16
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
19
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
20
|
|
|
* the Software without restriction, including without limitation the rights to |
21
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
22
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
23
|
|
|
* subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
31
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
32
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
33
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
34
|
|
|
***********************************************************************************/ |
35
|
|
|
|
36
|
|
|
namespace Apparat\Object\Domain\Model\Uri; |
37
|
|
|
|
38
|
|
|
use Apparat\Kernel\Ports\Kernel; |
39
|
|
|
use Apparat\Object\Domain\Model\Object\Id; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\Revision; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Object URL |
45
|
|
|
* |
46
|
|
|
* @package Apparat\Object\Domain\Model |
47
|
|
|
*/ |
48
|
|
|
class ObjectUrl extends Url implements LocatorInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Object locator |
52
|
|
|
* |
53
|
|
|
* @var Locator |
54
|
|
|
*/ |
55
|
|
|
protected $locator = null; |
56
|
|
|
|
57
|
|
|
/******************************************************************************* |
58
|
|
|
* PUBLIC METHODS |
59
|
|
|
*******************************************************************************/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Object URL constructor |
63
|
|
|
* |
64
|
|
|
* @param string $url Object URL |
65
|
|
|
* @param boolean $remote Accept remote URL (less strict date component checking) |
66
|
|
|
* @throws InvalidArgumentException If remote URLs are not allowed and a remote URL is given |
67
|
|
|
* @throws InvalidArgumentException If the locator component is empty |
68
|
|
|
*/ |
69
|
62 |
|
public function __construct($url, $remote = false) |
70
|
|
|
{ |
71
|
62 |
|
parent::__construct($url); |
72
|
|
|
|
73
|
|
|
// If it's an invalid remote object URL |
74
|
61 |
|
if ($this->isAbsolute() && !$remote) { |
75
|
1 |
|
throw new InvalidArgumentException( |
76
|
1 |
|
sprintf('Unallowed remote object URL "%s"', $url), |
77
|
|
|
InvalidArgumentException::UNALLOWED_REMOTE_OBJECT_URL |
78
|
1 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// If the locator component is empty |
82
|
60 |
|
if (empty($this->urlParts['path'])) { |
83
|
33 |
|
throw new InvalidArgumentException( |
84
|
33 |
|
'Invalid object URL path (empty)', |
85
|
|
|
InvalidArgumentException::INVALID_OBJECT_URL_LOCATOR |
86
|
33 |
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Instantiate the local locator component |
90
|
58 |
|
$this->locator = new Locator( |
91
|
58 |
|
$this->urlParts['path'], |
92
|
58 |
|
$remote ? true : null, |
93
|
58 |
|
$this->urlParts['path'] |
94
|
58 |
|
); |
95
|
|
|
|
96
|
|
|
// Normalize the locator prefix |
97
|
55 |
|
if (!strlen($this->urlParts['path'])) { |
98
|
44 |
|
$this->urlParts['path'] = null; |
99
|
44 |
|
} |
100
|
55 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the object's creation date |
104
|
|
|
* |
105
|
|
|
* @param \DateTimeInterface $creationDate |
106
|
|
|
* @return LocatorInterface|ObjectUrl New object locator |
107
|
|
|
*/ |
108
|
1 |
|
public function setCreationDate(\DateTimeInterface $creationDate) |
109
|
|
|
{ |
110
|
1 |
|
$this->locator = $this->locator->setCreationDate($creationDate); |
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the object type |
116
|
|
|
* |
117
|
|
|
* @param Type $type Object type |
118
|
|
|
* @return LocatorInterface|ObjectUrl New object URL |
119
|
|
|
*/ |
120
|
2 |
|
public function setType(Type $type) |
121
|
|
|
{ |
122
|
2 |
|
$this->locator = $this->locator->setType($type); |
123
|
1 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the object ID |
128
|
|
|
* |
129
|
|
|
* @param Id $uid Object ID |
130
|
|
|
* @return LocatorInterface|ObjectUrl New object URL |
131
|
|
|
*/ |
132
|
1 |
|
public function setId(Id $uid) |
133
|
|
|
{ |
134
|
1 |
|
$this->locator = $this->locator->setId($uid); |
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set the object revision |
140
|
|
|
* |
141
|
|
|
* @param Revision $revision Object revision |
142
|
|
|
* @return LocatorInterface|ObjectUrl New object URL |
143
|
|
|
*/ |
144
|
1 |
|
public function setRevision(Revision $revision) |
145
|
|
|
{ |
146
|
1 |
|
$this->locator = $this->locator->setRevision($revision); |
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Test if this URL matches all available parts of a given URL |
152
|
|
|
* |
153
|
|
|
* @param Url $url Comparison URL |
154
|
|
|
* @return bool This URL matches all available parts of the given URL |
155
|
|
|
*/ |
156
|
8 |
|
public function matches(Url $url) |
157
|
|
|
{ |
158
|
|
|
// If the standard URL components don't match |
159
|
8 |
|
if (!parent::matches($url)) { |
160
|
5 |
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Extended tests if it's an object URL |
164
|
5 |
|
if ($url instanceof self) { |
165
|
|
|
// Test the object creation date |
166
|
1 |
|
if ($this->getCreationDate() != $url->getCreationDate()) { |
167
|
1 |
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Test the object ID |
171
|
1 |
|
if ($this->getId()->serialize() !== $url->getId()->serialize()) { |
172
|
1 |
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Test the object type |
176
|
1 |
|
if ($this->getType()->serialize() !== $url->getType()->serialize()) { |
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Test the object revision |
181
|
1 |
|
if ($this->getRevision()->serialize() !== $url->getRevision()->serialize()) { |
182
|
1 |
|
return false; |
183
|
|
|
} |
184
|
1 |
|
} |
185
|
|
|
|
186
|
5 |
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return the object's creation date |
191
|
|
|
* |
192
|
|
|
* @return \DateTimeInterface Object creation date |
193
|
|
|
*/ |
194
|
25 |
|
public function getCreationDate() |
195
|
|
|
{ |
196
|
25 |
|
return $this->locator->getCreationDate(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Return the object ID |
201
|
|
|
* |
202
|
|
|
* @return Id Object ID |
203
|
|
|
*/ |
204
|
25 |
|
public function getId() |
205
|
|
|
{ |
206
|
25 |
|
return $this->locator->getId(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return the object type |
211
|
|
|
* |
212
|
|
|
* @return Type Object type |
213
|
|
|
*/ |
214
|
27 |
|
public function getType() |
215
|
|
|
{ |
216
|
27 |
|
return $this->locator->getType(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return the object revision |
221
|
|
|
* |
222
|
|
|
* @return Revision Object revision |
223
|
|
|
*/ |
224
|
25 |
|
public function getRevision() |
225
|
|
|
{ |
226
|
25 |
|
return $this->locator->getRevision(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the local object locator |
231
|
|
|
* |
232
|
|
|
* @return LocatorInterface|Locator Local object locator |
233
|
|
|
*/ |
234
|
1 |
|
public function getLocator() |
235
|
|
|
{ |
236
|
1 |
|
return $this->locator; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Return the object draft mode |
241
|
|
|
* |
242
|
|
|
* @return boolean Object draft mode |
243
|
|
|
*/ |
244
|
2 |
|
public function isDraft() |
245
|
|
|
{ |
246
|
2 |
|
return $this->locator->getRevision()->isDraft(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set the object draft mode |
251
|
|
|
* |
252
|
|
|
* @param boolean $draft Object draft mode |
253
|
|
|
* @return LocatorInterface|ObjectUrl New object locator |
254
|
|
|
*/ |
255
|
1 |
|
public function setDraft($draft) |
256
|
|
|
{ |
257
|
1 |
|
$this->locator = $this->locator->setRevision($this->locator->getRevision()->setDraft($draft)); |
258
|
1 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return the repository URL part of this object URL |
263
|
|
|
* |
264
|
|
|
* @return string Repository URL |
265
|
|
|
* @see https://github.com/apparat/apparat/blob/master/doc/URL-DESIGN.md#repository-url |
266
|
|
|
*/ |
267
|
26 |
|
public function getRepositoryUrl() |
268
|
|
|
{ |
269
|
|
|
// If the object URL is absolute and local: Extract the repository URL |
270
|
26 |
|
if ($this->isAbsoluteLocal()) { |
271
|
2 |
|
$baseUrl = Kernel::create(Url::class, [getenv('APPARAT_BASE_URL')]); |
272
|
2 |
|
return substr($this->getPath(), strlen($baseUrl->getPath())); |
273
|
|
|
|
274
|
|
|
// Else: If it's a relative URL: Extract the repository URL |
275
|
24 |
|
} elseif (!$this->isAbsolute()) { |
276
|
21 |
|
return $this->getPath(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// Else: It must be a remote repository |
280
|
|
|
$override = [ |
281
|
3 |
|
'object' => '', |
282
|
3 |
|
'query' => '', |
283
|
3 |
|
'fragment' => '', |
284
|
3 |
|
]; |
285
|
3 |
|
return $this->getUrlInternal($override); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Return the object hidden state |
291
|
|
|
* |
292
|
|
|
* @return boolean Object hidden state |
293
|
|
|
*/ |
294
|
1 |
|
public function isHidden() |
295
|
|
|
{ |
296
|
1 |
|
return $this->locator->isHidden(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set the object hidden state |
301
|
|
|
* |
302
|
|
|
* @param boolean $hidden Object hidden state |
303
|
|
|
* @return LocatorInterface|Locator New object locator |
304
|
|
|
*/ |
305
|
1 |
|
public function setHidden($hidden) |
306
|
|
|
{ |
307
|
1 |
|
$this->locator = $this->locator->setHidden($hidden); |
308
|
1 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Return the a complete serialized object URL |
313
|
|
|
* |
314
|
|
|
* @param array $override Override components |
315
|
|
|
* @return string Serialized URL |
316
|
|
|
*/ |
317
|
34 |
|
protected function getUrlInternal(array &$override = []) |
318
|
|
|
{ |
319
|
34 |
|
parent::getUrlInternal($override); |
320
|
|
|
|
321
|
|
|
// Prepare the local object locator |
322
|
34 |
|
$override['object'] = isset($override['object']) ? $override['object'] : strval($this->locator); |
323
|
|
|
|
324
|
34 |
|
return "{$override['scheme']}{$override['user']}{$override['pass']}{$override['host']}{$override['port']}". |
325
|
34 |
|
"{$override['path']}{$override['object']}{$override['query']}{$override['fragment']}"; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|