Completed
Pull Request — master (#10)
by ARCANEDEV
06:09
created

AbstractTaxonomy::createMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 28
    public function setNameAttribute($name)
75
    {
76 28
        return $this->attributes['name'] = $name;
77
    }
78
79
    /**
80
     * Set the slug attribute.
81
     *
82
     * @param  string  $name
83
     *
84
     * @return string
85
     */
86 28
    public function setSlugAttribute($name)
87
    {
88 28
        return $this->attributes['slug'] = Str::slug($name);
89
    }
90
91
    /**
92
     * Get the translatable attributes.
93
     *
94
     * @return array
95
     */
96 28
    public function getTranslatableAttributes()
97
    {
98 28
        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