Completed
Pull Request — master (#6)
by ARCANEDEV
04:38
created

Tag::createOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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