sarala-io /
sarala-laravel
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Sarala\Query; |
||||
| 6 | |||||
| 7 | use Illuminate\Http\Request; |
||||
| 8 | use Illuminate\Support\Arr; |
||||
| 9 | |||||
| 10 | class QueryParamBag |
||||
| 11 | { |
||||
| 12 | private $params = []; |
||||
| 13 | |||||
| 14 | public function __construct(Request $request, string $field) |
||||
| 15 | { |
||||
| 16 | if ($request->filled($field)) { |
||||
| 17 | $this->prepareParams($request->get($field)); |
||||
| 18 | } |
||||
| 19 | } |
||||
| 20 | |||||
| 21 | public function has($field): bool |
||||
| 22 | { |
||||
| 23 | return Arr::has($this->params, $field); |
||||
| 24 | } |
||||
| 25 | |||||
| 26 | public function keys(): array |
||||
| 27 | { |
||||
| 28 | return array_keys($this->params); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | public function get($field, $default = null) |
||||
| 32 | { |
||||
| 33 | return Arr::get($this->params, $field, $default); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function isEmpty($field) |
||||
| 37 | { |
||||
| 38 | return empty($this->get($field)); |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | public function each($callback) |
||||
| 42 | { |
||||
| 43 | collect($this->params)->each($callback); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 44 | } |
||||
| 45 | |||||
| 46 | private function prepareParams($value): void |
||||
| 47 | { |
||||
| 48 | if (is_string($value)) { |
||||
| 49 | $this->prepareStringBasedParams($value); |
||||
| 50 | } elseif (is_array($value)) { |
||||
| 51 | $this->prepareArrayBasedParams($value); |
||||
| 52 | } |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | private function prepareStringBasedParams($value): void |
||||
| 56 | { |
||||
| 57 | collect(explode(',', $value))->each(function ($param) { |
||||
|
0 ignored issues
–
show
explode(',', $value) of type string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 58 | $sections = explode(':', $param); |
||||
| 59 | $params = $this->prepareStringBasedNestedParams(array_slice($sections, 1)); |
||||
| 60 | |||||
| 61 | $this->params[$sections[0]] = $params; |
||||
| 62 | }); |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | private function prepareStringBasedNestedParams(array $params): array |
||||
| 66 | { |
||||
| 67 | return collect($params)->mapWithKeys(function ($param) { |
||||
|
0 ignored issues
–
show
$params of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 68 | $paramSections = explode('(', $param); |
||||
| 69 | |||||
| 70 | return [$paramSections[0] => explode('|', str_replace(')', '', $paramSections[1]))]; |
||||
| 71 | })->all(); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | private function prepareArrayBasedParams($value): void |
||||
| 75 | { |
||||
| 76 | collect($value)->each(function ($params, $field) { |
||||
| 77 | if ($params === '') { |
||||
| 78 | $params = []; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | if (is_array($params)) { |
||||
| 82 | $params = $this->prepareArrayBasedNestedParams($params); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | $this->params[$field] = $params; |
||||
| 86 | }); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | private function prepareArrayBasedNestedParams(array $params): array |
||||
| 90 | { |
||||
| 91 | return collect($params)->mapWithKeys(function ($param, $name) { |
||||
|
0 ignored issues
–
show
$params of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 92 | return [$name => explode('|', $param)]; |
||||
| 93 | })->all(); |
||||
| 94 | } |
||||
| 95 | } |
||||
| 96 |