Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/REST/Common/RequestParser/Pattern.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\RequestParser;
8
9 use eZ\Publish\Core\REST\Common\RequestParser;
10 use eZ\Publish\Core\REST\Common\Exceptions;
11
22 class Pattern implements RequestParser
23 {
29 protected $map = [];
30
36 protected $compileCache = [];
37
41 const STANDARD_VARIABLE_REGEX = '\{([A-Za-z-_]+)\}';
42
46 const SLASHES_VARIABLE_REGEX = '\{(?:\&\s*)([A-Za-z-_]+)\}';
47
53 public function __construct(array $map = [])
54 {
55 foreach ($map as $type => $pattern) {
56 $this->addPattern($type, $pattern);
57 }
58 }
59
66 public function addPattern($type, $pattern)
67 {
68 $this->map[$type] = $pattern;
69 unset($this->compileCache[$type]);
70 }
71
80 public function parse($url)
81 {
82 foreach ($this->map as $pattern) {
83 $pattern = $this->compile($pattern);
84 if (preg_match($pattern, $url, $match)) {
85 // remove numeric keys
86 foreach ($match as $key => $value) {
87 if (is_numeric($key)) {
88 unset($match[$key]);
89 }
90 }
91
92 return $match;
93 }
94 }
95
96 throw new Exceptions\InvalidArgumentException("URL '$url' did not match any route.");
97 }
98
106 protected function compile($pattern)
107 {
108 if (isset($this->compileCache[$pattern])) {
109 return $this->compileCache[$pattern];
110 }
111
112 $pcre = '(^';
113
114 do {
115 switch (true) {
116 case preg_match('(^[^{]+)', $pattern, $match):
117 $pattern = substr($pattern, strlen($match[0]));
118 $pcre .= preg_quote($match[0]);
119 break;
120
121 case preg_match('(^' . self::STANDARD_VARIABLE_REGEX . ')', $pattern, $match):
122 $pattern = substr($pattern, strlen($match[0]));
123 $pcre .= '(?P<' . $match[1] . '>[^/]+)';
124 break;
125
126 case preg_match('(^' . self::SLASHES_VARIABLE_REGEX . ')', $pattern, $match):
127 $pattern = substr($pattern, strlen($match[0]));
128 $pcre .= '(?P<' . $match[1] . '>.+)';
129 break;
130
131 default:
132 throw new Exceptions\InvalidArgumentException("Invalid pattern part: '$pattern'.");
133 }
134 } while ($pattern);
135
136 $pcre .= '$)S';
137
138 $this->compileCache[$pattern] = $pcre;
139
140 return $pcre;
141 }
142
151 public function generate($type, array $values = [])
152 {
153 if (!isset($this->map[$type])) {
154 throw new Exceptions\InvalidArgumentException("No URL for type '$type' available.");
155 }
156
157 $url = $this->map[$type];
158 preg_match_all(
159 '(' . self::STANDARD_VARIABLE_REGEX . '|' . self::SLASHES_VARIABLE_REGEX . ')',
160 $url,
161 $matches,
162 PREG_SET_ORDER
163 );
164 foreach ($matches as $matchSet) {
165 $variableName = empty($matchSet[1]) ? $matchSet[2] : $matchSet[1];
166 if (!isset($values[$variableName])) {
167 throw new Exceptions\InvalidArgumentException("No value provided for '{$variableName}'.");
168 }
169
170 $url = str_replace($matchSet[0], $values[$variableName], $url);
171 unset($values[$variableName]);
172 }
173
174 if (count($values)) {
175 throw new Exceptions\InvalidArgumentException("Unused values in values array: '" . implode("', '", array_keys($values)) . "'.");
176 }
177
178 return $url;
179 }
180
181 public function parseHref($href, $attribute)
182 {
183 $parsingResult = $this->parse($href);
184
185 if (!isset($parsingResult[$attribute])) {
186 throw new Exceptions\InvalidArgumentException("No such attribute '$attribute' in route matched from $href\n" . print_r($parsingResult, true));
187 }
188
189 return $parsingResult[$attribute];
190 }
191 }
eZ\Publish\Core\REST\Common\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: Core/REST/Common/Exceptions/InvalidArgumentException.php:15
eZ\Publish\Core\REST\Common\RequestParser\Pattern
Pattern based Request parser.
Definition: Core/REST/Common/RequestParser/Pattern.php:23
eZ\Publish\Core\REST\Common\RequestParser\Pattern\STANDARD_VARIABLE_REGEX
const STANDARD_VARIABLE_REGEX
Pattern regular sub-expression.
Definition: Core/REST/Common/RequestParser/Pattern.php:41
eZ\Publish\Core\REST\Common\RequestParser\Pattern\parseHref
parseHref($href, $attribute)
Tries to match $href as a route, and returns the value of $attribute from the result.
Definition: Core/REST/Common/RequestParser/Pattern.php:181
eZ\Publish\Core\REST\Common\RequestParser\Pattern\compile
compile($pattern)
Compiles a given pattern to a PCRE regular expression.
Definition: Core/REST/Common/RequestParser/Pattern.php:106
eZ\Publish\Core\REST\Common\RequestParser\Pattern\addPattern
addPattern($type, $pattern)
Adds a pattern for a type.
Definition: Core/REST/Common/RequestParser/Pattern.php:66
eZ\Publish\Core\REST\Common\RequestParser\Pattern\generate
generate($type, array $values=[])
Generate a URL of the given type from the specified values.
Definition: Core/REST/Common/RequestParser/Pattern.php:151
eZ\Publish\Core\REST\Common\RequestParser\Pattern\$map
$map
Definition: Core/REST/Common/RequestParser/Pattern.php:29
eZ\Publish\Core\REST\Common\RequestParser\Pattern\$compileCache
$compileCache
Definition: Core/REST/Common/RequestParser/Pattern.php:36
eZ\Publish\Core\REST\Common\RequestParser\Pattern\parse
parse($url)
Parse URL and return the IDs contained in the URL.
Definition: Core/REST/Common/RequestParser/Pattern.php:80
eZ\Publish\Core\REST\Common\RequestParser\Pattern\SLASHES_VARIABLE_REGEX
const SLASHES_VARIABLE_REGEX
Pattern regular sub-expression that might contain slashes.
Definition: Core/REST/Common/RequestParser/Pattern.php:46
eZ\Publish\Core\REST\Common\RequestParser\Pattern\__construct
__construct(array $map=[])
Construct from optional initial map.
Definition: Core/REST/Common/RequestParser/Pattern.php:53
eZ\Publish\Core\REST\Common\RequestParser
Interface for Request parsers.
Definition: RequestParser.php:13
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Common\RequestParser
Definition: EzPublish.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.