RoutePathRegexGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 120
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generate() 0 10 2
A getRoutePath() 0 12 3
A replaceRoutePathWithRulesExpressions() 0 5 2
A replaceRoutePathParameter() 0 19 3
A getRoutePathRegexTemplates() 0 19 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Routing\Route;
26
27
/**
28
 * RoutePathRegexGenerator
29
 *
30
 * Implementation of a route regular expression generator.
31
 * The path can be manipulated using the aliases to handle
32
 * expected segments as an OR condition.
33
 * @author Celestino Diaz <[email protected]>
34
 */
35
class RoutePathRegexGenerator {
36
37
    /** @var integer */
38
    const TEMPLATE_REQUIRED_KEY = 0;
39
    const TEMPLATE_OPTIONAL_KEY = 1;
40
    const TEMPLATE_REPLACE_KEY = 2;
41
    const TEMPLATE_ANY_VALUE_KEY = 3;
42
43
    /** @var array */
44
    private $aliases;
45
46
    /**
47
     * Class constructor.
48
     * @param array $aliases the routing aliases
49
     */
50 1
    public function __construct(array $aliases = []) {
51 1
        $this->aliases = $aliases;
52 1
    }
53
54
    /**
55
     * Returns a regular expression from the route to match a request path.
56
     * @param \Brickoo\Component\Routing\Route\Route $route
57
     * @return string the regular expression for the route
58
     */
59 2
    public function generate(Route $route) {
60 2
        $routePath = $this->getRoutePath($route);
61
62 2
        $matches = [];
63 2
        if (preg_match_all("~(\\{(?<parameters>[\\w]+)\\})~", $routePath, $matches)) {
64 2
            $this->replaceRoutePathWithRulesExpressions($routePath, $matches['parameters'], $route);
65 2
        }
66
67 2
        return sprintf("~^/%s$~i", trim($routePath, "/"));
68
    }
69
70
    /**
71
     * Returns the route path containing the aliases definitions if any given.
72
     * @param \Brickoo\Component\Routing\Route\Route $route
73
     * @return string the modified route path containing the aliases
74
     */
75 2
    private function getRoutePath(Route $route) {
76 2
        $routePath = $route->getPath();
77
78 2
        foreach ($this->aliases as $routeKey => $routeAlias) {
79 2
            if (strpos($routePath, $routeKey) !== false) {
80 2
                $replacement = sprintf("(%s|%s)", $routeKey, preg_quote($routeAlias, "~"));
81 2
                $routePath = str_replace($routeKey, $replacement, $routePath);
82 2
                break;
83
            }
84 2
        }
85 2
        return $routePath;
86
    }
87
88
    /**
89
     * Replaces the route parameters with the rules defined.
90
     * @param string $routePath the route path
91
     * @param array $parameters the dynamic parameters of the route
92
     * @param \Brickoo\Component\Routing\Route\Route $route
93
     * @return void
94
     */
95 2
    private function replaceRoutePathWithRulesExpressions(&$routePath, array $parameters, Route $route) {
96 2
        foreach ($parameters as $parameterName) {
97 2
            $routePath = $this->replaceRoutePathParameter($routePath, $parameterName, $route);
98 2
        }
99 2
    }
100
101
    /**
102
     * Replace route path parameter placeholder with regex.
103
     * @param string $routePath
104
     * @param string $parameterName
105
     * @param \Brickoo\Component\Routing\Route\Route $route
106
     * @return string
107
     */
108 2
    private function replaceRoutePathParameter($routePath, $parameterName, Route $route) {
109 2
        $template = $this->getRoutePathRegexTemplates($routePath, $parameterName);
110 2
        if (!$route->hasRule($parameterName)) {
111 1
            return str_replace(
112 1
                sprintf($template[self::TEMPLATE_REPLACE_KEY], $parameterName),
113 1
                sprintf($template[self::TEMPLATE_ANY_VALUE_KEY], $parameterName),
114
                $routePath
115 1
            );
116
        }
117
118 1
        return str_replace(
119 1
            sprintf($template[self::TEMPLATE_REPLACE_KEY], $parameterName),
120 1
            ($route->hasDefaultValue($parameterName) ?
121 1
                sprintf($template[self::TEMPLATE_OPTIONAL_KEY], $parameterName, $route->getRule($parameterName)) :
122 1
                sprintf($template[self::TEMPLATE_REQUIRED_KEY], $parameterName, $route->getRule($parameterName))
123 1
            ),
124
            $routePath
125 1
        );
126
    }
127
128
    /**
129
     * Return the route path corresponding regex templates.
130
     * @param string $routePath
131
     * @param string $parameterName
132
     * @return array
133
     */
134 2
    private function getRoutePathRegexTemplates($routePath, $parameterName) {
135 2
        if (strpos($routePath, sprintf("/{%s}", $parameterName)) !== false) {
136
            $template = [
137 2
                self::TEMPLATE_REPLACE_KEY => "/{%s}",
138 2
                self::TEMPLATE_REQUIRED_KEY => "/(?<%s>%s)",
139 2
                self::TEMPLATE_OPTIONAL_KEY => "(/(?<%s>(%s)?))?",
140 2
                self::TEMPLATE_ANY_VALUE_KEY => "/(?<%s>[^/]+)"
141 2
            ];
142 2
        }
143
        else {
144
            $template = [
145 1
                self::TEMPLATE_REPLACE_KEY => "{%s}",
146 1
                self::TEMPLATE_REQUIRED_KEY => "(?<%s>%s)",
147 1
                self::TEMPLATE_OPTIONAL_KEY => "(?<%s>(%s)?)",
148 1
                self::TEMPLATE_ANY_VALUE_KEY => "(?<%s>[^/]+)"
149 1
            ];
150
        }
151 2
        return $template;
152
    }
153
154
}
155