Completed
Push — master ( 28e582...816e5a )
by Restu
12:28
created

RegexDispatcher::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
1
<?php
2
namespace JayaCode\Framework\Core\Route\Dispatcher;
3
4
use JayaCode\Framework\Core\Route\Collector\RouteCollector;
5
use JayaCode\Framework\Core\Route\Route;
6
use JayaCode\Framework\Core\Route\Status;
7
8
/**
9
 * Class RegexDispatcher
10
 * @package JayaCode\Framework\Core\Route\Dispatcher
11
 */
12
class RegexDispatcher implements Dispatcher
13
{
14
    /**
15
     * @var RouteCollector
16
     */
17
    protected $routeCollector;
18
19
    /**
20
     * @var null
21
     */
22
    protected $data;
23
    /**
24
     * @var
25
     */
26
    protected $path;
27
28
    /**
29
     * @var array
30
     */
31
    protected $explodedPath;
32 3
33
    /**
34 3
     * @var
35 3
     */
36 3
    protected $httpMethod;
37
38
    /**
39
     * @param RouteCollector $routeCollector
40
     * @param null $data
41
     */
42 3
    public function __construct(RouteCollector $routeCollector, $data = null)
43
    {
44 3
        $this->routeCollector = $routeCollector;
45 3
        $this->data = $data;
46
    }
47 3
48
    /**
49 3
     * @param $httpMethod
50 3
     * @param $path
51 3
     * @return array|mixed
52
     */
53 3
    public function dispatch($httpMethod, $path)
54 3
    {
55
        $this->setPath($path);
56 3
        $this->httpMethod = $httpMethod;
57
58 3
        $data = is_null($this->data)?$this->routeCollector->getData():$this->data;
59 3
        return $this->findDataFromAllRoute($data);
60 3
    }
61 3
62 3
    /**
63
     * @param array $data
64 3
     * @return array
65 3
     */
66 3
    protected function findDataFromAllRoute(array $data)
67 3
    {
68 3
        $result = [
69 3
            Status::NOT_FOUND
70 3
        ];
71
72
        $countData = count($data);
73 3
        $countExplodePath = count($this->explodedPath);
74
75 3
        for ($i=0; $result[0] == Status::NOT_FOUND && $countData > $i; $i++) {
76
            $this->findDataByRouteMethodAndRulePath($data[$i], $countExplodePath, $result);
77
        }
78 3
79 3
        return $result;
80
    }
81
82 3
    /**
83 3
     * @param Route $route
84 3
     * @param $countExplodePath
85 3
     * @param array $result
86
     */
87 3
    protected function findDataByRouteMethodAndRulePath(Route $route, $countExplodePath, array &$result)
88 3
    {
89
        if ($this->httpMethod == $route->method && $countExplodePath == count($route->explodePath)) {
90 3
            $this->findDataByAllMatchesRulePath($route, $countExplodePath, $result);
91
92
            if ($result[0] == Status::FOUND) {
93
                $result[1] = $route->handle;
94
            }
95
        }
96 3
    }
97
98 3
    /**
99
     * @param Route $route
100 3
     * @param $countExplodePath
101 3
     * @param array $result
102
     */
103
    protected function findDataByAllMatchesRulePath(Route $route, $countExplodePath, array &$result)
104
    {
105
        $result[0] = Status::FOUND;
106
        if ($this->path != $route->path) {
107 3
            for ($j = 0; $j < $countExplodePath && $result[0] == Status::FOUND; $j++) {
108
                $this->findDataByMatchesRulePath($route, $j, $result);
109 3
            }
110 3
        }
111
    }
112
113
    /**
114
     * @param Route $route
115
     * @param $index
116
     * @param array $result
117 3
     */
118
    protected function findDataByMatchesRulePath(Route $route, $index, array &$result)
119 3
    {
120
        $countMatches = count($route->ruleMatches[$index]);
121
122
        if ($countMatches == 0 ||
123
            $countMatches == 2 && $this->explodedPath[$index] != $route->explodePath[$index] ||
124
            $countMatches == 5 &&
125
            !preg_match("/^".$route->ruleMatches[$index][4]."$/", $this->explodedPath[$index])) {
126
            $result[0] = Status::NOT_FOUND;
127
        } elseif ($countMatches == 3 || $countMatches == 5) {
128
            $result[2][$route->ruleMatches[$index][2]] = $this->explodedPath[$index];
129
        }
130
    }
131
132
    /**
133
     * @param mixed $path
134
     */
135
    public function setPath($path)
136
    {
137
        $this->path = static::normalizePath($path);
138
139
        $this->explodedPath = static::explodePath($this->path);
140
    }
141
142
    /**
143
     * @param $path
144
     * @return string
145
     */
146
    public static function normalizePath($path)
147
    {
148
        $path = trim($path, '/');
149
        return $path == ""?'/':$path;
150
    }
151
152
    /**
153
     * @param $path
154
     * @return array
155
     */
156
    public static function explodePath($path)
157
    {
158
        return (array) explode("/", $path);
159
    }
160
}
161