Completed
Pull Request — master (#223)
by Axel
01:39
created

QueryBuilderRequest::searches()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
9
class QueryBuilderRequest extends Request
10
{
11
    public static function fromRequest(Request $request): self
12
    {
13
        return static::createFrom($request, new self());
0 ignored issues
show
Documentation introduced by
$request is of type object<Illuminate\Http\Request>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
14
    }
15
16
    public function includes()
17
    {
18
        $parameter = config('query-builder.parameters.include');
19
20
        $includeParts = $this->query($parameter);
21
22
        if (! is_array($includeParts)) {
23
            $includeParts = explode(',', strtolower($this->query($parameter)));
24
        }
25
26
        return collect($includeParts)->filter();
27
    }
28
29 View Code Duplication
    public function appends()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $appendParameter = config('query-builder.parameters.append');
32
33
        $appendParts = $this->query($appendParameter);
34
35
        if (! is_array($appendParts)) {
36
            $appendParts = explode(',', strtolower($appendParts));
37
        }
38
39
        return collect($appendParts)->filter();
40
    }
41
42
    public function filters()
43
    {
44
        $filterParameter = config('query-builder.parameters.filter');
45
46
        $filterParts = $this->query($filterParameter, []);
47
48
        if (is_string($filterParts)) {
49
            return collect();
50
        }
51
52
        $filters = collect($filterParts);
53
54
        return $filters->map(function ($value) {
55
            return $this->getFilterValue($value);
56
        });
57
    }
58
59
    public function searches()
60
    {
61
        $searchParameter = config('query-builder.parameters.search');
62
63
        $searchParts = collect($this->query(null, []))
64
            ->filter(function ($value, $queryKey) use ($searchParameter) {
65
                return $queryKey === $searchParameter || Str::startsWith($queryKey, $searchParameter.':');
66
            })->toArray();
67
68
        if (is_string($searchParts)) {
69
            return collect();
70
        }
71
72
        $searches = collect($searchParts);
73
74
        return $searches->map(function ($value) {
75
            return $this->getSearchValue($value);
76
        });
77
    }
78
79 View Code Duplication
    public function fields(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $fieldsParameter = config('query-builder.parameters.fields');
82
83
        $fieldsPerTable = collect($this->query($fieldsParameter));
84
85
        if ($fieldsPerTable->isEmpty()) {
86
            return collect();
87
        }
88
89
        return $fieldsPerTable->map(function ($fields) {
90
            return explode(',', $fields);
91
        });
92
    }
93
94 View Code Duplication
    public function sorts(): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $sortParameter = config('query-builder.parameters.sort');
97
98
        $sortParts = $this->query($sortParameter);
99
100
        if (is_string($sortParts)) {
101
            $sortParts = explode(',', $sortParts);
102
        }
103
104
        return collect($sortParts)->filter();
105
    }
106
107 View Code Duplication
    protected function getFilterValue($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if (is_array($value)) {
110
            return collect($value)->map(function ($valueValue) {
111
                return $this->getFilterValue($valueValue);
112
            })->all();
113
        }
114
115
        if (Str::contains($value, ',')) {
116
            return explode(',', $value);
117
        }
118
119
        if ($value === 'true') {
120
            return true;
121
        }
122
123
        if ($value === 'false') {
124
            return false;
125
        }
126
127
        return $value;
128
    }
129
130 View Code Duplication
    protected function getSearchValue($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        if (is_array($value)) {
133
            return collect($value)->map(function ($valueValue) {
134
                return $this->getSearchValue($valueValue);
135
            })->all();
136
        }
137
138
        if (Str::contains($value, ',')) {
139
            return explode(',', $value);
140
        }
141
142
        if ($value === 'true') {
143
            return true;
144
        }
145
146
        if ($value === 'false') {
147
            return false;
148
        }
149
150
        return $value;
151
    }
152
}
153