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