LaraCrafts /
laravel-geo-routes
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace LaraCrafts\GeoRoutes; |
||||
| 4 | |||||
| 5 | use Closure; |
||||
| 6 | use Illuminate\Support\Arr; |
||||
| 7 | |||||
| 8 | class GeoGroup |
||||
| 9 | { |
||||
| 10 | use Concerns\HasCallback; |
||||
| 11 | use Concerns\ControlsAccess; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * Determines if the geo rule is applied. |
||||
| 15 | * |
||||
| 16 | * @var bool |
||||
| 17 | */ |
||||
| 18 | protected $applied; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * The routes closure. |
||||
| 22 | * |
||||
| 23 | * @var \Closure |
||||
| 24 | */ |
||||
| 25 | protected $routes; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * The routes group shared attributes. |
||||
| 29 | * |
||||
| 30 | * @var array |
||||
| 31 | */ |
||||
| 32 | protected $attributes; |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * The router instance. |
||||
| 36 | * |
||||
| 37 | * @var \Illuminate\Routing\Router |
||||
| 38 | */ |
||||
| 39 | protected $router; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * The attributes that can be set through this class. |
||||
| 43 | * |
||||
| 44 | * @var array |
||||
| 45 | */ |
||||
| 46 | protected $allowedAttributes = [ |
||||
| 47 | 'as', 'domain', 'middleware', 'name', 'namespace', 'prefix', 'where', |
||||
| 48 | ]; |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * The attributes that are aliased. |
||||
| 52 | * |
||||
| 53 | * @var array |
||||
| 54 | */ |
||||
| 55 | protected $aliases = [ |
||||
| 56 | 'name' => 'as', |
||||
| 57 | ]; |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Create a new GeoGroup instance. |
||||
| 61 | * |
||||
| 62 | * @param array $attributes |
||||
| 63 | * @param \Closure $routes |
||||
| 64 | */ |
||||
| 65 | 112 | public function __construct(array $attributes, Closure $routes) |
|||
| 66 | { |
||||
| 67 | 112 | $this->attributes = $attributes; |
|||
| 68 | 112 | $this->routes = $routes; |
|||
| 69 | 112 | $this->router = app('router'); |
|||
| 70 | 112 | $this->applied = false; |
|||
| 71 | |||||
| 72 | 112 | static::loadProxies(); |
|||
| 73 | 112 | } |
|||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Destruct the GeoGroup instance and apply the rule. |
||||
| 77 | */ |
||||
| 78 | 112 | public function __destruct() |
|||
| 79 | { |
||||
| 80 | 112 | $this->applyConstraint(); |
|||
| 81 | 112 | } |
|||
| 82 | |||||
| 83 | /** |
||||
| 84 | * Set the array of countries covered by the rule. |
||||
| 85 | * |
||||
| 86 | * @param string ...$countries |
||||
| 87 | * |
||||
| 88 | * @return $this |
||||
| 89 | */ |
||||
| 90 | public function from(string ...$countries) |
||||
| 91 | { |
||||
| 92 | $this->countries = $countries; |
||||
| 93 | |||||
| 94 | return $this; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | /** |
||||
| 98 | * Apply the geo-constraint to the routes group. |
||||
| 99 | */ |
||||
| 100 | 112 | protected function applyConstraint() |
|||
| 101 | { |
||||
| 102 | 112 | if ($this->applied || !$this->countries) { |
|||
|
0 ignored issues
–
show
|
|||||
| 103 | return; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | 112 | $attributes = $this->attributes; |
|||
| 107 | 112 | $attributes['middleware'][] = 'geo'; |
|||
| 108 | 112 | $attributes['geo'] = [ |
|||
| 109 | 112 | 'strategy' => $this->strategy, |
|||
| 110 | 112 | 'countries' => (array)$this->countries, |
|||
| 111 | 112 | 'callback' => $this->callback, |
|||
| 112 | ]; |
||||
| 113 | |||||
| 114 | 112 | $this->router->group($attributes, $this->routes); |
|||
| 115 | |||||
| 116 | 112 | $this->applied = true; |
|||
| 117 | 112 | } |
|||
| 118 | |||||
| 119 | /** |
||||
| 120 | * Dynamically call the router methods. |
||||
| 121 | * |
||||
| 122 | * @param string $method |
||||
| 123 | * @param array $arguments |
||||
| 124 | * |
||||
| 125 | * @return mixed |
||||
| 126 | */ |
||||
| 127 | 16 | public function __call(string $method, array $arguments) |
|||
| 128 | { |
||||
| 129 | 16 | if ($this->callbackExists($method)) { |
|||
| 130 | return $this->setCallback(static::$proxies[$method], $arguments); |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | 16 | if ($this->router::hasMacro($method)) { |
|||
| 134 | return $this->macroCall($method, $arguments); |
||||
|
0 ignored issues
–
show
The method
macroCall() does not exist on LaraCrafts\GeoRoutes\GeoGroup. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 135 | } |
||||
| 136 | |||||
| 137 | 16 | if ($method === 'middleware') { |
|||
| 138 | return $this->attribute($method, is_array($arguments[0]) ? $arguments[0] : $arguments); |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | 16 | return ($this)->attribute($method, $arguments[0]); |
|||
| 142 | } |
||||
| 143 | |||||
| 144 | /** |
||||
| 145 | * Set the value for a given attribute. |
||||
| 146 | * |
||||
| 147 | * @param string $key |
||||
| 148 | * @param mixed $value |
||||
| 149 | * |
||||
| 150 | * @return $this |
||||
| 151 | * |
||||
| 152 | * @throws \InvalidArgumentException |
||||
| 153 | */ |
||||
| 154 | 32 | public function attribute($key, $value) |
|||
| 155 | { |
||||
| 156 | 32 | if (! in_array($key, $this->allowedAttributes)) { |
|||
| 157 | throw new \InvalidArgumentException("Attribute [{$key}] does not exist."); |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | 32 | $this->attributes[Arr::get($this->aliases, $key, $key)] = $value; |
|||
| 161 | |||||
| 162 | 32 | return $this; |
|||
| 163 | } |
||||
| 164 | } |
||||
| 165 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.