1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bakery\Types; |
4
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
6
|
|
|
use Bakery\Fields\Field; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Bakery\Fields\EloquentField; |
9
|
|
|
use Bakery\Support\Facades\Bakery; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Bakery\Fields\PolymorphicField; |
12
|
|
|
use Bakery\Types\Definitions\EloquentInputType; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
14
|
|
|
|
15
|
|
|
abstract class EloquentMutationInputType extends EloquentInputType |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Get the fillable fields for the input type. |
19
|
|
|
* |
20
|
|
|
* Here we grab the fillable fields from the model and filter out the |
21
|
|
|
* ones that are not a leaf type. Right now Bakery only supports passing |
22
|
|
|
* leaf types or list of leaf types as input. Complex, nested input only |
23
|
|
|
* work with relations. |
24
|
|
|
* |
25
|
|
|
* @return Collection |
26
|
|
|
*/ |
27
|
|
|
protected function getFillableFields(): Collection |
28
|
|
|
{ |
29
|
|
|
return $this->modelSchema->getFillableFields() |
30
|
|
|
->filter(function (Field $field) { |
31
|
|
|
return ! $field instanceof PolymorphicField && $field->setRegistry($this->registry)->getType()->isLeafType(); |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the fields for the relations of the model. |
37
|
|
|
* |
38
|
|
|
* @return Collection |
39
|
|
|
*/ |
40
|
|
|
protected function getRelationFields(): Collection |
41
|
|
|
{ |
42
|
|
|
$relations = $this->modelSchema->getFillableRelationFields(); |
43
|
|
|
|
44
|
|
|
return $relations->keys()->reduce(function (Collection $fields, $key) use ($relations) { |
45
|
|
|
$field = $relations[$key]; |
46
|
|
|
|
47
|
|
|
if ($field instanceof EloquentField) { |
48
|
|
|
return $fields->merge($this->getFieldsForRelation($key, $field)); |
49
|
|
|
} elseif ($field instanceof PolymorphicField) { |
50
|
|
|
return $fields->merge($this->getFieldsForPolymorphicRelation($key, $field)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $fields; |
54
|
|
|
}, collect()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the relation fields. |
59
|
|
|
* |
60
|
|
|
* @param string $relation |
61
|
|
|
* @param EloquentField $root |
62
|
|
|
* @return Collection |
63
|
|
|
*/ |
64
|
|
|
protected function getFieldsForRelation(string $relation, EloquentField $root): Collection |
65
|
|
|
{ |
66
|
|
|
$fields = collect(); |
67
|
|
|
$root->setRegistry($this->registry); |
68
|
|
|
$inputType = 'Create'.$root->getName().'Input'; |
69
|
|
|
$relationship = $root->getRelation($this->model); |
70
|
|
|
|
71
|
|
|
if ($root->isList()) { |
72
|
|
|
$name = Str::singular($relation).'Ids'; |
73
|
|
|
$field = $this->registry->field($this->registry->ID())->list()->nullable()->accessor($relation); |
74
|
|
|
$fields->put($name, $field); |
75
|
|
|
|
76
|
|
|
if ($this->registry->hasType($inputType)) { |
77
|
|
|
$field = $this->registry->field($inputType)->list()->nullable()->accessor($relation); |
78
|
|
|
$fields->put($relation, $field); |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$name = Str::singular($relation).'Id'; |
82
|
|
|
$field = $this->registry->field($this->registry->ID())->nullable()->accessor($relation); |
83
|
|
|
$fields->put($name, $field); |
84
|
|
|
|
85
|
|
|
if ($this->registry->hasType($inputType)) { |
86
|
|
|
$field = $this->registry->field($inputType)->nullable()->accessor($relation); |
87
|
|
|
$fields->put($relation, $field); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($relationship instanceof BelongsToMany) { |
92
|
|
|
$fields = $fields->merge($this->getFieldsForPivot($root, $relationship)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $fields; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the polymorphic relation fields. |
100
|
|
|
* |
101
|
|
|
* @param string $relation |
102
|
|
|
* @param \Bakery\Fields\PolymorphicField $field |
103
|
|
|
* @return Collection |
104
|
|
|
*/ |
105
|
|
|
protected function getFieldsForPolymorphicRelation(string $relation, PolymorphicField $field) |
106
|
|
|
{ |
107
|
|
|
$fields = collect(); |
108
|
|
|
$typename = Utils::typename($relation).'On'.$this->modelSchema->typename(); |
109
|
|
|
$createInputType = 'Create'.$typename.'Input'; |
110
|
|
|
$attachInputType = 'Attach'.$typename.'Input'; |
111
|
|
|
|
112
|
|
|
if ($this->registry->hasType($createInputType)) { |
113
|
|
|
if ($field->isList()) { |
114
|
|
|
$fields->put($relation, $this->registry->field($createInputType)->list()->nullable()); |
115
|
|
|
$fields->put($relation.'Ids', $this->registry->field($attachInputType)->list()->nullable()); |
116
|
|
|
} else { |
117
|
|
|
$fields->put($relation, $this->registry->field($createInputType)->nullable()); |
118
|
|
|
$fields->put($relation.'Id', $this->registry->field($attachInputType)->nullable()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $fields; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the fields for a pivot relation. |
127
|
|
|
* |
128
|
|
|
* @param \Bakery\Fields\EloquentField $field |
129
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation |
130
|
|
|
* @return Collection |
131
|
|
|
*/ |
132
|
|
|
protected function getFieldsForPivot(EloquentField $field, BelongsToMany $relation): Collection |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$fields = collect(); |
135
|
|
|
$pivot = $relation->getPivotClass(); |
136
|
|
|
$relationName = $relation->getRelationName(); |
137
|
|
|
|
138
|
|
|
if (! $this->registry->hasSchemaForModel($pivot)) { |
139
|
|
|
return collect(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$inputType = 'Create'.Utils::typename($relationName).'On'.$this->modelSchema->typename().'WithPivotInput'; |
143
|
|
|
$fields->put($relationName, $this->registry->field($inputType)->list()->nullable()); |
144
|
|
|
|
145
|
|
|
$name = Str::singular($relationName).'Ids'; |
146
|
|
|
$inputType = Utils::pluralTypename($relationName).'PivotInput'; |
147
|
|
|
$fields->put($name, $this->registry->field($inputType)->list()->nullable()); |
148
|
|
|
|
149
|
|
|
return $fields; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.