1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\QueryBuilder\ColumnNameSanitizer; |
8
|
|
|
use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery; |
9
|
|
|
|
10
|
|
|
trait AddsFieldsToQuery |
11
|
|
|
{ |
12
|
|
|
/** @var \Illuminate\Support\Collection */ |
13
|
|
|
protected $allowedFields; |
14
|
|
|
|
15
|
|
|
public function allowedFields($fields): self |
16
|
|
|
{ |
17
|
|
|
$fields = is_array($fields) ? $fields : func_get_args(); |
18
|
|
|
|
19
|
|
|
$this->allowedFields = collect($fields) |
20
|
|
|
->map(function (string $fieldName) { |
21
|
|
|
if (! Str::contains($fieldName, '.')) { |
22
|
|
|
$modelTableName = $this->getModel()->getTable(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
return "{$modelTableName}.{$fieldName}"; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $fieldName; |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
if (! $this->allowedFields->contains('*')) { |
31
|
|
|
$this->guardAgainstUnknownFields(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function parseFields() |
38
|
|
|
{ |
39
|
|
|
$this->addFieldsToQuery($this->getRequestedFields()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function addFieldsToQuery(Collection $fields) |
43
|
|
|
{ |
44
|
|
|
$modelTableName = $this->getModel()->getTable(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if ($modelFields = $fields->get($modelTableName)) { |
47
|
|
|
$sanitizedFields = ColumnNameSanitizer::sanitizeArray($modelFields); |
48
|
|
|
|
49
|
|
|
$prependedFields = $this->prependFieldsWithTableName($sanitizedFields, $modelTableName); |
50
|
|
|
|
51
|
|
|
$this->select($prependedFields); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function prependFieldsWithTableName(array $fields, string $tableName): array |
56
|
|
|
{ |
57
|
|
|
return array_map(function ($field) use ($tableName) { |
58
|
|
|
return "{$tableName}.{$field}"; |
59
|
|
|
}, $fields); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getFieldsForIncludedTable(string $relation): array |
63
|
|
|
{ |
64
|
|
|
return $this->getRequestedFields()->get($relation, []); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function guardAgainstUnknownFields() |
68
|
|
|
{ |
69
|
|
|
$fields = $this->getRequestedFields() |
70
|
|
|
->map(function ($fields, $model) { |
71
|
|
|
$tableName = Str::snake(preg_replace('/-/', '_', $model)); |
72
|
|
|
|
73
|
|
|
$fields = array_map([Str::class, 'snake'], $fields); |
74
|
|
|
|
75
|
|
|
return $this->prependFieldsWithTableName($fields, $tableName); |
76
|
|
|
}) |
77
|
|
|
->flatten() |
78
|
|
|
->unique(); |
79
|
|
|
|
80
|
|
|
$diff = $fields->diff($this->allowedFields); |
81
|
|
|
|
82
|
|
|
if ($diff->count()) { |
83
|
|
|
throw InvalidFieldQuery::fieldsNotAllowed($diff, $this->allowedFields); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getRequestedFields(): Collection |
88
|
|
|
{ |
89
|
|
|
return $this->request->fields(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.