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\Path; |
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 path |
46
|
|
|
* |
47
|
|
|
* @package Apparat\Object |
48
|
|
|
* @subpackage Apparat\Object\Domain |
49
|
|
|
*/ |
50
|
|
|
class LocalPath implements PathInterface |
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 \DateTimeImmutable |
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 $path Object path |
104
|
|
|
* @param null|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
105
|
|
|
* URLs)] |
106
|
|
|
* @param string $leader Leading base path |
107
|
|
|
* @throws InvalidArgumentException If the object URL path is invalid |
108
|
|
|
*/ |
109
|
52 |
|
public function __construct($path = null, $datePrecision = null, &$leader = '') |
110
|
|
|
{ |
111
|
52 |
|
if (!empty($path)) { |
112
|
|
|
// If the local default date precision should be used |
113
|
51 |
|
if ($datePrecision === null) { |
114
|
26 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
115
|
26 |
|
} |
116
|
|
|
|
117
|
|
|
// Build the regular expression for matching a local path |
118
|
51 |
|
$pathPattern = $this->buildPathRegex($datePrecision); |
119
|
|
|
|
120
|
|
|
// Match the local path |
121
|
50 |
|
if (!preg_match($pathPattern, $path, $pathParts)) { |
122
|
23 |
|
throw new InvalidArgumentException( |
123
|
23 |
|
sprintf('Invalid object URL path "%s"', $path), |
124
|
|
|
InvalidArgumentException::INVALID_OBJECT_URL_PATH |
125
|
23 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// If date components are used |
129
|
50 |
|
if ($datePrecision) { |
130
|
50 |
|
$year = $pathParts['year']; |
131
|
50 |
|
$month = isset($pathParts['month']) ? $pathParts['month'] ?: '01' : '01'; |
132
|
50 |
|
$day = isset($pathParts['day']) ? $pathParts['day'] ?: '01' : '01'; |
133
|
50 |
|
$hour = isset($pathParts['hour']) ? $pathParts['hour'] ?: '00' : '00'; |
134
|
50 |
|
$minute = isset($pathParts['minute']) ? $pathParts['minute'] ?: '00' : '00'; |
135
|
50 |
|
$second = isset($pathParts['second']) ? $pathParts['second'] ?: '00' : '00'; |
136
|
50 |
|
$this->creationDate = new \DateTimeImmutable("$year-$month-$day".'T'."$hour:$minute:$second+00:00"); |
137
|
50 |
|
} |
138
|
|
|
|
139
|
|
|
// Determine the leader |
140
|
50 |
|
$leader = ($datePrecision === true) ? substr( |
141
|
32 |
|
$path, |
142
|
32 |
|
0, |
143
|
32 |
|
strlen($path) - strlen($pathParts[0]) |
144
|
50 |
|
) : $pathParts['leader']; |
145
|
|
|
|
146
|
|
|
// Set the hidden state |
147
|
50 |
|
$this->hidden = !empty($pathParts['hidden']); |
148
|
|
|
|
149
|
|
|
// Set the ID |
150
|
50 |
|
$this->uid = Kernel::create(Id::class, [intval($pathParts['id'])]); |
151
|
|
|
|
152
|
|
|
// Set the type |
153
|
50 |
|
$this->type = Kernel::create(Type::class, [$pathParts['type']]); |
154
|
|
|
|
155
|
|
|
// Set the revision |
156
|
50 |
|
$this->revision = Kernel::create( |
157
|
50 |
|
Revision::class, |
158
|
|
|
[ |
159
|
50 |
|
empty($pathParts['revision']) ? Revision::CURRENT : intval($pathParts['revision']), |
160
|
50 |
|
!empty($pathParts['draft']) |
161
|
50 |
|
] |
162
|
50 |
|
); |
163
|
50 |
|
} |
164
|
51 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create and return the object URL path |
168
|
|
|
* |
169
|
|
|
* @return string Object path |
170
|
|
|
*/ |
171
|
27 |
|
public function __toString() |
172
|
|
|
{ |
173
|
27 |
|
$path = []; |
174
|
27 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
175
|
|
|
|
176
|
|
|
// Add the creation date |
177
|
27 |
|
foreach (array_slice(array_keys(self::$datePattern), 0, $datePrecision) as $dateFormat) { |
178
|
27 |
|
$path[] = $this->creationDate->format($dateFormat); |
179
|
27 |
|
} |
180
|
|
|
|
181
|
|
|
// Add the object ID and type |
182
|
27 |
|
$path[] = ($this->hidden ? '.' : '').$this->uid->getId().'.'.$this->type->getType(); |
183
|
|
|
|
184
|
|
|
// Add the ID, draft mode and revision |
185
|
27 |
|
$uid = $this->uid->getId(); |
186
|
27 |
|
$path[] = rtrim(($this->revision->isDraft() ? '.' : '').$uid.'-'.$this->revision->getRevision(), '-'); |
187
|
|
|
|
188
|
27 |
|
return '/'.implode('/', $path); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return the object's creation date |
193
|
|
|
* |
194
|
|
|
* @return \DateTimeImmutable Object creation date |
195
|
|
|
*/ |
196
|
28 |
|
public function getCreationDate() |
197
|
|
|
{ |
198
|
28 |
|
return $this->creationDate; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the object's creation date |
203
|
|
|
* |
204
|
|
|
* @param \DateTimeImmutable $creationDate |
205
|
|
|
* @return PathInterface|LocalPath New object path |
206
|
|
|
*/ |
207
|
2 |
|
public function setCreationDate(\DateTimeImmutable $creationDate) |
208
|
|
|
{ |
209
|
2 |
|
$path = clone $this; |
210
|
2 |
|
$path->creationDate = $creationDate; |
211
|
2 |
|
return $path; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Return the object type |
216
|
|
|
* |
217
|
|
|
* @return Type Object type |
218
|
|
|
*/ |
219
|
30 |
|
public function getType() |
220
|
|
|
{ |
221
|
30 |
|
return $this->type; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the object type |
226
|
|
|
* |
227
|
|
|
* @param Type $type Object type |
228
|
|
|
* @return PathInterface|LocalPath New object path |
229
|
|
|
*/ |
230
|
2 |
|
public function setType(Type $type) |
231
|
|
|
{ |
232
|
2 |
|
$path = clone $this; |
233
|
2 |
|
$path->type = $type; |
234
|
2 |
|
return $path; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return the object ID |
239
|
|
|
* |
240
|
|
|
* @return Id Object ID |
241
|
|
|
*/ |
242
|
31 |
|
public function getId() |
243
|
|
|
{ |
244
|
31 |
|
return $this->uid; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set the object ID |
249
|
|
|
* |
250
|
|
|
* @param Id $uid Object ID |
251
|
|
|
* @return PathInterface|LocalPath New object path |
252
|
|
|
*/ |
253
|
2 |
|
public function setId(Id $uid) |
254
|
|
|
{ |
255
|
2 |
|
$path = clone $this; |
256
|
2 |
|
$path->uid = $uid; |
257
|
2 |
|
return $path; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return the object revision |
262
|
|
|
* |
263
|
|
|
* @return Revision Object revision |
264
|
|
|
*/ |
265
|
30 |
|
public function getRevision() |
266
|
|
|
{ |
267
|
30 |
|
return $this->revision; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set the object revision |
272
|
|
|
* |
273
|
|
|
* @param Revision $revision Object revision |
274
|
|
|
* @return PathInterface|LocalPath New object path |
275
|
|
|
*/ |
276
|
27 |
|
public function setRevision(Revision $revision) |
277
|
|
|
{ |
278
|
27 |
|
$path = clone $this; |
279
|
27 |
|
$path->revision = $revision; |
280
|
27 |
|
return $path; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return the object hidden state |
285
|
|
|
* |
286
|
|
|
* @return boolean Object hidden state |
287
|
|
|
*/ |
288
|
|
|
public function isHidden() |
289
|
|
|
{ |
290
|
|
|
return $this->hidden; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Set the object hidden state |
295
|
|
|
* |
296
|
|
|
* @param boolean $hidden Object hidden state |
297
|
|
|
* @return PathInterface|LocalPath New object path |
298
|
|
|
*/ |
299
|
|
|
public function setHidden($hidden) |
300
|
|
|
{ |
301
|
|
|
$path = clone $this; |
302
|
|
|
$path->hidden = !!$hidden; |
303
|
|
|
return $path; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Build the regular expression for matching a local object path |
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 path |
312
|
|
|
* @throws InvalidArgumentException If the date precision is invalid |
313
|
|
|
*/ |
314
|
51 |
|
protected function buildPathRegex($datePrecision) |
315
|
|
|
{ |
316
|
51 |
|
$pathPattern = null; |
317
|
|
|
|
318
|
|
|
// If a valid integer date precision is given |
319
|
51 |
|
if (is_int($datePrecision) && ($datePrecision >= 0) && ($datePrecision < 7)) { |
320
|
|
|
$pathPattern = '%^(?P<leader>(/[^/]+)*)?/'. |
321
|
26 |
|
implode( |
322
|
26 |
|
'/', |
323
|
26 |
|
array_slice(self::$datePattern, 0, $datePrecision) |
324
|
26 |
|
).($datePrecision ? '/' : ''); |
325
|
|
|
|
326
|
|
|
// Else if the date precision may be arbitrary |
327
|
51 |
|
} elseif ($datePrecision === true) { |
328
|
33 |
|
$pathPattern = '%(?:/'.implode('(?:/', self::$datePattern); |
329
|
33 |
|
$pathPattern .= str_repeat(')?', count(self::$datePattern)); |
330
|
33 |
|
$pathPattern .= '/'; |
331
|
33 |
|
} |
332
|
|
|
|
333
|
|
|
// If the date precision is invalid |
334
|
51 |
|
if ($pathPattern === 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
|
50 |
|
$pathPattern .= '(?P<hidden>\.)?(?P<id>\d+)\.(?P<type>[a-z]+)(?:/(.*\.)?\\k'; |
346
|
50 |
|
$pathPattern .= '<id>(?:(?P<draft>\+)|(?:-(?P<revision>\d+)))?(?P<extension>\.[a-z0-9]+)?)?$%'; |
347
|
|
|
|
348
|
50 |
|
return $pathPattern; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|