Completed
Push — master ( 5b04ab...a06757 )
by ARCANEDEV
07:06
created

Category::populate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 6
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\Facades\Cache;
8
use Illuminate\Support\Str;
9
10
/**
11
 * Class     Category
12
 *
13
 * @package  Arcanesoft\Blog\Models
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @property  int             id
17
 * @property  string          name
18
 * @property  string          slug
19
 * @property  \Carbon\Carbon  created_at
20
 * @property  \Carbon\Carbon  updated_at
21
 * @property  \Carbon\Carbon  deleted_at
22
 *
23
 * @property  \Illuminate\Database\Eloquent\Collection  posts
24
 */
25
class Category extends AbstractModel
26
{
27
    /* -----------------------------------------------------------------
28
     |  Traits
29
     | -----------------------------------------------------------------
30
     */
31
32
    use SoftDeletes,
33
        HasTranslations;
34
35
    /* -----------------------------------------------------------------
36
     |  Properties
37
     | -----------------------------------------------------------------
38
     */
39
40
    /**
41
     * The database table used by the model
42
     *
43
     * @var string
44
     */
45
    protected $table    = 'categories';
46
47
    /**
48
     * The attributes that are mass assignable
49
     *
50
     * @var array
51
     */
52
    protected $fillable = ['name', 'slug'];
53
54
    /**
55
     * The attributes that should be mutated to dates.
56
     *
57
     * @var array
58
     */
59
    protected $dates    = ['deleted_at'];
60
61
    /**
62
     * The attributes that should be cast to native types.
63
     *
64
     * @var array
65
     */
66
    protected $casts = [
67
        'id' => 'integer',
68
    ];
69
70
    /* -----------------------------------------------------------------
71
     |  Relationships
72
     | -----------------------------------------------------------------
73
     */
74
75
    /**
76
     * Relationship with posts.
77
     *
78
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
79
     */
80
    public function posts()
81
    {
82
        return $this->hasMany(Post::class);
83
    }
84
85
    /* -----------------------------------------------------------------
86
     |  Getters & Setters
87
     | -----------------------------------------------------------------
88
     */
89
90
    /**
91
     * Set the name attribute.
92
     *
93
     * @param  string  $name
94
     *
95
     * @return string
96
     */
97
    public function setNameAttribute($name)
98
    {
99
        return $this->attributes['name'] = $name;
100
    }
101
102
    /**
103
     * Set the slug attribute.
104
     *
105
     * @param  string  $name
106
     *
107
     * @return string
108
     */
109
    public function setSlugAttribute($name)
110
    {
111
        return $this->attributes['slug'] = Str::slug($name);
112
    }
113
114
    /**
115
     * Get the translatable attributes.
116
     *
117
     * @return array
118
     */
119
    public function getTranslatableAttributes()
120
    {
121
        return Blog::instance()->isTranslatable() ? ['name', 'slug'] : [];
122
    }
123
124
    /* -----------------------------------------------------------------
125
     |  Main Methods
126
     | -----------------------------------------------------------------
127
     */
128
129
    /**
130
     * Create a new category.
131
     *
132
     * @param  array  $attributes
133
     *
134
     * @return self
135
     */
136
    public static function createOne(array $attributes)
137
    {
138
        $category = new self;
139
        $category->populate($attributes)->save();
140
141
        return $category;
142
    }
143
144
    /**
145
     * Update the current category.
146
     *
147
     * @param  array  $attributes
148
     *
149
     * @return self
150
     */
151
    public function updateOne(array $attributes)
152
    {
153
        $this->populate($attributes)->save();
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get the categories options for select input.
160
     *
161
     * @param  bool  $placeholder
162
     *
163
     * @return \Illuminate\Database\Eloquent\Collection
164
     */
165
    public static function getSelectOptions($placeholder = true)
166
    {
167
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
168
        $categories = Cache::remember('blog_categories_select_options', 5, function () {
169
            return self::all()->keyBy('id')->transform(function (Category $category) {
170
                return  Blog::instance()->isTranslatable()
171
                    ? implode(' / ', $category->getTranslations('name'))
172
                    : $category->name;
173
            });
174
        });
175
176
        return $placeholder
177
            ? $categories->prepend(trans('blog::categories.select-category'), 0)
178
            : $categories;
179
    }
180
181
    /* -----------------------------------------------------------------
182
     |  Check Methods
183
     | -----------------------------------------------------------------
184
     */
185
186
    /**
187
     * Check if category has posts.
188
     *
189
     * @return bool
190
     */
191
    public function hasPosts()
192
    {
193
        return ! $this->posts->isEmpty();
194
    }
195
196
    /**
197
     * Check if the category is deletable.
198
     *
199
     * @return bool
200
     */
201
    public function isDeletable()
202
    {
203
        return ! $this->hasPosts();
204
    }
205
206
    /* -----------------------------------------------------------------
207
     |  Other Methods
208
     | -----------------------------------------------------------------
209
     */
210
211
    /**
212
     * Clear the cached categories.
213
     */
214
    public static function clearCache()
215
    {
216
        cache()->forget('blog_categories_select_options');
217
    }
218
219
    /**
220
     * Fill the model with an array of attributes.
221
     *
222
     * @param  array  $attributes
223
     *
224
     * @return self
225
     */
226
    protected function populate(array $attributes)
227
    {
228
        if ( ! Blog::instance()->isTranslatable())
229
            return $this->fill($attributes);
230
231
        $this->setTranslations('name', $attributes['name'])
232
             ->setTranslations('slug', $attributes['name']);
233
234
        return $this->fill(Arr::except($attributes, ['name', 'slug']));
235
    }
236
}
237