Passed
Push — master ( 9e113b...59ff1d )
by Zlatin
01:34
created

Route::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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