1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Attributes\Models; |
6
|
|
|
|
7
|
|
|
use Spatie\Sluggable\SlugOptions; |
8
|
|
|
use Rinvex\Support\Traits\HasSlug; |
9
|
|
|
use Spatie\EloquentSortable\Sortable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
12
|
|
|
use Illuminate\Database\Eloquent\Builder; |
13
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
14
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
15
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
16
|
|
|
use Rinvex\Attributes\Contracts\AttributeContract; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Rinvex\Attributes\Models\Attribute. |
21
|
|
|
* |
22
|
|
|
* @property int $id |
23
|
|
|
* @property string $slug |
24
|
|
|
* @property array $name |
25
|
|
|
* @property array $description |
26
|
|
|
* @property int $sort_order |
27
|
|
|
* @property string $group |
28
|
|
|
* @property string $type |
29
|
|
|
* @property bool $is_required |
30
|
|
|
* @property bool $is_collection |
31
|
|
|
* @property string $default |
32
|
|
|
* @property \Carbon\Carbon|null $created_at |
33
|
|
|
* @property \Carbon\Carbon|null $updated_at |
34
|
|
|
* @property array $entities |
35
|
|
|
* @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values |
36
|
|
|
* |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute ordered($direction = 'asc') |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereCreatedAt($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDefault($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDescription($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereGroup($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereId($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsCollection($value) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsRequired($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereName($value) |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSlug($value) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSortOrder($value) |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereType($value) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereUpdatedAt($value) |
50
|
|
|
* @mixin \Eloquent |
51
|
|
|
*/ |
52
|
|
|
class Attribute extends Model implements AttributeContract, Sortable |
53
|
|
|
{ |
54
|
|
|
use HasSlug; |
55
|
|
|
use SortableTrait; |
56
|
|
|
use HasTranslations; |
57
|
|
|
use ValidatingTrait; |
58
|
|
|
use CacheableEloquent; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected $fillable = [ |
64
|
|
|
'name', |
65
|
|
|
'slug', |
66
|
|
|
'description', |
67
|
|
|
'sort_order', |
68
|
|
|
'group', |
69
|
|
|
'type', |
70
|
|
|
'entities', |
71
|
|
|
'is_required', |
72
|
|
|
'is_collection', |
73
|
|
|
'default', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected $casts = [ |
80
|
|
|
'slug' => 'string', |
81
|
|
|
'sort_order' => 'integer', |
82
|
|
|
'group' => 'string', |
83
|
|
|
'type' => 'string', |
84
|
|
|
'is_required' => 'boolean', |
85
|
|
|
'is_collection' => 'boolean', |
86
|
|
|
'default' => 'string', |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
protected $observables = [ |
93
|
|
|
'validating', |
94
|
|
|
'validated', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public $translatable = [ |
101
|
|
|
'name', |
102
|
|
|
'description', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public $sortable = [ |
109
|
|
|
'order_column_name' => 'sort_order', |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* The default rules that the model will validate against. |
114
|
|
|
* |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
protected $rules = []; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Whether the model should throw a |
121
|
|
|
* ValidationException if it fails validation. |
122
|
|
|
* |
123
|
|
|
* @var bool |
124
|
|
|
*/ |
125
|
|
|
protected $throwValidationExceptions = true; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create a new Eloquent model instance. |
129
|
|
|
* |
130
|
|
|
* @param array $attributes |
131
|
|
|
*/ |
132
|
|
|
public function __construct(array $attributes = []) |
133
|
|
|
{ |
134
|
|
|
parent::__construct($attributes); |
135
|
|
|
|
136
|
|
|
$this->setTable(config('rinvex.attributes.tables.attributes')); |
137
|
|
|
$this->setRules([ |
138
|
|
|
'name' => 'required|string|max:150', |
139
|
|
|
'description' => 'nullable|string|max:10000', |
140
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.attributes.tables.attributes').',slug', |
141
|
|
|
'sort_order' => 'nullable|integer|max:10000000', |
142
|
|
|
'group' => 'nullable|string|max:150', |
143
|
|
|
'type' => 'required|string|max:150', |
144
|
|
|
'is_required' => 'sometimes|boolean', |
145
|
|
|
'is_collection' => 'sometimes|boolean', |
146
|
|
|
'default' => 'nullable|string|max:10000', |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Enforce clean groups. |
152
|
|
|
* |
153
|
|
|
* @param string $value |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function setGroupAttribute($value) |
158
|
|
|
{ |
159
|
|
|
$this->attributes['group'] = str_slug($value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Access entities relation and retrieve entity types as an array, |
164
|
|
|
* Accessors/Mutators preceeds relation value when called dynamically. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getEntitiesAttribute(): array |
169
|
|
|
{ |
170
|
|
|
return $this->entities()->pluck('entity_type')->toArray(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the attribute attached entities. |
175
|
|
|
* |
176
|
|
|
* @param \Illuminate\Support\Collection|array $value |
|
|
|
|
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function setEntitiesAttribute($entities) |
181
|
|
|
{ |
182
|
|
|
static::saved(function ($model) use ($entities) { |
|
|
|
|
183
|
|
|
$this->entities()->delete(); |
184
|
|
|
! $entities || $this->entities()->createMany(array_map(function ($entity) { |
185
|
|
|
return ['entity_type' => $entity]; |
186
|
|
|
}, $entities)); |
187
|
|
|
}); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the options for generating the slug. |
192
|
|
|
* |
193
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
194
|
|
|
*/ |
195
|
|
|
public function getSlugOptions(): SlugOptions |
196
|
|
|
{ |
197
|
|
|
return SlugOptions::create() |
198
|
|
|
->doNotGenerateSlugsOnUpdate() |
199
|
|
|
->generateSlugsFrom('name') |
200
|
|
|
->saveSlugsTo('slug'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the entities attached to this attribute. |
205
|
|
|
* |
206
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
207
|
|
|
*/ |
208
|
|
|
public function entities(): HasMany |
209
|
|
|
{ |
210
|
|
|
return $this->hasMany(config('rinvex.attributes.models.attribute_entity'), 'attribute_id', 'id'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the entities attached to this attribute. |
215
|
|
|
* |
216
|
|
|
* @param string $value |
217
|
|
|
* |
218
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
219
|
|
|
*/ |
220
|
|
|
public function values(string $value): HasMany |
221
|
|
|
{ |
222
|
|
|
return $this->hasMany($value, 'attribute_id', 'id'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.