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
|
|
|
$this->guardAgainstUnknownFields(); |
31
|
|
|
|
32
|
|
|
$this->addModelFieldsToQuery(); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function addAllRequestedFields() |
38
|
|
|
{ |
39
|
|
|
if ($this->allowedFields instanceof Collection) { |
40
|
|
|
// If we have allowed fields we will add them in the allowed fields method. |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this |
45
|
|
|
->getRequestedFields() |
46
|
|
|
->each(function (array $fields, string $table) { |
|
|
|
|
47
|
|
|
return ColumnNameSanitizer::sanitizeArray($fields); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$this->addModelFieldsToQuery(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getFieldsForRelatedTable(string $relation): array |
54
|
|
|
{ |
55
|
|
|
// This method is being called from the `allowedIncludes` section of the query builder. |
56
|
|
|
// If `allowedIncludes` is called before `allowedFields` we don't know what fields to |
57
|
|
|
// sanitize yet so we'll sanitize all of them. This is an edge case that will be fixed |
58
|
|
|
// in version 2 of the package. |
59
|
|
|
// TL;DR: Put `allowedFields` before `allowedIncludes` |
60
|
|
|
|
61
|
|
|
$fields = $this->getRequestedFields()->get($relation, []); |
62
|
|
|
|
63
|
|
|
if ($this->allowedFields instanceof Collection) { |
64
|
|
|
// AllowedFields method has already sanitized for us. |
65
|
|
|
|
66
|
|
|
return $fields; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Empty allowed fields means they're all allowed by default. |
70
|
|
|
// Sanitize all of them to be safe. |
71
|
|
|
|
72
|
|
|
return ColumnNameSanitizer::sanitizeArray($fields); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// TEMP: Below this point is sanitized |
76
|
|
|
|
77
|
|
|
protected function getRequestedFields(): Collection |
78
|
|
|
{ |
79
|
|
|
// We can't sanitize here yet because the sketchy fields might be allowed. |
80
|
|
|
|
81
|
|
|
return $this->request->fields(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function guardAgainstUnknownFields() |
85
|
|
|
{ |
86
|
|
|
$fields = $this->getRequestedFields() |
87
|
|
|
->map(function ($fields, $model) { |
88
|
|
|
$tableName = Str::snake(preg_replace('/-/', '_', $model)); |
89
|
|
|
|
90
|
|
|
$fields = array_map([Str::class, 'snake'], $fields); |
91
|
|
|
|
92
|
|
|
return $this->prependFieldsWithTableName($fields, $tableName); |
93
|
|
|
}) |
94
|
|
|
->flatten() |
95
|
|
|
->unique(); |
96
|
|
|
|
97
|
|
|
$diff = $fields->diff($this->allowedFields); |
98
|
|
|
|
99
|
|
|
if ($diff->count()) { |
100
|
|
|
throw InvalidFieldQuery::fieldsNotAllowed($diff, $this->allowedFields); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function addModelFieldsToQuery() |
105
|
|
|
{ |
106
|
|
|
$modelTableName = $this->getModel()->getTable(); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$modelFields = $this->getRequestedFields()->get($modelTableName); |
109
|
|
|
|
110
|
|
|
if (empty($modelFields)) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$prependedFields = $this->prependFieldsWithTableName($modelFields, $modelTableName); |
115
|
|
|
|
116
|
|
|
$this->select($prependedFields); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function prependFieldsWithTableName(array $fields, string $tableName): array |
120
|
|
|
{ |
121
|
|
|
return array_map(function ($field) use ($tableName) { |
122
|
|
|
return "{$tableName}.{$field}"; |
123
|
|
|
}, $fields); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.