Passed
Push — master ( 6bec4b...1de4a9 )
by Hong
01:35
created

RouteGroup::fetchMatched()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 25
rs 9.7998
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Route
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types = 1);
11
12
namespace Phoole\Route\Util;
13
14
use Phoole\Route\Router;
15
use Phoole\Route\Parser\ParserInterface;
16
17
/**
18
 * RouteGroup
19
 *
20
 * @package Phoole\Route
21
 */
22
class RouteGroup
23
{
24
    /**
25
     * @var Route[]
26
     */
27
    protected $routes = [];
28
29
    /**
30
     * @var ParserInterface
31
     */
32
    protected $parser;
33
34
    /**
35
     * Set the parser
36
     *
37
     * @param  ParserInterface $parser
38
     */
39
    public function __construct(ParserInterface $parser)
40
    {
41
        $this->parser = $parser;
42
    }
43
44
    /**
45
     * Add a route to the route group
46
     *
47
     * @param  Route $route
48
     * @return $this
49
     */
50
    public function addRoute(Route $route)
51
    {
52
        $pattern = $route->getPattern();
53
        $hash = md5($route->getPattern());
54
        if (isset($this->routes[$hash])) {
55
            // add new method to existing route
56
            $this->routes[$hash]->addMethods($route);
57
        } else {
58
            // add new route
59
            $this->routes[$hash] = $route;
60
        }
61
62
        $this->parser->parse($hash, $pattern);
63
        return $this;
64
    }
65
66
    /**
67
     * @param  Result
68
     * @return bool
69
     */
70
    public function match(Result $result): bool
71
    {
72
        $request = $result->getRequest();
73
        $uri = $request->getUri()->getPath();
74
        $matched = $this->parser->match($uri);
75
76
        if (empty($matched)) {
77
            // not matched
78
            return FALSE;
79
        } else {
80
            // matched
81
            return $this->fetchMatched($matched, $result);
82
        }
83
    }
84
85
    /**
86
     * @param  array  $matched
87
     * @param  Result $result
88
     * @return bool
89
     */
90
    protected function fetchMatched(array $matched, Result $result): bool
91
    {
92
        // fetch the matched route
93
        list($hash, $params) = $matched;
94
        $route = $this->routes[$hash];
95
96
        // check if method exists
97
        $request = $result->getRequest();
98
        $method = $request->getMethod();
99
        if (isset($route->getMethods()[$method])) {
100
            // update request
101
            list($handler, $defaults) = $route->getMethods()[$method];
102
            $request = $request->withAttribute(
103
                Router::URI_PARAMETERS,
104
                array_merge($defaults, $params)
105
            );
106
107
            // update result
108
            $result->setHandler($handler);
109
            $result->setRoute($route);
110
            $result->setRequest($request);
111
112
            return TRUE;
113
        }
114
        return FALSE;
115
    }
116
}