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 Rinvex\Support\Traits\HasTranslations; |
13
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
14
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Rinvex\Attributes\Models\Attribute. |
19
|
|
|
* |
20
|
|
|
* @property int $id |
21
|
|
|
* @property string $slug |
22
|
|
|
* @property array $name |
23
|
|
|
* @property array $description |
24
|
|
|
* @property int $sort_order |
25
|
|
|
* @property string $group |
26
|
|
|
* @property string $type |
27
|
|
|
* @property bool $is_required |
28
|
|
|
* @property bool $is_collection |
29
|
|
|
* @property string $default |
30
|
|
|
* @property \Carbon\Carbon|null $created_at |
31
|
|
|
* @property \Carbon\Carbon|null $updated_at |
32
|
|
|
* @property array $entities |
33
|
|
|
* @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values |
34
|
|
|
* |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute ordered($direction = 'asc') |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereCreatedAt($value) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDefault($value) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDescription($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereGroup($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereId($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsCollection($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsRequired($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSlug($value) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSortOrder($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 whereType($value) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereUpdatedAt($value) |
48
|
|
|
* @mixin \Eloquent |
49
|
|
|
*/ |
50
|
|
|
class Attribute extends Model implements Sortable |
51
|
|
|
{ |
52
|
|
|
use HasSlug; |
53
|
|
|
use SortableTrait; |
54
|
|
|
use HasTranslations; |
55
|
|
|
use ValidatingTrait; |
56
|
|
|
use CacheableEloquent; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
protected $fillable = [ |
62
|
|
|
'name', |
63
|
|
|
'slug', |
64
|
|
|
'description', |
65
|
|
|
'sort_order', |
66
|
|
|
'group', |
67
|
|
|
'type', |
68
|
|
|
'is_required', |
69
|
|
|
'is_collection', |
70
|
|
|
'default', |
71
|
|
|
'entities', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected $casts = [ |
78
|
|
|
'slug' => 'string', |
79
|
|
|
'sort_order' => 'integer', |
80
|
|
|
'group' => 'string', |
81
|
|
|
'type' => 'string', |
82
|
|
|
'is_required' => 'boolean', |
83
|
|
|
'is_collection' => 'boolean', |
84
|
|
|
'default' => 'string', |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected $observables = [ |
91
|
|
|
'validating', |
92
|
|
|
'validated', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public $translatable = [ |
99
|
|
|
'name', |
100
|
|
|
'description', |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public $sortable = [ |
107
|
|
|
'order_column_name' => 'sort_order', |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The default rules that the model will validate against. |
112
|
|
|
* |
113
|
|
|
* @var array |
114
|
|
|
*/ |
115
|
|
|
protected $rules = []; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Whether the model should throw a |
119
|
|
|
* ValidationException if it fails validation. |
120
|
|
|
* |
121
|
|
|
* @var bool |
122
|
|
|
*/ |
123
|
|
|
protected $throwValidationExceptions = true; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* An array to map class names to their type names in database. |
127
|
|
|
* |
128
|
|
|
* @var array |
129
|
|
|
*/ |
130
|
|
|
protected static $typeMap = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a new Eloquent model instance. |
134
|
|
|
* |
135
|
|
|
* @param array $attributes |
136
|
|
|
*/ |
137
|
|
|
public function __construct(array $attributes = []) |
138
|
|
|
{ |
139
|
|
|
parent::__construct($attributes); |
140
|
|
|
|
141
|
|
|
$this->setTable(config('rinvex.attributes.tables.attributes')); |
142
|
|
|
$this->setRules([ |
143
|
|
|
'name' => 'required|string|max:150', |
144
|
|
|
'description' => 'nullable|string|max:10000', |
145
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.attributes.tables.attributes').',slug', |
146
|
|
|
'sort_order' => 'nullable|integer|max:10000000', |
147
|
|
|
'group' => 'nullable|string|max:150', |
148
|
|
|
'type' => 'required|string|max:150', |
149
|
|
|
'is_required' => 'sometimes|boolean', |
150
|
|
|
'is_collection' => 'sometimes|boolean', |
151
|
|
|
'default' => 'nullable|string|max:10000', |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Enforce clean slugs. |
157
|
|
|
* |
158
|
|
|
* @param string $value |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function setSlugAttribute($value): void |
163
|
|
|
{ |
164
|
|
|
$this->attributes['slug'] = str_slug($value, $this->getSlugOptions()->slugSeparator, $this->getSlugOptions()->slugLanguage); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set or get the type map for attribute types. |
169
|
|
|
* |
170
|
|
|
* @param array|null $map |
171
|
|
|
* @param bool $merge |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public static function typeMap(array $map = null, $merge = true) |
176
|
|
|
{ |
177
|
|
|
if (is_array($map)) { |
178
|
|
|
static::$typeMap = $merge && static::$typeMap |
|
|
|
|
179
|
|
|
? $map + static::$typeMap : $map; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return static::$typeMap; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the model associated with a custom attribute type. |
187
|
|
|
* |
188
|
|
|
* @param string $alias |
189
|
|
|
* |
190
|
|
|
* @return string|null |
191
|
|
|
*/ |
192
|
|
|
public static function getTypeModel($alias) |
193
|
|
|
{ |
194
|
|
|
return self::$typeMap[$alias] ?? null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Enforce clean groups. |
199
|
|
|
* |
200
|
|
|
* @param string $value |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function setGroupAttribute($value): void |
205
|
|
|
{ |
206
|
|
|
$this->attributes['group'] = str_slug($value); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Access entities relation and retrieve entity types as an array, |
211
|
|
|
* Accessors/Mutators preceeds relation value when called dynamically. |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function getEntitiesAttribute(): array |
216
|
|
|
{ |
217
|
|
|
return $this->entities()->pluck('entity_type')->toArray(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the attribute attached entities. |
222
|
|
|
* |
223
|
|
|
* @param \Illuminate\Support\Collection|array $value |
|
|
|
|
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function setEntitiesAttribute($entities): void |
228
|
|
|
{ |
229
|
|
|
static::saved(function ($model) use ($entities) { |
|
|
|
|
230
|
|
|
$this->entities()->delete(); |
231
|
|
|
! $entities || $this->entities()->createMany(array_map(function ($entity) { |
232
|
|
|
return ['entity_type' => $entity]; |
233
|
|
|
}, $entities)); |
234
|
|
|
}); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get the options for generating the slug. |
239
|
|
|
* |
240
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
241
|
|
|
*/ |
242
|
|
|
public function getSlugOptions(): SlugOptions |
243
|
|
|
{ |
244
|
|
|
return SlugOptions::create() |
245
|
|
|
->usingSeparator('_') |
246
|
|
|
->doNotGenerateSlugsOnUpdate() |
247
|
|
|
->generateSlugsFrom('name') |
248
|
|
|
->saveSlugsTo('slug'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get the entities attached to this attribute. |
253
|
|
|
* |
254
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
255
|
|
|
*/ |
256
|
|
|
public function entities(): HasMany |
257
|
|
|
{ |
258
|
|
|
return $this->hasMany(config('rinvex.attributes.models.attribute_entity'), 'attribute_id', 'id'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the entities attached to this attribute. |
263
|
|
|
* |
264
|
|
|
* @param string $value |
265
|
|
|
* |
266
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
267
|
|
|
*/ |
268
|
|
|
public function values(string $value): HasMany |
269
|
|
|
{ |
270
|
|
|
return $this->hasMany($value, 'attribute_id', 'id'); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.