Completed
Push — master ( 527bb5...aca9c3 )
by César
05:15
created

Route::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Preetender\Routing;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class Route
10
 * @package Preetender\Routing
11
 */
12
class Route
13
{
14
    use RouteReflection;
15
16
    /** @var string */
17
    protected $url;
18
19
    /** @var  */
20
    protected $callable;
21
22
    /** @var   */
23
    protected $name;
24
25
    /** @var array  */
26
    private static $parameters = [];
27
28
    /** @var array  */
29
    private static $attributes = [];
30
31
    /** @var Request  */
32
    protected $request;
33
34
    /** @var  Response */
35
    protected $response;
36
37
    /** @var  */
38
    protected $running;
39
40
    /**
41
     * Route constructor.
42
     * @param $url
43
     * @param $callable
44
     * @param null $name
45
     */
46
    public function __construct($url, $callable, $name = null)
47
    {
48
        $this->url = trim($url, '/');
49
        $this->callable = $callable;
50
        $this->request = Kernel::getContainer()->get(Request::class);
51
        $this->response = Kernel::getContainer()->get(Response::class);
52
        $this->name = $name;
53
    }
54
55
    /**
56
     * Check the path, if there are parameters to create attributes for the scope of the request
57
     *
58
     * @param $url
59
     * @return bool
60
     */
61
    public function extractParameters($url)
62
    {
63
        $url = trim($url, '/');
64
        $path = preg_replace_callback('#:([\w]+)#', [$this, 'attributesMatch'], $this->getUrl());
65
        if(!preg_match("#^$path$#i", $url, $matches)) {
66
            return false;
67
        }
68
        array_shift($matches);
69
        static::$parameters = $matches;
0 ignored issues
show
Bug introduced by
Since $parameters is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $parameters to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
70
        return true;
71
    }
72
73
    /**
74
     * ...
75
     *
76
     * @param $match
77
     * @return string
78
     */
79
    private function attributesMatch($match) {
80
        if(isset(static::$attributes[$match[1]])) {
0 ignored issues
show
Bug introduced by
Since $attributes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $attributes to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
81
            return '(' . static::$attributes[$match[1]] . ')';
0 ignored issues
show
Bug introduced by
Since $attributes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $attributes to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
82
        }
83
        return '([^/]+)';
84
    }
85
86
    /**
87
     * Get url.
88
     *
89
     * @return string
90
     */
91
    public function getUrl(): string
92
    {
93
        return $this->url;
94
    }
95
96
    /**
97
     * Get name route
98
     *
99
     * @return null|string
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * Get callback.
108
     *
109
     * @return mixed
110
     */
111
    public function getCallable()
112
    {
113
        return $this->callable;
114
    }
115
116
    /**
117
     * Get params.
118
     *
119
     * @return array
120
     */
121
    public static function getParameters(): array
122
    {
123
        return static::$parameters;
0 ignored issues
show
Bug introduced by
Since $parameters is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $parameters to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
124
    }
125
126
    /**
127
     * Get properties on route
128
     *
129
     * @return array
130
     */
131
    public function getRoute()
132
    {
133
        return [
134
           'name' => $this->getName(),
135
           'path' => $this->getUrl(),
136
           'callable' => $this->getCallable(),
137
           'parameters' => static::resolveParameters()
138
        ];
139
    }
140
141
    /**
142
     * Executing route
143
     *
144
     * @param array $parameters
145
     * @return mixed
146
     */
147
    public function requestUrlCall($parameters = [])
148
    {
149
        $path = $this->getUrl();
150
        foreach ($parameters as $key => $value) {
151
            $path = str_replace(":$key", $value, $path);
152
        }
153
        $this->extractParameters($path);
154
        return $this->run();
155
    }
156
157
    /**
158
     * Exporta para o controlador uma coleção de dados;
159
     *
160
     * @param string $attribute
161
     * @param string $regex
162
     * @return $this
163
     */
164
    public function with(string $attribute, string $regex)
165
    {
166
        static::$attributes[$attribute] = str_replace('(', '(?:', $regex);
0 ignored issues
show
Bug introduced by
Since $attributes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $attributes to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
167
        return $this;
168
    }
169
170
    /**
171
     * Check the type of request and define which treatment it should receive
172
     *
173
     * @return mixed
174
     */
175
    public function run()
176
    {
177
        if(is_string($this->getCallable())) {
178
            return  $this->handleRequestController();
179
        }
180
        return $this->handleRequestCallable();
181
    }
182
}