Completed
Pull Request — master (#5)
by Derek Stephen
02:02
created

Route::getControllerName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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