Completed
Push — master ( 6b56a3...d3d796 )
by
unknown
16:05 queued 14:46
created

src/Concerns/AddsFieldsToQuery.php (3 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\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\Exceptions\AllowedFieldsMustBeCalledBeforeAllowedIncludes;
8
use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery;
9
use Spatie\QueryBuilder\Exceptions\UnknownIncludedFieldsQuery;
10
11
trait AddsFieldsToQuery
12
{
13
    /** @var \Illuminate\Support\Collection */
14
    protected $allowedFields;
15
16
    public function allowedFields($fields): self
17
    {
18
        if ($this->allowedIncludes instanceof Collection) {
19
            throw new AllowedFieldsMustBeCalledBeforeAllowedIncludes();
20
        }
21
22
        $fields = is_array($fields) ? $fields : func_get_args();
23
24
        $this->allowedFields = collect($fields)
25
            ->map(function (string $fieldName) {
26
                return $this->prependField($fieldName);
27
            });
28
29
        $this->ensureAllFieldsExist();
30
31
        $this->addRequestedModelFieldsToQuery();
32
33
        return $this;
34
    }
35
36
    protected function addRequestedModelFieldsToQuery()
37
    {
38
        $modelTableName = $this->getModel()->getTable();
0 ignored issues
show
It seems like getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
40
        $modelFields = $this->request->fields()->get($modelTableName);
41
42
        if (empty($modelFields)) {
43
            return;
44
        }
45
46
        $prependedFields = $this->prependFieldsWithTableName($modelFields, $modelTableName);
47
48
        $this->select($prependedFields);
0 ignored issues
show
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
    }
50
51
    public function getRequestedFieldsForRelatedTable(string $relation): array
52
    {
53
        $fields = $this->request->fields()->mapWithKeys(function ($fields, $relation) {
54
            return [Str::camel($relation) => $fields];
55
        })->get($relation);
56
57
        if (! $fields) {
58
            return [];
59
        }
60
61
        if (! $this->allowedFields instanceof Collection) {
62
            // We have requested fields but no allowed fields (yet?)
63
64
            throw new UnknownIncludedFieldsQuery($fields);
65
        }
66
67
        return $fields;
68
    }
69
70
    protected function ensureAllFieldsExist()
71
    {
72
        $requestedFields = $this->request->fields()
73
            ->map(function ($fields, $model) {
74
                $tableName = Str::snake(preg_replace('/-/', '_', $model));
75
76
                $fields = array_map([Str::class, 'snake'], $fields);
77
78
                return $this->prependFieldsWithTableName($fields, $tableName);
79
            })
80
            ->flatten()
81
            ->unique();
82
83
        $unknownFields = $requestedFields->diff($this->allowedFields);
84
85
        if ($unknownFields->isNotEmpty()) {
86
            throw InvalidFieldQuery::fieldsNotAllowed($unknownFields, $this->allowedFields);
87
        }
88
    }
89
90
    protected function prependFieldsWithTableName(array $fields, string $tableName): array
91
    {
92
        return array_map(function ($field) use ($tableName) {
93
            return $this->prependField($field, $tableName);
94
        }, $fields);
95
    }
96
97
    protected function prependField(string $field, ?string $table = null): string
98
    {
99
        if (! $table) {
100
            $table = $this->getModel()->getTable();
0 ignored issues
show
It seems like getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
        }
102
103
        if (Str::contains($field, '.')) {
104
            // Already prepended
105
106
            return $field;
107
        }
108
109
        return "{$table}.{$field}";
110
    }
111
}
112