Completed
Push — master ( b68b20...ae6559 )
by Milroy
02:48
created

QueryParamBag::filled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiChef\RequestQueryHelper;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
10
class QueryParamBag
11
{
12
    private $params = [];
13
14
    /** @var Request $request*/
15
    private $request;
16
17
    /** @var string $field */
18
    private $field;
19
20 9
    public function __construct(Request $request, string $field)
21
    {
22 9
        $this->request = $request;
23 9
        $this->field = $field;
24
25 9
        if ($request->filled($field)) {
26 9
            $this->prepareParams($request->get($field));
27
        }
28 9
    }
29
30 3
    public function filled(): bool
31
    {
32 3
        return $this->request->filled($this->field);
33
    }
34
35 6
    public function has($field): bool
36
    {
37 6
        return Arr::has($this->params, $field);
38
    }
39
40
    public function keys(): array
41
    {
42
        return array_keys($this->params);
43
    }
44
45 6
    public function get($field, $default = null)
46
    {
47 6
        return Arr::get($this->params, $field, $default);
48
    }
49
50 6
    public function isEmpty($field)
51
    {
52 6
        return empty($this->get($field));
53
    }
54
55
    public function each($callback)
56
    {
57
        collect($this->params)->each($callback);
58
    }
59
60 9
    private function prepareParams($value): void
61
    {
62 9
        if (is_string($value)) {
63 3
            $this->prepareStringBasedParams($value);
64 6
        } elseif (is_array($value)) {
65 6
            $this->prepareArrayBasedParams($value);
66
        }
67 9
    }
68
69 3
    private function prepareStringBasedParams($value): void
70
    {
71
        collect(explode(',', $value))->each(function ($param) {
72 3
            $sections = explode(':', $param);
73 3
            $params = $this->prepareStringBasedNestedParams(array_slice($sections, 1));
74
75 3
            $this->params[$sections[0]] = $params;
76 3
        });
77 3
    }
78
79 3
    private function prepareStringBasedNestedParams(array $params): array
80
    {
81
        return collect($params)->mapWithKeys(function ($param) {
82 3
            $paramSections = explode('(', $param);
83
84 3
            return [$paramSections[0] => explode('|', str_replace(')', '', $paramSections[1]))];
85 3
        })->all();
86
    }
87
88 6
    private function prepareArrayBasedParams($value): void
89
    {
90
        collect($value)->each(function ($params, $field) {
91 6
            if (empty($params)) {
92 3
                $params = [];
93
            }
94
95 6
            if (is_array($params)) {
96 3
                $params = $this->prepareArrayBasedNestedParams($params);
97
            }
98
99 6
            $this->params[$field] = $params;
100 6
        });
101 6
    }
102
103 3
    private function prepareArrayBasedNestedParams(array $params): array
104
    {
105
        return collect($params)->mapWithKeys(function ($param, $name) {
106 3
            return [$name => explode('|', $param)];
107 3
        })->all();
108
    }
109
}
110