collectRouteParameters()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
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\Matcher;
26
27
use Brickoo\Component\Http\HttpRequest;
28
use Brickoo\Component\Routing\Route\HttpRoute;
29
use Brickoo\Component\Routing\Route\Route;
30
use Brickoo\Component\Routing\Route\RouteCollection;
31
use Brickoo\Component\Routing\Route\RoutePathRegexGenerator;
32
33
/**
34
 * CommonRouteMatcherStructure
35
 *
36
 * Implementation common http route matcher routines.
37
 * Following DRY principle.
38
 * @author Celestino Diaz <[email protected]>
39
 */
40
trait CommonRouteMatcherStructure {
41
42
    /** @var array */
43
    private $pathParameters;
44
45
    /**
46
     * Checks if the route does match the request uri path.
47
     * On matching, the request matching parameters will be extracted.
48
     * @param string $matchingPath
49
     * @param string $routeRegexPath
50
     * @return boolean check result
51
     */
52 2
    private function isMatchingRoute($matchingPath, $routeRegexPath) {
53 2
        $this->pathParameters = array();
54 2
        return (preg_match($routeRegexPath, $matchingPath, $this->pathParameters) == 1);
55
    }
56
57
    /**
58
     * Returns the route matched parameters.
59
     * @param \Brickoo\Component\Routing\Route\Route $route
60
     * @return array
61
     */
62 4
    private function collectRouteParameters(Route $route) {
63 4
        if (!$route->hasRules()) {
64 2
            return [];
65
        }
66
67 2
        $routeParameters = [];
68 2
        foreach (array_keys($route->getRules()) as $ruleParameter) {
69 2
            $routeParameters[$ruleParameter] = $this->getRuleCorrespondingRouteParameter($ruleParameter, $route);
70 2
        }
71 2
        return $routeParameters;
72
    }
73
74
    /**
75
     * Return the rule corresponding route parameter.
76
     * @param string $ruleParameter
77
     * @param Route $route
78
     * @return mixed
79
     */
80 2
    private function getRuleCorrespondingRouteParameter($ruleParameter, Route $route) {
81 2
        if (isset($this->pathParameters[$ruleParameter])
82 2
            && (!empty($this->pathParameters[$ruleParameter]))) {
83 2
                return $this->pathParameters[$ruleParameter];
84
        }
85 2
        return $route->getDefaultValue($ruleParameter);
86
    }
87
88
}
89