Passed
Push — main ( 1a3a01...f6a128 )
by Moises
01:22
created

Matcher::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace DevPontes\Route\Traits;
4
5
use DevPontes\Route\Router;
6
7
trait Matcher
8
{
9
    /**
10
     * Set param
11
     *
12
     * @param string $routeUrl
13
     * @return string
14
     */
15
    protected function setParam(string $routeUrl): string
16
    {
17
        $routeUrl = explode('/', $routeUrl);
18
19
        foreach ($routeUrl as $k => $v) {
20
            if (preg_match('/^\{.*\}$/', $v) && (count($this->url) == count($routeUrl))) {
21
                $routeUrl[$k] = $this->url[$k];
22
                $this->param  = $this->url[$k];
0 ignored issues
show
Bug Best Practice introduced by
The property param does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
            }
24
25
            $route = implode('/', $routeUrl);
26
        }
27
28
        return $route;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $route seems to be defined by a foreach iteration on line 19. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
29
    }
30
31
    /**
32
     * Find the corresponding route based on the given URL
33
     *
34
     * @param array $url
35
     * @return Router|null
36
     */
37
    protected function match(array $url): ?Router
38
    {
39
        $url = implode('/', $url);
40
41
        foreach ($this->routes as $route) {
42
            $routeurl = $this->setParam($route->getUrl());
43
44
            if ($routeurl == $url) {
45
                return $route;
46
            }
47
        }
48
49
        return null;
50
    }
51
}
52