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