Completed
Push — master ( 1ec95f...b2eddc )
by Freek
01:31 queued 11s
created

src/QueryBuilderRequest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
9
class QueryBuilderRequest extends Request
10
{
11
    public static function fromRequest(Request $request): self
12
    {
13
        return static::createFrom($request, new self());
14
    }
15
16
    public function includes(): Collection
17
    {
18
        $includeParameterName = config('query-builder.parameters.include');
19
20
        $includeParts = $this->query($includeParameterName);
21
22
        if (! is_array($includeParts)) {
23
            $includeParts = explode(',', $this->query($includeParameterName));
24
        }
25
26
        return collect($includeParts)
27
            ->filter()
28
            ->map([Str::class, 'camel']);
29
    }
30
31 View Code Duplication
    public function appends(): Collection
0 ignored issues
show
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...
32
    {
33
        $appendParameterName = config('query-builder.parameters.append');
34
35
        $appendParts = $this->query($appendParameterName);
36
37
        if (! is_array($appendParts)) {
38
            $appendParts = explode(',', strtolower($appendParts));
39
        }
40
41
        return collect($appendParts)->filter();
42
    }
43
44 View Code Duplication
    public function filters(): Collection
0 ignored issues
show
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...
45
    {
46
        $filterParameterName = config('query-builder.parameters.filter');
47
48
        $filterParts = $this->query($filterParameterName, []);
49
50
        if (is_string($filterParts)) {
51
            return collect();
52
        }
53
54
        $filters = collect($filterParts);
55
56
        return $filters->map(function ($value) {
57
            return $this->getFilterValue($value);
58
        });
59
    }
60
61 View Code Duplication
    public function fields(): Collection
62
    {
63
        $fieldsParameterName = config('query-builder.parameters.fields');
64
65
        $fieldsPerTable = collect($this->query($fieldsParameterName));
66
67
        if ($fieldsPerTable->isEmpty()) {
68
            return collect();
69
        }
70
71
        return $fieldsPerTable->map(function ($fields) {
72
            return explode(',', $fields);
73
        });
74
    }
75
76 View Code Duplication
    public function sorts(): Collection
77
    {
78
        $sortParameterName = config('query-builder.parameters.sort');
79
80
        $sortParts = $this->query($sortParameterName);
81
82
        if (is_string($sortParts)) {
83
            $sortParts = explode(',', $sortParts);
84
        }
85
86
        return collect($sortParts)->filter();
87
    }
88
89
    /**
90
     * @param $value
91
     *
92
     * @return array|bool
93
     */
94
    protected function getFilterValue($value)
95
    {
96
        if (is_array($value)) {
97
            return collect($value)->map(function ($valueValue) {
98
                return $this->getFilterValue($valueValue);
99
            })->all();
100
        }
101
102
        if (Str::contains($value, ',')) {
103
            return explode(',', $value);
104
        }
105
106
        if ($value === 'true') {
107
            return true;
108
        }
109
110
        if ($value === 'false') {
111
            return false;
112
        }
113
114
        return $value;
115
    }
116
}
117