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
|
|
|
if ($this->ensureAllFieldsExist()) { |
30
|
|
|
$this->addRequestedModelFieldsToQuery(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function addRequestedModelFieldsToQuery() |
37
|
|
|
{ |
38
|
|
|
$modelTableName = $this->getModel()->getTable(); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
if (! $this->ensureAllFieldsExist()) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $fields; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function ensureAllFieldsExist(): bool |
75
|
|
|
{ |
76
|
|
|
$requestedFields = $this->request->fields() |
77
|
|
|
->map(function ($fields, $model) { |
78
|
|
|
$tableName = Str::snake(preg_replace('/-/', '_', $model)); |
79
|
|
|
|
80
|
|
|
$fields = array_map([Str::class, 'snake'], $fields); |
81
|
|
|
|
82
|
|
|
return $this->prependFieldsWithTableName($fields, $tableName); |
83
|
|
|
}) |
84
|
|
|
->flatten() |
85
|
|
|
->unique(); |
86
|
|
|
|
87
|
|
|
$unknownFields = $requestedFields->diff($this->allowedFields); |
88
|
|
|
|
89
|
|
|
if ($unknownFields->isNotEmpty()) { |
90
|
|
|
if ($this->throwInvalidQueryExceptions) { |
|
|
|
|
91
|
|
|
throw InvalidFieldQuery::fieldsNotAllowed($unknownFields, $this->allowedFields); |
92
|
|
|
} else { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function prependFieldsWithTableName(array $fields, string $tableName): array |
101
|
|
|
{ |
102
|
|
|
return array_map(function ($field) use ($tableName) { |
103
|
|
|
return $this->prependField($field, $tableName); |
104
|
|
|
}, $fields); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function prependField(string $field, ?string $table = null): string |
108
|
|
|
{ |
109
|
|
|
if (! $table) { |
110
|
|
|
$table = $this->getModel()->getTable(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (Str::contains($field, '.')) { |
114
|
|
|
// Already prepended |
115
|
|
|
|
116
|
|
|
return $field; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return "{$table}.{$field}"; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: