Passed
Push — master ( 249964...c93993 )
by Nícollas
01:35
created

RouteManagerUtils::withoutMiddleware()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace MinasRouter\Traits;
4
5
use MinasRouter\Router\Middlewares\MiddlewareCollection;
6
7
trait RouteManagerUtils
8
{
9
    /** @var object */
10
    protected $middleware;
11
    
12
    public abstract function getDefaultName();
13
    public abstract function setName(String $name);
14
    public abstract function setWhereData(String $key, String $value);
15
16
    /**
17
     * Method responsible for defining the regular
18
     * expressions of dynamic parameters.
19
     * 
20
     * @param array $matches
21
     * 
22
     */
23
    public function where(array $matches)
24
    {
25
        array_map(function ($key, $value) {
26
            $this->setWhereData($key, $value);
27
        }, array_keys($matches), $matches);
28
29
        return $this;
30
    }
31
32
    /**
33
     * Alias of the where method, but only for one parameter
34
     * 
35
     * @param string $param
36
     * @param string $value
37
     * 
38
     */
39
    public function whereParam(String $param, String $value)
40
    {
41
        $this->setWhereData($param, $value);
42
43
        return $this;
44
    }
45
46
    /**
47
     * Method responsible for defining a
48
     * dynamic parameter as a number.
49
     * 
50
     * @param string $param
51
     * 
52
     */
53
    public function whereNumber(String $param)
54
    {
55
        return $this->whereParam($param, "[0-9]+");
56
    }
57
58
    /**
59
     * Method responsible for defining a
60
     * dynamic parameter as alpha characters.
61
     * 
62
     * @param string $param
63
     * 
64
     */
65
    public function whereAlpha(String $param)
66
    {
67
        return $this->whereParam($param, "[a-zA-ZÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖßÙÚÛÜÝŸÑàáâãäåçèéêëìíîïðòóôõöùúûüýÿñ]+");
68
    }
69
70
    /**
71
     * Method responsible for defining a
72
     * dynamic parameter as alpha numeric.
73
     * 
74
     * @param string $param
75
     * 
76
     */
77
    public function whereAlphaNumeric(String $param)
78
    {
79
        return $this->whereParam($param, "[a-zA-ZÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖßÙÚÛÜÝŸÑàáâãäåçèéêëìíîïðòóôõöùúûüýÿñ0-9]+");
80
    }
81
82
    /**
83
     * Method responsible for defining a
84
     * dynamic parameter as alpha numeric.
85
     * 
86
     * @param string $param
87
     * 
88
     */
89
    public function whereUuid(String $param)
90
    {
91
        return $this->whereParam($param, "(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}");
92
    }
93
94
    /**
95
     * Set the name of route. The second parameter
96
     * when true will ignore the group prefix.
97
     * 
98
     * @param string $name
99
     * @param bool $ignoreDefault
100
     * 
101
     */
102
    public function name(String $name, Bool $ignoreDefault = false)
103
    {
104
        if($ignoreDefault) {
105
            $this->setName($name);
106
        } else {
107
            $this->setName($this->getDefaultName() . $name);
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * Alias of the name method.
115
     * 
116
     * @param string $name
117
     * 
118
     */
119
    public function as(String $name)
120
    {
121
        return $this->name($name);
122
    }
123
124
    /**
125
     * Remove a group middleware of the route
126
     * 
127
     * @param string|array $middleware
128
     * 
129
     */
130
    public function withoutMiddleware($middleware)
131
    {
132
        if(!is_a($this->middleware, MiddlewareCollection::class)) {
133
            return;
134
        }
135
136
        $this->middleware->removeMiddleware($middleware);
137
    }
138
}