Passed
Push — master ( b268a6...9e113b )
by Zlatin
01:21
created

Route::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.6246

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 6
cts 13
cp 0.4615
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 2.6246
1
<?php
2
namespace DevOp\Core\Router;
3
4
class Route
5
{
6
7
    /**
8
     * @var string
9
     */
10
    private $pattern;
11
12
    /**
13
     * @var string
14
     */
15
    private $regEx;
16
17
    /**
18
     * @var array
19
     */
20
    private $parameters = [];
21
22
    /**
23
     * @var mixed
24
     */
25
    private $callback;
26
27
    /**
28
     * @param string $name
29
     * @param string $pattern
30
     * @param mixed $callback
31
     */
32 4
    public function __construct($pattern, $callback)
33
    {
34 4
        $this->pattern = $pattern;
35 4
        $this->callback = $callback;
36
37 4
        $this->compile($pattern);
38 4
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getRegEx()
44
    {
45
        return $this->regEx;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getPattern()
52
    {
53
        return $this->pattern;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getParameters()
60
    {
61
        return $this->parameters;
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    public function getCallback()
68
    {
69
        return $this->callback;
70
    }
71
72
    /**
73
     * @param string $pattern
74
     * @return string
75
     */
76 4
    public function compile($pattern)
77
    {
78 4
        $matches = [];
79 4
        preg_match_all('#\{(.*?)\}#s', $pattern, $matches, PREG_SET_ORDER);
80
81 4
        if (empty($matches)) {
82 4
            $this->regEx = "{$pattern}";
83 4
            return;
84
        }
85
        
86
        $components = array_map(function($values) {
87
            return ['name' => $values[0], 'value' => $values[1]];
88
        }, $matches);
89
90
        $replaces = $this->build($components);
91
92
        $keys = array_keys($replaces);
93
        $values = array_values($replaces);
94
95
        $this->regEx = sprintf("%s", str_replace($keys, $values, $pattern));
96
    }
97
98
    /**
99
     * @param array $components
100
     * @return array
101
     */
102
    private function build($components)
103
    {
104
105
        $replaces = [];
106
107
        foreach ($components AS $route) {
108
            $placeholder = ltrim($route['value'], '/');
109
            $optional = substr($route['value'], 0, 1) === '/';
110
            if (strchr($placeholder, ':')) {
111
                list($parameter, $value) = explode(':', $placeholder);
112
                $regEx = "(?P<$parameter>{$value})";
113
                $this->parameters[] = $parameter;
114
            } else {
115
                $regEx = "(?P<$placeholder>[^/]++)";
116
                $this->parameters[] = $placeholder;
117
            }
118
            $replaces[$route['name']] = !$optional ? $regEx : "(?:/{$regEx})";
119
        }
120
121
        return $replaces;
122
    }
123
}
124