LaraCrafts /
laravel-geo-routes
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaraCrafts\GeoRoutes; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use Illuminate\Routing\Route; |
||
| 7 | use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @mixin \Illuminate\Routing\Route |
||
| 11 | */ |
||
| 12 | class GeoRoute |
||
| 13 | { |
||
| 14 | use Concerns\HasCallback; |
||
| 15 | use Concerns\ControlsAccess; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Rule is applied. |
||
| 19 | * |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | protected $applied; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The countries to apply the rule for. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $countries; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The route. |
||
| 33 | * |
||
| 34 | * @var \Illuminate\Routing\Route |
||
| 35 | */ |
||
| 36 | protected $route; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The rule's strategy. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $strategy; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new GeoRoute instance. |
||
| 47 | * |
||
| 48 | * @param \Illuminate\Routing\Route $route |
||
| 49 | * @param array $countries |
||
| 50 | * @param string $strategy |
||
| 51 | */ |
||
| 52 | 80 | public function __construct(Route $route, array $countries, string $strategy) |
|
| 53 | { |
||
| 54 | 80 | $this->applied = false; |
|
| 55 | 80 | $this->countries = array_map('strtoupper', $countries); |
|
| 56 | 80 | $this->route = $route; |
|
| 57 | 80 | $this->strategy = $strategy; |
|
| 58 | 80 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Dynamically call the underlying route. |
||
| 62 | * |
||
| 63 | * @param string $method |
||
| 64 | * @param array $arguments |
||
| 65 | * |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | public function __call(string $method, array $arguments) |
||
| 69 | { |
||
| 70 | if (method_exists($this->route, $method) || Route::hasMacro($method)) { |
||
| 71 | return $this->route->$method(...$arguments); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (CallbackRegistrar::hasProxy($method)) { |
||
| 75 | return $this->setCallback(CallbackRegistrar::callback($method), $arguments); |
||
| 76 | } |
||
| 77 | |||
| 78 | throw new BadMethodCallException("Undefined method '$method'"); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Destruct the GeoRoute instance and apply the middleware. |
||
| 83 | */ |
||
| 84 | 80 | public function __destruct() |
|
| 85 | { |
||
| 86 | 80 | $this->applyConstraint(); |
|
| 87 | 80 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Apply the geo-constraint to the route. |
||
| 91 | */ |
||
| 92 | 80 | protected function applyConstraint() |
|
| 93 | { |
||
| 94 | 80 | if ($this->applied || !$this->countries) { |
|
|
0 ignored issues
–
show
|
|||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | 80 | $action = $this->route->getAction(); |
|
| 99 | 80 | $action['middleware'][] = 'geo'; |
|
| 100 | 80 | $action['geo'] = [ |
|
| 101 | 80 | 'strategy' => $this->strategy, |
|
| 102 | 80 | 'countries' => (array)$this->countries, |
|
| 103 | 80 | 'callback' => $this->callback, |
|
| 104 | ]; |
||
| 105 | |||
| 106 | 80 | $this->route->setAction($action); |
|
| 107 | |||
| 108 | 80 | $this->applied = true; |
|
| 109 | 80 | } |
|
| 110 | } |
||
| 111 |
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.