Passed
Push — master ( 24c12a...33eab4 )
by Marcio
03:56
created

Routes::route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * KNUT7 K7F (https://marciozebedeu.com/)
4
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @link      https://github.com/knut7/framework/ for the canonical source repository
11
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
12
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
13
 * @author    Marcio Zebedeu - [email protected]
14
 * @version   1.0.2
15
 */
16
17
namespace Ballybran\Helpers\Routing;
18
19
use Ballybran\Exception\Exception;
20
use Ballybran\Exception\KException;
21
use Ballybran\Helpers\Language;
0 ignored issues
show
Bug introduced by
The type Ballybran\Helpers\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class Routes
25
 * @package Ballybran\Helpers\Routing
26
 */
27
class Routes
28
{
29
    private $path, $callable, $matches = [], $params = [];
30
    protected $_controllerPath = PV . APP . DS . "Controllers/";
31
32
33
    /**
34
     * Routes constructor.
35
     * @param string $path
36
     * @param $callable
37
     */
38
    public function __construct($path, $callable)
39
    {
40
        $this->path = trim($path, '/');
41
        $this->callable = $callable;
42
43
    }
44
45
    /**
46
     * @param string $params
47
     * @param string $regex
48
     * @return Routes
49
     * example with('id','[0-9]+')
50
     */
51
    public function with(string $params, string $regex): Routes
52
    {
53
        $this->params[$params] = str_replace('(', '(?:', $regex);
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $url
59
     * @return bool
60
     */
61
    public function match($url) : bool
62
    {
63
        $url = trim($url, '/');
64
        $path = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], $this->path);
65
66
        $regex = "#^$path$#i";
67
68
        if (!preg_match($regex, $url, $matches)) {
69
70
            return false;
71
        }
72
        array_shift($matches);
73
        $this->matches = $matches;
74
75
        return true;
76
    }
77
78
79
    /**
80
     * @param array $match
81
     * @return string
82
     */
83
    private function paramMatch(array $match) : string
84
    {
85
        if (isset($this->params[$match[1]])) {
86
            return '(' . $this->params[$match[1]] . ')';
87
        }
88
89
        return '([^/]+)';
90
    }
91
	
92
93
        public function call()
94
    {
95
            
96
            if (is_string($this->callable)) {
97
98
                $params = explode('@', $this->callable);
99
100
                $file = $this->_controllerPath . $params[0] . '.php';
0 ignored issues
show
Unused Code introduced by
The assignment to $file is dead and can be removed.
Loading history...
101
102
                $controller = $this->_controllerPath . $params[0] . '.php';
0 ignored issues
show
Unused Code introduced by
The assignment to $controller is dead and can be removed.
Loading history...
103
                $namespace = str_replace('/', '\\', $this->_controllerPath);
104
                $className = $namespace . $params[0];
105
106
                $controller = new $className;
107
                // $controller = new $controller();
108
109
                return call_user_func_array([$controller, $params[1]], $this->matches);
110
111
            } else {
112
113
                return call_user_func_array($this->callable, $this->matches);
114
115
            }
116
117
    }
118
119
    /**
120
     * @param $params
121
     * @return string
122
     */
123
    public function getUrl($params) : string
124
    {
125
        $path = $this->path;
126
127
        foreach ($params as $key => $value) {
128
            $path = str_replace(":$key", $value, $path);
129
130
        }
131
        return $path;
132
    }
133
134
}
135