1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Localization\Traits\HasTranslations; |
4
|
|
|
use Arcanesoft\Blog\Blog; |
5
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractTaxonomy |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Blog\Models |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property int id |
16
|
|
|
* @property string name |
17
|
|
|
* @property string slug |
18
|
|
|
* @property \Carbon\Carbon created_at |
19
|
|
|
* @property \Carbon\Carbon updated_at |
20
|
|
|
* @property \Carbon\Carbon deleted_at |
21
|
|
|
* |
22
|
|
|
* @property \Illuminate\Database\Eloquent\Collection posts |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractTaxonomy extends AbstractModel |
25
|
|
|
{ |
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Traits |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use SoftDeletes, |
32
|
|
|
HasTranslations; |
33
|
|
|
|
34
|
|
|
/* ----------------------------------------------------------------- |
35
|
|
|
| Properties |
36
|
|
|
| ----------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The attributes that are mass assignable |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $fillable = ['name', 'slug']; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that should be mutated to dates. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $dates = ['deleted_at']; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The attributes that should be cast to native types. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $casts = [ |
59
|
|
|
'id' => 'integer', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/* ----------------------------------------------------------------- |
63
|
|
|
| Getters & Setters |
64
|
|
|
| ----------------------------------------------------------------- |
65
|
|
|
*/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the name attribute. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
26 |
|
public function setNameAttribute($name) |
75
|
|
|
{ |
76
|
26 |
|
return $this->attributes['name'] = $name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the slug attribute. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
26 |
|
public function setSlugAttribute($name) |
87
|
|
|
{ |
88
|
26 |
|
return $this->attributes['slug'] = Str::slug($name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the translatable attributes. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
26 |
|
public function getTranslatableAttributes() |
97
|
|
|
{ |
98
|
26 |
|
return Blog::instance()->isTranslatable() ? ['name', 'slug'] : []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* ----------------------------------------------------------------- |
102
|
|
|
| Main Methods |
103
|
|
|
| ----------------------------------------------------------------- |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a new taxonomy (category or tag). |
108
|
|
|
* |
109
|
|
|
* @param array $attributes |
110
|
|
|
* |
111
|
|
|
* @return static |
112
|
|
|
*/ |
113
|
|
|
public static function createOne(array $attributes) |
114
|
|
|
{ |
115
|
24 |
|
return tap(new static, function (self $taxonomy) use ($attributes) { |
116
|
24 |
|
$attributes['slug'] = $attributes['slug'] ?? $attributes['name']; |
117
|
|
|
|
118
|
24 |
|
$taxonomy->populate($attributes)->save(); |
119
|
24 |
|
}); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create many taxonomies (categories or tags). |
124
|
|
|
* |
125
|
|
|
* @param array $taxonomies |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\Support\Collection |
128
|
|
|
*/ |
129
|
|
|
public static function createMany(array $taxonomies) |
130
|
|
|
{ |
131
|
4 |
|
return collect($taxonomies)->transform(function ($attributes) { |
132
|
4 |
|
return static::createOne($attributes); |
133
|
4 |
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Update the current taxonomy (category or tag). |
138
|
|
|
* |
139
|
|
|
* @param array $attributes |
140
|
|
|
* |
141
|
|
|
* @return static |
142
|
|
|
*/ |
143
|
8 |
|
public function updateOne(array $attributes) |
144
|
|
|
{ |
145
|
8 |
|
$this->populate($attributes)->save(); |
146
|
|
|
|
147
|
8 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/* ----------------------------------------------------------------- |
151
|
|
|
| Check Methods |
152
|
|
|
| ----------------------------------------------------------------- |
153
|
|
|
*/ |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if tag has posts. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
4 |
|
public function hasPosts() |
161
|
|
|
{ |
162
|
4 |
|
return ! $this->posts->isEmpty(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if the category is deletable. |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
4 |
|
public function isDeletable() |
171
|
|
|
{ |
172
|
4 |
|
return ! $this->hasPosts(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/* ----------------------------------------------------------------- |
176
|
|
|
| Other Methods |
177
|
|
|
| ----------------------------------------------------------------- |
178
|
|
|
*/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Fill the model with an array of attributes. |
182
|
|
|
* |
183
|
|
|
* @param array $attributes |
184
|
|
|
* |
185
|
|
|
* @return static |
186
|
|
|
*/ |
187
|
24 |
|
protected function populate(array $attributes) |
188
|
|
|
{ |
189
|
24 |
|
if ( ! empty($keys = $this->getTranslatableAttributes())) { |
190
|
8 |
|
foreach ($keys as $key) { |
191
|
8 |
|
$this->setTranslations($key, $attributes[$key] ?? []); |
192
|
|
|
} |
193
|
|
|
|
194
|
8 |
|
$attributes = Arr::except($attributes, $keys); |
195
|
|
|
} |
196
|
|
|
|
197
|
24 |
|
return $this->fill($attributes); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|