|
6 | 6 | use Illuminate\Database\Eloquent\Model;
|
7 | 7 | use Illuminate\Database\Eloquent\Relations\Relation;
|
8 | 8 | use Illuminate\Http\Request;
|
| 9 | +use Illuminate\Support\Facades\Log; |
9 | 10 | use Illuminate\Support\ServiceProvider;
|
10 | 11 | use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;
|
| 12 | +use Symfony\Component\HttpFoundation\ParameterBag; |
11 | 13 |
|
12 | 14 | class CrudProvider extends ServiceProvider
|
13 | 15 | {
|
@@ -68,6 +70,66 @@ function (...$arguments): Model {
|
68 | 70 | return $this;
|
69 | 71 | });
|
70 | 72 |
|
| 73 | + Request::macro( |
| 74 | + 'getFiltered', |
| 75 | + /** |
| 76 | + * @param string|int $filter can be 'alpha', 'alnum', 'digits', 'int', 'boolean' or an int flag |
| 77 | + * @see \filter_var() for $filter and $options |
| 78 | + * @see ParameterBag::getAlpha() |
| 79 | + * @see ParameterBag::getAlnum() |
| 80 | + * @see ParameterBag::getDigits() |
| 81 | + * @see ParameterBag::getInt() |
| 82 | + * @see ParameterBag::getBoolean() |
| 83 | + */ |
| 84 | + function ( |
| 85 | + string $key, |
| 86 | + mixed $default = null, |
| 87 | + string|int $filter = '', |
| 88 | + array|int $options = [], |
| 89 | + ): mixed { |
| 90 | + /** @var Request $this */ |
| 91 | + if (\is_int($filter)) { |
| 92 | + if ($this->attributes->has($key)) { |
| 93 | + return $this->attributes->filter($key, $default, $filter, $options); |
| 94 | + } |
| 95 | + |
| 96 | + if ($this->query->has($key)) { |
| 97 | + return $this->query->filter($key, $default, $filter, $options); |
| 98 | + } |
| 99 | + |
| 100 | + if ($this->request->has($key)) { |
| 101 | + return $this->request->filter($key, $default, $filter, $options); |
| 102 | + } |
| 103 | + |
| 104 | + return $default; |
| 105 | + } |
| 106 | + |
| 107 | + if ($filter !== '') { |
| 108 | + $filter = \ucfirst(\strtolower($filter)); |
| 109 | + |
| 110 | + if (!\in_array($filter, ['Alpha', 'Alnum', 'Digits', 'Int', 'Boolean'], true)) { |
| 111 | + $filter = ''; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + if ($this->attributes->has($key)) { |
| 117 | + return $this->attributes->{'get' . $filter}($key, $default); |
| 118 | + } |
| 119 | + if ($this->query->has($key)) { |
| 120 | + return $this->query->{'get' . $filter}($key, $default); |
| 121 | + } |
| 122 | + if ($this->request->has($key)) { |
| 123 | + return $this->request->{'get' . $filter}($key, $default); |
| 124 | + } |
| 125 | + } catch (\Throwable $e) { |
| 126 | + Log::error($e->getMessage(), $e->getTrace()); |
| 127 | + } |
| 128 | + |
| 129 | + return $default; |
| 130 | + } |
| 131 | + ); |
| 132 | + |
71 | 133 | Relation::macro('noConstraintsForRelationName', function (?string $relation = null): ?string {
|
72 | 134 | global $noConstraintsForRelationName;
|
73 | 135 |
|
|
0 commit comments