1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Meraki\Route; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Implementation of a 'request-target' but with placeholder support. |
8
|
|
|
* |
9
|
|
|
* @author Nathan Bishop <[email protected]> (https://nathanbishop.name) |
10
|
|
|
* @copyright 2019 Nathan Bishop |
11
|
|
|
* @license The MIT license. |
12
|
|
|
*/ |
13
|
|
|
final class Pattern |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @const string [PLACEHOLDER_REGEX description] |
17
|
|
|
*/ |
18
|
|
|
const PLACEHOLDER_SEARCH_REGEX = '~:([^/]+)~'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @const string [COMPILED_PLACEHOLDER_REGEX description] |
22
|
|
|
*/ |
23
|
|
|
const PLACEHOLDER_REPLACE_REGEX = '(?<\1>[^/]+)'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string [$pattern description] |
27
|
|
|
*/ |
28
|
|
|
private $pattern; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] [$placeholders description] |
32
|
|
|
*/ |
33
|
|
|
private $placeholders; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $pattern [description] |
39
|
|
|
*/ |
40
|
|
|
public function __construct(string $pattern) |
41
|
|
|
{ |
42
|
|
|
$this->pattern = $this->stripQueryString($pattern); |
43
|
|
|
$this->placeholders = []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieve any placeholders and their values. |
48
|
|
|
* |
49
|
|
|
* @return string[] The placeholder names and its values (only after compiling). |
50
|
|
|
*/ |
51
|
|
|
public function getPlaceholders(): array |
52
|
|
|
{ |
53
|
|
|
return $this->placeholders; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allow this object to be treated like a string. |
58
|
|
|
* |
59
|
|
|
* @return string The pattern BEFORE it's compiled. |
60
|
|
|
*/ |
61
|
|
|
public function __toString(): string |
62
|
|
|
{ |
63
|
|
|
return $this->pattern; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Compile the request-target pattern into a regular expression. |
68
|
|
|
* |
69
|
|
|
* @todo Extract into a separate object along with the placeholders (something like `CompiledPattern`). |
70
|
|
|
* @return string A regular expression representing the pattern. |
71
|
|
|
*/ |
72
|
|
|
public function compile(): string |
73
|
|
|
{ |
74
|
|
|
$regex = preg_replace_callback(self::PLACEHOLDER_SEARCH_REGEX, [$this, 'extractPlaceholders'], $this->pattern); |
75
|
|
|
$regex = sprintf('~^%s$~', $regex); |
76
|
|
|
|
77
|
|
|
return $regex; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Compiles the pattern, checks if the request-target matches and extracts the placeholders. |
82
|
|
|
* |
83
|
|
|
* @param string $requestTarget The request-target to match against. |
84
|
|
|
* @return boolean `true` if compiled, matched and extracted successfully, `false` otherwise. |
85
|
|
|
*/ |
86
|
|
|
public function matches(string $requestTarget): bool |
87
|
|
|
{ |
88
|
|
|
$compiledPattern = $this->compile(); |
89
|
|
|
$requestTarget = $this->stripQueryString($requestTarget); |
90
|
|
|
|
91
|
|
|
if (preg_match($compiledPattern, $requestTarget, $matches) === 1) { |
92
|
|
|
$this->setPlaceholderValues($matches); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Extract placeholder names and give it an appropriate regex for matching. |
102
|
|
|
* |
103
|
|
|
* @param array $matches The matches returned from compile(). |
104
|
|
|
* @return string The regex to use in place of the placeholder name. |
105
|
|
|
*/ |
106
|
|
|
private function extractPlaceholders(array $matches): string |
107
|
|
|
{ |
108
|
|
|
$this->placeholders[$matches[1]] = null; |
109
|
|
|
|
110
|
|
|
return sprintf('(?<%s>[^/]+)', $matches[1]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the placeholder values from the match results. |
115
|
|
|
* |
116
|
|
|
* @param string[] $matches The results from preg_match(). |
117
|
|
|
*/ |
118
|
|
|
private function setPlaceholderValues(array $matches): void |
119
|
|
|
{ |
120
|
|
|
foreach ($this->placeholders as $key => $value) { |
121
|
|
|
if (isset($matches[$key])) { |
122
|
|
|
$this->placeholders[$key] = $matches[$key]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove the 'query string' part from a request-target. |
129
|
|
|
* |
130
|
|
|
* @param string $requestTarget The request-target which may or may not have a query string. |
131
|
|
|
* @return string The request-target without the query string. |
132
|
|
|
*/ |
133
|
|
|
private function stripQueryString(string $requestTarget): string |
134
|
|
|
{ |
135
|
|
|
return strpos($requestTarget, '?') !== false ? strstr($requestTarget, '?', true) : $requestTarget; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|