QueryParamBag::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
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
$this->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 ignore-type  annotation

43
        collect(/** @scrutinizer ignore-type */ $this->params)->each($callback);
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
Bug introduced by
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 ignore-type  annotation

57
        collect(/** @scrutinizer ignore-type */ explode(',', $value))->each(function ($param) {
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
Bug introduced by
$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 ignore-type  annotation

67
        return collect(/** @scrutinizer ignore-type */ $params)->mapWithKeys(function ($param) {
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
Bug introduced by
$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 ignore-type  annotation

91
        return collect(/** @scrutinizer ignore-type */ $params)->mapWithKeys(function ($param, $name) {
Loading history...
92
            return [$name => explode('|', $param)];
93
        })->all();
94
    }
95
}
96