Repository navigation
Expand file tree
/
Copy pathRoute.php
More file actions
157 lines (140 loc) · 4.24 KB
/
Copy pathRoute.php
File metadata and controls
157 lines (140 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Phprest\Annotation;
use InvalidArgumentException;
use LogicException;
use Phprest\Application;
/**
* @Annotation
*
* @Target("METHOD")
*/
class Route
{
/**
* @var string
*/
public $method;
/**
* @var string
*/
public $path;
/**
* @var null|string
*/
public $version;
/**
* @param mixed $options
*
* @throws InvalidArgumentException
*/
public function __construct($options)
{
$this->validate($options);
$version = null;
if (isset($options['since'], $options['until'])) {
$version = $this->getSinceUntilRegExp($options['since'], $options['until']);
} elseif (isset($options['since'])) {
$version = $this->getSinceRegExp($options['since']);
} elseif (isset($options['until'])) {
$version = $this->getUntilRegExp($options['until']);
}
$this->method = $options['method'];
$this->path = $options['path'];
$this->version = null !== $version ? '{version:' . $version . '}' : '{version:any}';
}
/**
* @param array $options
*
* @return void
*/
protected function validate(array $options): void
{
if (! isset($options['method'])) {
throw new InvalidArgumentException('method property is missing');
} elseif (! in_array($options['method'], ['GET', 'POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE', 'HEAD'])) {
throw new InvalidArgumentException('method property is not valid');
} elseif (! isset($options['path'])) {
throw new InvalidArgumentException('path property is missing');
} elseif (
isset($options['since'])
&& ! preg_match('#^' . Application::API_VERSION_REG_EXP . '$#', $options['since'])
) {
throw new InvalidArgumentException('since property is not valid');
} elseif (
isset($options['until'])
&& ! preg_match('#^' . Application::API_VERSION_REG_EXP . '$#', $options['until'])
) {
throw new InvalidArgumentException('until property is not valid');
}
}
/**
* @param string $sinceVersion
* @param string $untilVersion
*
* @return string
*/
protected function getSinceUntilRegExp($sinceVersion, $untilVersion)
{
$sinceVersion = str_pad($sinceVersion, 3, '.0');
$untilVersion = str_pad($untilVersion, 3, '.0');
if (! ($sinceVersion < $untilVersion)) {
throw new LogicException('since must be lesser than until');
}
if ($sinceVersion[0] === $untilVersion[0]) {
return sprintf(
'(?:%d\.[%d-%d])',
$sinceVersion[0],
$sinceVersion[2],
$untilVersion[2]
);
} elseif (abs($sinceVersion[0] - $untilVersion[0]) === 1) {
return sprintf(
'(?:%d\.[%d-9])|(?:%d\.[0-%d])',
$sinceVersion[0],
$sinceVersion[2],
$untilVersion[0],
$untilVersion[2]
);
} else {
return sprintf(
'(?:%d\.[%d-9])|(?:%d\.[0-%d])|(?:[%d-%d]\.\d)',
$sinceVersion[0],
$sinceVersion[2],
$untilVersion[0],
$untilVersion[2],
9 < $sinceVersion[0] + 1 ? 9 : $sinceVersion[0] + 1,
0 > $untilVersion[0] - 1 ? 0 : $untilVersion[0] - 1
);
}
}
/**
* @param string $version
*
* @return string
*/
protected function getSinceRegExp($version): string
{
$version = str_pad($version, 3, '.0');
return sprintf(
'(?:[%d-9]\.[%d-9])|(?:[%d-9]\.\d)',
$version[0],
$version[2],
9 < $version[0] + 1 ? 9 : $version[0] + 1
);
}
/**
* @param string $version
*
* @return string
*/
protected function getUntilRegExp($version): string
{
$version = str_pad($version, 3, '.0');
return sprintf(
'(?:[0-%d]\.[0-%d])|(?:[0-%d]\.\d)',
$version[0],
$version[2],
0 > $version[0] - 1 ? 0 : $version[0] - 1
);
}
}