ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FastRoute\RouteParser\Std Class Reference

Parses route strings of the following form: More...

+ Inheritance diagram for FastRoute\RouteParser\Std:
+ Collaboration diagram for FastRoute\RouteParser\Std:

Public Member Functions

 parse ($route)
 Parses a route string into multiple route data arrays. More...
 

Data Fields

const VARIABLE_REGEX
 
const DEFAULT_DISPATCH_REGEX = '[^/]+'
 

Private Member Functions

 parsePlaceholders ($route)
 Parses a route string that does not contain optional segments. More...
 

Detailed Description

Parses route strings of the following form:

"/user/{name}[/{id:[0-9]+}]"

Definition at line 13 of file Std.php.

Member Function Documentation

◆ parse()

FastRoute\RouteParser\Std::parse (   $route)

Parses a route string into multiple route data arrays.

The expected output is defined using an example:

For the route string "/fixedRoutePart/{varName}[/moreFixed/{varName2:\d+}]", if {varName} is interpreted as a placeholder and [...] is interpreted as an optional route part, the expected result is:

[ // first route: without optional part [ "/fixedRoutePart/", ["varName", "[^/]+"], ], // second route: with optional part [ "/fixedRoutePart/", ["varName", "[^/]+"], "/moreFixed/", ["varName2", [0-9]+"], ], ]

Here one route string was converted into two route data arrays.

Parameters
string$routeRoute string to parse
Returns
mixed[][] Array of route data arrays

Implements FastRoute\RouteParser.

Definition at line 25 of file Std.php.

References $n, and FastRoute\RouteParser\Std\parsePlaceholders().

26  {
27  $routeWithoutClosingOptionals = rtrim($route, ']');
28  $numOptionals = strlen($route) - strlen($routeWithoutClosingOptionals);
29 
30  // Split on [ while skipping placeholders
31  $segments = preg_split('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $routeWithoutClosingOptionals);
32  if ($numOptionals !== count($segments) - 1) {
33  // If there are any ] in the middle of the route, throw a more specific error message
34  if (preg_match('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \]~x', $routeWithoutClosingOptionals)) {
35  throw new BadRouteException('Optional segments can only occur at the end of a route');
36  }
37  throw new BadRouteException("Number of opening '[' and closing ']' does not match");
38  }
39 
40  $currentRoute = '';
41  $routeDatas = [];
42  foreach ($segments as $n => $segment) {
43  if ($segment === '' && $n !== 0) {
44  throw new BadRouteException('Empty optional part');
45  }
46 
47  $currentRoute .= $segment;
48  $routeDatas[] = $this->parsePlaceholders($currentRoute);
49  }
50  return $routeDatas;
51  }
$n
Definition: RandomTest.php:85
parsePlaceholders($route)
Parses a route string that does not contain optional segments.
Definition: Std.php:59
+ Here is the call graph for this function:

◆ parsePlaceholders()

FastRoute\RouteParser\Std::parsePlaceholders (   $route)
private

Parses a route string that does not contain optional segments.

Parameters
string
Returns
mixed[]

Definition at line 59 of file Std.php.

Referenced by FastRoute\RouteParser\Std\parse().

60  {
61  if (!preg_match_all(
62  '~' . self::VARIABLE_REGEX . '~x', $route, $matches,
63  PREG_OFFSET_CAPTURE | PREG_SET_ORDER
64  )) {
65  return [$route];
66  }
67 
68  $offset = 0;
69  $routeData = [];
70  foreach ($matches as $set) {
71  if ($set[0][1] > $offset) {
72  $routeData[] = substr($route, $offset, $set[0][1] - $offset);
73  }
74  $routeData[] = [
75  $set[1][0],
76  isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX
77  ];
78  $offset = $set[0][1] + strlen($set[0][0]);
79  }
80 
81  if ($offset !== strlen($route)) {
82  $routeData[] = substr($route, $offset);
83  }
84 
85  return $routeData;
86  }
const DEFAULT_DISPATCH_REGEX
Definition: Std.php:23
+ Here is the caller graph for this function:

Field Documentation

◆ DEFAULT_DISPATCH_REGEX

const FastRoute\RouteParser\Std::DEFAULT_DISPATCH_REGEX = '[^/]+'

Definition at line 23 of file Std.php.

◆ VARIABLE_REGEX

const FastRoute\RouteParser\Std::VARIABLE_REGEX
Initial value:
= <<<'REGEX'
\{
\s* ([a-zA-Z_][a-zA-Z0-9_-]*) \s*
(?:
: \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*)
)?
\}
REGEX

Definition at line 15 of file Std.php.


The documentation for this class was generated from the following file: