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\Uri; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\Id; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\Revision; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Object locator |
46
|
|
|
* |
47
|
|
|
* @package Apparat\Object |
48
|
|
|
* @subpackage Apparat\Object\Domain |
49
|
|
|
*/ |
50
|
|
|
class Locator implements LocatorInterface |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Date PCRE pattern |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected static $datePattern = [ |
58
|
|
|
'Y' => '(?P<year>\d{4})', |
59
|
|
|
'm' => '(?P<month>\d{2})', |
60
|
|
|
'd' => '(?P<day>\d{2})', |
61
|
|
|
'H' => '(?P<hour>\d{2})', |
62
|
|
|
'i' => '(?P<minute>\d{2})', |
63
|
|
|
's' => '(?P<second>\d{2})', |
64
|
|
|
]; |
65
|
|
|
/** |
66
|
|
|
* Creation date |
67
|
|
|
* |
68
|
|
|
* @var \DateTimeInterface |
69
|
|
|
*/ |
70
|
|
|
protected $creationDate = null; |
71
|
|
|
/** |
72
|
|
|
* Object ID |
73
|
|
|
* |
74
|
|
|
* @var Id |
75
|
|
|
*/ |
76
|
|
|
protected $uid = null; |
77
|
|
|
/** |
78
|
|
|
* Object type |
79
|
|
|
* |
80
|
|
|
* @var Type |
81
|
|
|
*/ |
82
|
|
|
protected $type = null; |
83
|
|
|
/** |
84
|
|
|
* Object revision |
85
|
|
|
* |
86
|
|
|
* @var Revision |
87
|
|
|
*/ |
88
|
|
|
protected $revision = null; |
89
|
|
|
/** |
90
|
|
|
* Hidden object |
91
|
|
|
* |
92
|
|
|
* @var boolean |
93
|
|
|
*/ |
94
|
|
|
protected $hidden = false; |
95
|
|
|
|
96
|
|
|
/******************************************************************************* |
97
|
|
|
* PUBLIC METHODS |
98
|
|
|
*******************************************************************************/ |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Object URL constructor |
102
|
|
|
* |
103
|
|
|
* @param null|string $locator Object locator |
104
|
|
|
* @param null|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
105
|
|
|
* URLs)] |
106
|
|
|
* @param string $leader Leading base locator |
107
|
|
|
* @throws InvalidArgumentException If the object URL locator is invalid |
108
|
|
|
*/ |
109
|
69 |
|
public function __construct($locator = null, $datePrecision = null, &$leader = '') |
110
|
|
|
{ |
111
|
69 |
|
if (!empty($locator)) { |
112
|
|
|
// If the local default date precision should be used |
113
|
63 |
|
if ($datePrecision === null) { |
114
|
32 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
115
|
32 |
|
} |
116
|
|
|
|
117
|
|
|
// Build the regular expression for matching a local locator |
118
|
63 |
|
$locatorPattern = $this->buildLocatorRegex($datePrecision); |
119
|
|
|
|
120
|
|
|
// Match the local locator |
121
|
62 |
|
if (!preg_match($locatorPattern, $locator, $locatorParts)) { |
122
|
31 |
|
throw new InvalidArgumentException( |
123
|
31 |
|
sprintf('Invalid object URL locator "%s"', $locator), |
124
|
|
|
InvalidArgumentException::INVALID_OBJECT_URL_LOCATOR |
125
|
31 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// If date components are used |
129
|
62 |
|
if ($datePrecision) { |
130
|
62 |
|
$year = $locatorParts['year']; |
131
|
62 |
|
$month = isset($locatorParts['month']) ? $locatorParts['month'] ?: '01' : '01'; |
132
|
62 |
|
$day = isset($locatorParts['day']) ? $locatorParts['day'] ?: '01' : '01'; |
133
|
62 |
|
$hour = isset($locatorParts['hour']) ? $locatorParts['hour'] ?: '00' : '00'; |
134
|
62 |
|
$minute = isset($locatorParts['minute']) ? $locatorParts['minute'] ?: '00' : '00'; |
135
|
62 |
|
$second = isset($locatorParts['second']) ? $locatorParts['second'] ?: '00' : '00'; |
136
|
62 |
|
$this->creationDate = new \DateTimeImmutable("$year-$month-$day".'T'."$hour:$minute:$second+00:00"); |
137
|
62 |
|
} |
138
|
|
|
|
139
|
|
|
// Determine the leader |
140
|
62 |
|
$leader = ($datePrecision === true) ? substr( |
141
|
40 |
|
$locator, |
142
|
40 |
|
0, |
143
|
40 |
|
strlen($locator) - strlen($locatorParts[0]) |
144
|
62 |
|
) : $locatorParts['leader']; |
145
|
|
|
|
146
|
|
|
// Set the hidden state |
147
|
62 |
|
$this->hidden = !empty($locatorParts['hidden']); |
148
|
|
|
|
149
|
|
|
// Set the ID |
150
|
62 |
|
$this->uid = Kernel::create(Id::class, [intval($locatorParts['id'])]); |
151
|
|
|
|
152
|
|
|
// Set the type |
153
|
62 |
|
$this->type = Kernel::create(Type::class, [$locatorParts['type']]); |
154
|
|
|
|
155
|
|
|
// Set the revision |
156
|
62 |
|
$this->revision = Kernel::create( |
157
|
62 |
|
Revision::class, |
158
|
|
|
[ |
159
|
62 |
|
empty($locatorParts['revision']) ? Revision::CURRENT : intval($locatorParts['revision']), |
160
|
62 |
|
!empty($locatorParts['draft']) |
161
|
62 |
|
] |
162
|
62 |
|
); |
163
|
62 |
|
} |
164
|
68 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create and return the object URL locator |
168
|
|
|
* |
169
|
|
|
* @return string Object locator |
170
|
|
|
*/ |
171
|
40 |
|
public function __toString() |
172
|
|
|
{ |
173
|
40 |
|
$locator = []; |
174
|
40 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
175
|
|
|
|
176
|
|
|
// Add the creation date |
177
|
40 |
|
foreach (array_slice(array_keys(self::$datePattern), 0, $datePrecision) as $dateFormat) { |
178
|
40 |
|
$locator[] = $this->creationDate->format($dateFormat); |
179
|
40 |
|
} |
180
|
|
|
|
181
|
|
|
// Add the object ID and type |
182
|
40 |
|
$locator[] = ($this->hidden ? '.' : '').$this->uid->getId().'-'.$this->type->getType(); |
183
|
|
|
|
184
|
|
|
// Add the ID, draft mode and revision |
185
|
40 |
|
$uid = $this->uid->getId(); |
186
|
40 |
|
$locator[] = rtrim(($this->revision->isDraft() ? '.' : '').$uid.'-'.$this->revision->getRevision(), '-'); |
187
|
|
|
|
188
|
40 |
|
return '/'.implode('/', $locator); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return the object's creation date |
193
|
|
|
* |
194
|
|
|
* @return \DateTimeInterface Object creation date |
195
|
|
|
*/ |
196
|
42 |
|
public function getCreationDate() |
197
|
|
|
{ |
198
|
42 |
|
return $this->creationDate; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the object's creation date |
203
|
|
|
* |
204
|
|
|
* @param \DateTimeInterface $creationDate |
205
|
|
|
* @return LocatorInterface|Locator New object locator |
206
|
|
|
*/ |
207
|
7 |
|
public function setCreationDate(\DateTimeInterface $creationDate) |
208
|
|
|
{ |
209
|
7 |
|
$locator = clone $this; |
210
|
7 |
|
$locator->creationDate = $creationDate; |
211
|
7 |
|
return $locator; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Return the object type |
216
|
|
|
* |
217
|
|
|
* @return Type Object type |
218
|
|
|
*/ |
219
|
46 |
|
public function getType() |
220
|
|
|
{ |
221
|
46 |
|
return $this->type; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the object type |
226
|
|
|
* |
227
|
|
|
* @param Type $type Object type |
228
|
|
|
* @return LocatorInterface|Locator New object locator |
229
|
|
|
*/ |
230
|
7 |
|
public function setType(Type $type) |
231
|
|
|
{ |
232
|
7 |
|
$locator = clone $this; |
233
|
7 |
|
$locator->type = $type; |
234
|
7 |
|
return $locator; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return the object ID |
239
|
|
|
* |
240
|
|
|
* @return Id Object ID |
241
|
|
|
*/ |
242
|
45 |
|
public function getId() |
243
|
|
|
{ |
244
|
45 |
|
return $this->uid; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set the object ID |
249
|
|
|
* |
250
|
|
|
* @param Id $uid Object ID |
251
|
|
|
* @return LocatorInterface|Locator New object locator |
252
|
|
|
*/ |
253
|
7 |
|
public function setId(Id $uid) |
254
|
|
|
{ |
255
|
7 |
|
$locator = clone $this; |
256
|
7 |
|
$locator->uid = $uid; |
257
|
7 |
|
return $locator; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return the object revision |
262
|
|
|
* |
263
|
|
|
* @return Revision Object revision |
264
|
|
|
*/ |
265
|
44 |
|
public function getRevision() |
266
|
|
|
{ |
267
|
44 |
|
return $this->revision; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set the object revision |
272
|
|
|
* |
273
|
|
|
* @param Revision $revision Object revision |
274
|
|
|
* @return LocatorInterface|Locator New object locator |
275
|
|
|
*/ |
276
|
41 |
|
public function setRevision(Revision $revision) |
277
|
|
|
{ |
278
|
41 |
|
$locator = clone $this; |
279
|
41 |
|
$locator->revision = $revision; |
280
|
41 |
|
return $locator; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return the object hidden state |
285
|
|
|
* |
286
|
|
|
* @return boolean Object hidden state |
287
|
|
|
*/ |
288
|
2 |
|
public function isHidden() |
289
|
|
|
{ |
290
|
2 |
|
return $this->hidden; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Set the object hidden state |
295
|
|
|
* |
296
|
|
|
* @param boolean $hidden Object hidden state |
297
|
|
|
* @return LocatorInterface|Locator New object locator |
298
|
|
|
*/ |
299
|
38 |
|
public function setHidden($hidden) |
300
|
|
|
{ |
301
|
38 |
|
$locator = clone $this; |
302
|
38 |
|
$locator->hidden = !!$hidden; |
303
|
38 |
|
return $locator; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Build the regular expression for matching a local object locator |
308
|
|
|
* |
309
|
|
|
* @param null|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
310
|
|
|
* URLs)] |
311
|
|
|
* @return string Regular expression for matching a local object locator |
312
|
|
|
* @throws InvalidArgumentException If the date precision is invalid |
313
|
|
|
*/ |
314
|
63 |
|
protected function buildLocatorRegex($datePrecision) |
315
|
|
|
{ |
316
|
63 |
|
$locatorPattern = null; |
317
|
|
|
|
318
|
|
|
// If a valid integer date precision is given |
319
|
63 |
|
if (is_int($datePrecision) && ($datePrecision >= 0) && ($datePrecision < 7)) { |
320
|
|
|
$locatorPattern = '%^(?P<leader>(/[^/]+)*)?/'. |
321
|
32 |
|
implode( |
322
|
32 |
|
'/', |
323
|
32 |
|
array_slice(self::$datePattern, 0, $datePrecision) |
324
|
32 |
|
).($datePrecision ? '/' : ''); |
325
|
|
|
|
326
|
|
|
// Else if the date precision may be arbitrary |
327
|
63 |
|
} elseif ($datePrecision === true) { |
328
|
43 |
|
$locatorPattern = '%(?:/'.implode('(?:/', self::$datePattern); |
329
|
43 |
|
$locatorPattern .= str_repeat(')?', count(self::$datePattern)); |
330
|
43 |
|
$locatorPattern .= '/'; |
331
|
43 |
|
} |
332
|
|
|
|
333
|
|
|
// If the date precision is invalid |
334
|
63 |
|
if ($locatorPattern === null) { |
335
|
1 |
|
throw new InvalidArgumentException( |
336
|
1 |
|
sprintf( |
337
|
1 |
|
'Invalid date precision "%s" (%s)', |
338
|
1 |
|
strval($datePrecision), |
339
|
1 |
|
gettype($datePrecision) |
340
|
1 |
|
), |
341
|
|
|
InvalidArgumentException::INVALID_DATE_PRECISION |
342
|
1 |
|
); |
343
|
|
|
} |
344
|
|
|
|
345
|
62 |
|
$locatorPattern .= '(?P<hidden>\.)?(?P<id>\d+)\-(?P<type>[a-z]+)(?:/(?P<draft>\.)?(.*\.)?'; |
346
|
62 |
|
$locatorPattern .= '\\k<id>(?:-(?P<revision>\d+))?(?P<extension>\.[a-z0-9]+)?)?$%'; |
347
|
|
|
|
348
|
62 |
|
return $locatorPattern; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|