Completed
Push — master ( e1bc0f...1d405f )
by Derek Stephen
01:42
created

Route::processMandatoryParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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