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\Factory; |
38
|
|
|
|
39
|
|
|
use Apparat\Object\Domain\Model\Object\Revision; |
40
|
|
|
use Apparat\Object\Domain\Repository\InvalidArgumentException; |
41
|
|
|
use Apparat\Object\Domain\Repository\Selector as RepositorySelector; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Object selector factory |
45
|
|
|
* |
46
|
|
|
* @package Apparat\Object |
47
|
|
|
* @subpackage Apparat\Object\Domain |
48
|
|
|
*/ |
49
|
|
|
class SelectorFactory |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Date PCRE pattern |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
* @see ObjectUrl::$datePattern |
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
|
|
|
/** |
67
|
|
|
* Parse and instantiate an object selector |
68
|
|
|
* |
69
|
|
|
* @param string $selector String selector |
70
|
|
|
* @return \Apparat\Object\Domain\Repository\Selector Object selector |
71
|
|
|
* @throws InvalidArgumentException If the selector is invalid |
72
|
|
|
*/ |
73
|
11 |
|
public static function createFromString($selector) |
74
|
|
|
{ |
75
|
11 |
|
$datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
76
|
11 |
|
$selectorPattern = '/(?P<id>(?:\d+)|\*)\.(?P<type>(?:[a-z]+)|\*)(?:/\\k<id>(?:-(?P<revision>\d+))?)?'; |
77
|
|
|
|
78
|
|
|
// If the creation date is used as selector component |
79
|
11 |
|
if ($datePrecision) { |
80
|
11 |
|
$selectorPattern = implode( |
81
|
11 |
|
'', array_slice( |
82
|
11 |
|
self::$datePattern, 0, |
83
|
|
|
$datePrecision |
84
|
11 |
|
) |
|
|
|
|
85
|
11 |
|
).'(?:'.$selectorPattern.str_repeat( |
86
|
11 |
|
')?', |
87
|
|
|
$datePrecision |
88
|
11 |
|
); |
89
|
11 |
|
} |
90
|
11 |
|
$selectorPattern = '%^'.$selectorPattern.'$%'; |
91
|
|
|
|
92
|
|
|
// If the selector is invalid |
93
|
11 |
|
if (!strlen($selector) || !preg_match( |
94
|
11 |
|
$selectorPattern, $selector, |
95
|
|
|
$selectorParts |
96
|
11 |
|
) || !strlen($selectorParts[0]) |
97
|
11 |
|
) { |
98
|
1 |
|
throw new InvalidArgumentException( |
99
|
1 |
|
sprintf('Invalid respository selector "%s"', $selector), |
100
|
|
|
InvalidArgumentException::INVALID_REPOSITORY_SELECTOR |
101
|
1 |
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
10 |
|
$year = $month = $day = $hour = $minute = $second = $uid = null; |
|
|
|
|
105
|
10 |
|
if (($datePrecision > 0)) { |
106
|
10 |
|
$year = isset($selectorParts['year']) ? self::castInt( |
107
|
10 |
|
$selectorParts['year'] |
108
|
10 |
|
) : RepositorySelector::WILDCARD; |
109
|
10 |
|
} |
110
|
10 |
|
if (($datePrecision > 1)) { |
111
|
10 |
|
$month = isset($selectorParts['month']) ? self::castInt( |
112
|
8 |
|
$selectorParts['month'] |
113
|
10 |
|
) : RepositorySelector::WILDCARD; |
114
|
10 |
|
} |
115
|
10 |
|
if (($datePrecision > 2)) { |
116
|
10 |
|
$day = isset($selectorParts['day']) ? self::castInt($selectorParts['day']) : RepositorySelector::WILDCARD; |
117
|
10 |
|
} |
118
|
10 |
|
if (($datePrecision > 3)) { |
119
|
3 |
|
$hour = isset($selectorParts['hour']) ? self::castInt( |
120
|
2 |
|
$selectorParts['hour'] |
121
|
3 |
|
) : RepositorySelector::WILDCARD; |
122
|
3 |
|
} |
123
|
10 |
|
if (($datePrecision > 4)) { |
124
|
3 |
|
$minute = isset($selectorParts['minute']) ? self::castInt( |
125
|
2 |
|
$selectorParts['minute'] |
126
|
3 |
|
) : RepositorySelector::WILDCARD; |
127
|
3 |
|
} |
128
|
10 |
|
if (($datePrecision > 5)) { |
129
|
3 |
|
$second = isset($selectorParts['second']) ? self::castInt( |
130
|
2 |
|
$selectorParts['second'] |
131
|
3 |
|
) : RepositorySelector::WILDCARD; |
132
|
3 |
|
} |
133
|
10 |
|
$uid = isset($selectorParts['id']) ? self::castInt($selectorParts['id']) : RepositorySelector::WILDCARD; |
134
|
|
|
|
135
|
10 |
|
$type = empty($selectorParts['type']) ? RepositorySelector::WILDCARD : trim($selectorParts['type']); |
136
|
10 |
|
$revision = (isset($selectorParts['revision']) && strlen($selectorParts['revision'])) ? intval( |
137
|
2 |
|
$selectorParts['revision'] |
138
|
10 |
|
) : Revision::CURRENT; |
139
|
|
|
|
140
|
10 |
|
return new RepositorySelector($year, $month, $day, $hour, $minute, $second, $uid, $type, $revision); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Cast a value as integer if it's not a wildcard |
145
|
|
|
* |
146
|
|
|
* @param string $value Value |
147
|
|
|
* @return int|string Integer value or wildcard |
148
|
|
|
*/ |
149
|
10 |
|
protected static function castInt($value) |
150
|
|
|
{ |
151
|
10 |
|
return ($value === RepositorySelector::WILDCARD) ? $value : intval($value); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|