Completed
Push — dev-master ( 4d197b...82c8e5 )
by Derek Stephen
05:07
created

Route::getActionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
namespace Bone\Mvc\Router;
4
use Bone\Regex;
5
use Bone\Regex\Url;
6
7
class Route
8
{
9
    /**
10
     *  This be th' route from th' configgerashun
11
     * @var string
12
     */
13
    private $name;
14
15
    /**
16
     * Th' settin's for th' route. garr!
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     *  This be the exploded path of th' array
23
     * @var array
24
     */
25
    private $parts;
26
27
    /**
28
     *  th' optional [/:var] part of th' uri as a regex
29
     * @var string;
30
     */
31
    private $optional;
32
33
    /**
34
     *  a bunch of voodoo regex strings
35
     * @var array
36
     */
37
    private $strings;
38
39
    /**
40
     *  fer matchin' patterns
41
     * @var \Bone\Regex
42
     */
43
    private $regex;
44
45
46
    /**
47
     *  th' exploded uri
48
     * @var array
49
     */
50
    private $matched_uri_parts;
51
52
53
54
55
56
57
58
59
60
    /**
61
     * The key an' value from th' configgerashun
62
     *
63
     * @param $name
64
     * @param array $config
65
     */
66 16
    public function __construct($name, array $config)
67 16
    {
68 16
        $this->name = $name;
69 16
        $this->config = $config;
70 16
        $this->strings = [
71 16
            0 => '',
72
        ];
73
74 16
        $name = $this->removeOptionalFromName($name);
75
76 16
        $this->setParts($name);
77
78 16
        $this->setStrings();
79
80 16
        $this->setOptionalStrings();
81
82
83 16
    }
84
85
86
    /**
87
     * Check fer an optional var [/:var] in the configgered route
88
     * @param string $name
89
     * @return string
90
     */
91 16
    private function removeOptionalFromName($name)
92 16
    {
93 16
        $this->regex = new Regex(Url::SQUARE_BRACKETS);
94 16
        if($matches = $this->regex->getMatches($name))
95 16
        {
96
            /**
97
             *  th' route has an optional field [/:var] at th' end garr
98
             *  we'll add it after we've done the rest
99
             */
100 6
            $this->optional = str_replace('/','',$matches[1]);
101 6
            $name = str_replace('[/'.$this->optional.']','', $name);
102 6
        }
103 16
        return $name;
104
    }
105
106
107
108
109
110
111
112
113
    /**
114
     * checks t' see if th' uri matches the regex routes
115
     *
116
     * @param $uri
117
     * @return bool
118
     */
119 6
    public function checkRoute($uri)
120 6
    {
121 6
        foreach($this->strings as $expression)
122
        {
123
            // check if it matches the pattern
124 6
            $this->regex->setPattern($expression);
125 6
            if($this->regex->getMatches($uri))
126 6
            {
127 1
                $this->matched_uri_parts = explode('/',$uri);
128 1
                return true;
129
            }
130 6
        }
131 6
        return false;
132
    }
133
134
135
    /**
136
     * @param $name
137
     */
138 16
    private function setParts($name)
139 16
    {
140
        /**
141
         *  blow the feckin' route to smithereens
142
         */
143 16
        $this->parts = explode('/',$name);
144 16
    }
145
146
147
    /**
148
     *  break the url t' smithereens! garrr!
149
     */
150 16
    private function setStrings()
151 16
    {
152
        /*
153
         * Sift through the wreckage
154
         */
155 16
        foreach($this->parts as $part)
156
        {
157 16
            $this->checkPart($part);
158 16
        }
159
160
        /*
161
         *  if there's still nuthin', we must be on the feckin' home page
162
         */
163 16
        $this->strings[0] = ($this->strings[0] == '') ? '\/' : $this->strings[0];
164 16
    }
165
166
167
    /**
168
     * @param string $part
169
     */
170 16
    private function checkPart($part)
171 16
    {
172
        /*
173
         *  look fer a colon
174
         */
175 16
        if($part && strstr($part,':'))
176 16
        {
177
            /*
178
             * Make it look fer /something
179
             */
180 6
            $this->strings[0] .= Url::SLASH_WORD;
181 6
        }
182 16
        elseif($part)
183
        {
184
            /*
185
             * make it look fer /part
186
             */
187 6
            $this->strings[0] .= '\/'.$part;
188 6
        }
189 16
    }
190
191
192
193
    /**
194
     *  checks fer the optional stuff
195
     */
196 16
    private function setOptionalStrings()
197 16
    {
198
        /*
199
         *  Make another string t' check fer
200
         */
201 16
        if($this->optional)
202 16
        {
203 6
            $this->strings[1] = $this->strings[0].Url::SLASH_WORD;
204
            //reverse the fecker, if the longer one matches first, good!
205 6
            $this->strings = array_reverse($this->strings);
206 6
        }
207 16
    }
208
209
210
211
212
    /**
213
     * th' patterns the route wants t' match
214
     *
215
     * @return array
216
     */
217 16
    public function getRegexStrings()
218 16
    {
219 16
        return $this->strings;
220
    }
221
222
223
224
225
226
227
228
229
230 1
    public function getControllerName()
231 1
    {
232 1
        return $this->config['controller'];
233
    }
234
235
236
237
238
239
240
241 1
    public function getActionName()
242 1
    {
243 1
        return $this->config['action'];
244
    }
245
246
247
248
249
250
251
252
253 1
    public function getParams()
254 1
    {
255 1
        $x = 0;
256 1
        foreach($this->parts as $part)
257
        {
258 1
            if(strstr($part,':'))
259 1
            {
260 1
                $this->config['params'][str_replace(':','',$part)] = $this->matched_uri_parts[$x];
261 1
            }
262 1
            $x ++;
263 1
        }
264 1
        if($this->optional)
265 1
        {
266 1
            $this->config['params'][str_replace(':','',$this->optional)] = isset($this->matched_uri_parts[$x]) ? $this->matched_uri_parts[$x] : null;
267 1
        }
268 1
        return $this->config['params'];
269
    }
270
}