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