|
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
|
|
|
/******************************************************************************* |
|
91
|
|
|
* PUBLIC METHODS |
|
92
|
|
|
*******************************************************************************/ |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Object URL constructor |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path Object path |
|
98
|
|
|
* @param NULL|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
|
99
|
|
|
* URLs)] |
|
100
|
|
|
* @param string $leader Leading base path |
|
101
|
|
|
* @throws InvalidArgumentException If the date precision is invalid |
|
102
|
|
|
* @throws InvalidArgumentException If the object URL path is invalid |
|
103
|
|
|
*/ |
|
104
|
40 |
|
public function __construct($path, $datePrecision = null, &$leader = '') |
|
105
|
|
|
{ |
|
106
|
|
|
// If the local default date precision should be used |
|
107
|
40 |
|
if ($datePrecision === null) { |
|
108
|
21 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
|
109
|
21 |
|
} |
|
110
|
|
|
|
|
111
|
40 |
|
$pathPattern = null; |
|
112
|
|
|
|
|
113
|
|
|
// If a valid integer date precision is given |
|
114
|
40 |
|
if (is_int($datePrecision) && ($datePrecision >= 0) && ($datePrecision < 7)) { |
|
115
|
|
|
$pathPattern = '%^(?P<leader>(/[^/]+)*)?/'. |
|
116
|
21 |
|
implode( |
|
117
|
21 |
|
'/', |
|
118
|
21 |
|
array_slice(self::$datePattern, 0, $datePrecision) |
|
119
|
21 |
|
).($datePrecision ? '/' : ''); |
|
120
|
|
|
|
|
121
|
|
|
// Else if the date precision may be arbitrary |
|
122
|
40 |
|
} elseif ($datePrecision === true) { |
|
123
|
21 |
|
$pathPattern = '%(?:/'.implode('(?:/', self::$datePattern); |
|
124
|
21 |
|
$pathPattern .= str_repeat(')?', count(self::$datePattern)); |
|
125
|
21 |
|
$pathPattern .= '/'; |
|
126
|
21 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
// If the date precision is invalid |
|
129
|
40 |
|
if ($pathPattern === null) { |
|
130
|
1 |
|
throw new InvalidArgumentException( |
|
131
|
1 |
|
sprintf( |
|
132
|
1 |
|
'Invalid date precision "%s" (%s)', |
|
133
|
1 |
|
strval($datePrecision), |
|
134
|
1 |
|
gettype($datePrecision) |
|
135
|
1 |
|
), |
|
136
|
|
|
InvalidArgumentException::INVALID_DATE_PRECISION |
|
137
|
1 |
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
39 |
|
$pathPattern .= '(?P<id>\d+)\.(?P<type>[a-z]+)(?:/(.*\.)?\\k'; |
|
141
|
39 |
|
$pathPattern .= '<id>(?:-(?P<revision>\d+))?(?P<extension>\.[a-z0-9]+)?)?$%'; |
|
142
|
|
|
|
|
143
|
39 |
|
if (empty($path) || !preg_match($pathPattern, $path, $pathParts)) { |
|
144
|
16 |
|
throw new InvalidArgumentException( |
|
145
|
16 |
|
sprintf( |
|
146
|
16 |
|
'Invalid object URL path "%s"', |
|
147
|
16 |
|
empty($path) ? '(empty)' : $path |
|
148
|
16 |
|
), |
|
149
|
|
|
InvalidArgumentException::INVALID_OBJECT_URL_PATH |
|
150
|
16 |
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// If date components are used |
|
154
|
28 |
|
if ($datePrecision) { |
|
155
|
28 |
|
$year = $pathParts['year']; |
|
156
|
28 |
|
$month = isset($pathParts['month']) ? $pathParts['month'] ?: '01' : '01'; |
|
157
|
28 |
|
$day = isset($pathParts['day']) ? $pathParts['day'] ?: '01' : '01'; |
|
158
|
28 |
|
$hour = isset($pathParts['hour']) ? $pathParts['hour'] ?: '00' : '00'; |
|
159
|
28 |
|
$minute = isset($pathParts['minute']) ? $pathParts['minute'] ?: '00' : '00'; |
|
160
|
28 |
|
$second = isset($pathParts['second']) ? $pathParts['second'] ?: '00' : '00'; |
|
161
|
28 |
|
$this->creationDate = new \DateTimeImmutable("$year-$month-$day".'T'."$hour:$minute:$second+00:00"); |
|
162
|
28 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Determine the leader |
|
165
|
28 |
|
$leader = ($datePrecision === true) ? substr( |
|
166
|
17 |
|
$path, |
|
167
|
17 |
|
0, |
|
168
|
17 |
|
strlen($path) - strlen($pathParts[0]) |
|
169
|
28 |
|
) : $pathParts['leader']; |
|
170
|
|
|
|
|
171
|
|
|
// Set the ID |
|
172
|
|
|
|
|
173
|
28 |
|
$this->uid = Kernel::create(Id::class, [intval($pathParts['id'])]); |
|
174
|
|
|
|
|
175
|
|
|
// Set the type |
|
176
|
28 |
|
$this->type = Kernel::create(Type::class, [$pathParts['type']]); |
|
177
|
|
|
|
|
178
|
|
|
// Set the revision |
|
179
|
28 |
|
$this->revision = Kernel::create( |
|
180
|
28 |
|
Revision::class, |
|
181
|
28 |
|
[empty($pathParts['revision']) ? Revision::CURRENT : intval($pathParts['revision'])] |
|
182
|
28 |
|
); |
|
183
|
28 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Create and return the object URL path |
|
187
|
|
|
* |
|
188
|
|
|
* @return string Object path |
|
189
|
|
|
*/ |
|
190
|
9 |
|
public function __toString() |
|
191
|
|
|
{ |
|
192
|
9 |
|
$path = []; |
|
193
|
9 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
|
194
|
|
|
|
|
195
|
|
|
// Add the creation date |
|
196
|
9 |
|
foreach (array_slice(array_keys(self::$datePattern), 0, $datePrecision) as $dateFormat) { |
|
197
|
9 |
|
$path[] = $this->creationDate->format($dateFormat); |
|
198
|
9 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
// Add the object ID and type |
|
201
|
9 |
|
$path[] = $this->uid->getId().'.'.$this->type->getType(); |
|
202
|
|
|
|
|
203
|
|
|
// Add the ID and revision |
|
204
|
9 |
|
$path[] = rtrim($this->uid->getId().'-'.$this->revision->getRevision(), '-'); |
|
205
|
|
|
|
|
206
|
9 |
|
return '/'.implode('/', $path); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Return the object's creation date |
|
211
|
|
|
* |
|
212
|
|
|
* @return \DateTimeImmutable Object creation date |
|
213
|
|
|
*/ |
|
214
|
7 |
|
public function getCreationDate() |
|
215
|
|
|
{ |
|
216
|
7 |
|
return $this->creationDate; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Set the object's creation date |
|
221
|
|
|
* |
|
222
|
|
|
* @param \DateTimeImmutable $creationDate |
|
223
|
|
|
* @return PathInterface|LocalPath New object path |
|
224
|
|
|
*/ |
|
225
|
|
|
public function setCreationDate(\DateTimeImmutable $creationDate) |
|
226
|
|
|
{ |
|
227
|
|
|
$path = clone $this; |
|
228
|
|
|
$path->creationDate = $creationDate; |
|
229
|
|
|
return $path; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Return the object type |
|
234
|
|
|
* |
|
235
|
|
|
* @return Type Object type |
|
236
|
|
|
*/ |
|
237
|
9 |
|
public function getType() |
|
238
|
|
|
{ |
|
239
|
9 |
|
return $this->type; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Set the object type |
|
244
|
|
|
* |
|
245
|
|
|
* @param Type $type Object type |
|
246
|
|
|
* @return PathInterface|LocalPath New object path |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setType(Type $type) |
|
249
|
|
|
{ |
|
250
|
|
|
$path = clone $this; |
|
251
|
|
|
$path->type = $type; |
|
252
|
|
|
return $path; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Return the object ID |
|
257
|
|
|
* |
|
258
|
|
|
* @return Id Object ID |
|
259
|
|
|
*/ |
|
260
|
15 |
|
public function getId() |
|
261
|
|
|
{ |
|
262
|
15 |
|
return $this->uid; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Set the object ID |
|
267
|
|
|
* |
|
268
|
|
|
* @param Id $uid Object ID |
|
269
|
|
|
* @return PathInterface|LocalPath New object path |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setId(Id $uid) |
|
272
|
|
|
{ |
|
273
|
|
|
$path = clone $this; |
|
274
|
|
|
$path->uid = $uid; |
|
275
|
|
|
return $path; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Return the object revision |
|
280
|
|
|
* |
|
281
|
|
|
* @return Revision Object revision |
|
282
|
|
|
*/ |
|
283
|
7 |
|
public function getRevision() |
|
284
|
|
|
{ |
|
285
|
7 |
|
return $this->revision; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Set the object revision |
|
290
|
|
|
* |
|
291
|
|
|
* @param Revision $revision Object revision |
|
292
|
|
|
* @return PathInterface|LocalPath New object path |
|
293
|
|
|
*/ |
|
294
|
|
|
public function setRevision(Revision $revision) |
|
295
|
|
|
{ |
|
296
|
|
|
$path = clone $this; |
|
297
|
|
|
$path->revision = $revision; |
|
298
|
|
|
return $path; |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|