Completed
Push — develop ( 07ee33...309d0f )
by Abdelrahman
01:05
created

Page::setTitleAttribute()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Pages\Models;
6
7
use Spatie\Sluggable\HasSlug;
8
use Spatie\Sluggable\SlugOptions;
9
use Spatie\EloquentSortable\Sortable;
10
use Watson\Validating\ValidatingTrait;
11
use Illuminate\Database\Eloquent\Model;
12
use Rinvex\Cacheable\CacheableEloquent;
13
use Spatie\Translatable\HasTranslations;
14
use Illuminate\Database\Eloquent\Builder;
15
use Rinvex\Support\Traits\UniqueInjector;
16
use Spatie\EloquentSortable\SortableTrait;
17
18
class Page extends Model implements Sortable
19
{
20
    use HasSlug;
21
    use SortableTrait;
22
    use HasTranslations;
23
    use CacheableEloquent;
24
    use ValidatingTrait, UniqueInjector
25
    {
26
        UniqueInjector::prepareUniqueRule insteadof ValidatingTrait;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected $fillable = [
33
        'uri',
34
        'slug',
35
        'title',
36
        'subtitle',
37
        'domain',
38
        'middleware',
39
        'excerpt',
40
        'content',
41
        'view',
42
        'is_active',
43
        'sort_order',
44
    ];
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected $casts = [
50
        'uri' => 'string',
51
        'slug' => 'string',
52
        'title' => 'string',
53
        'subtitle' => 'string',
54
        'domain' => 'string',
55
        'middleware' => 'string',
56
        'excerpt' => 'string',
57
        'content' => 'string',
58
        'view' => 'string',
59
        'is_active' => 'boolean',
60
        'sort_order' => 'integer',
61
        'deleted_at' => 'datetime',
62
    ];
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected $observables = [
68
        'validating',
69
        'validated',
70
    ];
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public $translatable = [
76
        'title',
77
        'subtitle',
78
        'excerpt',
79
        'content',
80
    ];
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public $sortable = [
86
        'order_column_name' => 'sort_order',
87
    ];
88
89
    /**
90
     * The default rules that the model will validate against.
91
     *
92
     * @var array
93
     */
94
    protected $rules = [];
95
96
    /**
97
     * Whether the model should throw a
98
     * ValidationException if it fails validation.
99
     *
100
     * @var bool
101
     */
102
    protected $throwValidationExceptions = true;
103
104
    /**
105
     * Create a new Eloquent model instance.
106
     *
107
     * @param array $attributes
108
     */
109
    public function __construct(array $attributes = [])
110
    {
111
        parent::__construct($attributes);
112
113
        $this->setTable(config('rinvex.pages.tables.pages'));
114
        $this->setRules([
115
            'uri' => 'required|regex:/^([0-9a-z\/_-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',uri,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 159 characters

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.

Loading history...
116
            'slug' => 'required|regex:/^([0-9a-z\._-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',slug,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 161 characters

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.

Loading history...
117
            'domain' => 'nullable|string|max:150',
118
            'middleware' => 'nullable|string|max:150',
119
            'title' => 'required|string|max:150',
120
            'subtitle' => 'nullable|string|max:150',
121
            'excerpt' => 'nullable|string|max:10000',
122
            'content' => 'required|string|max:10000000',
123
            'view' => 'required|string|max:150',
124
            'is_active' => 'sometimes|boolean',
125
            'sort_order' => 'sometimes|integer|max:10000000',
126
        ]);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected static function boot()
133
    {
134
        parent::boot();
135
136
        // Auto generate slugs early before validation
137
        static::registerModelEvent('validating', function (self $attribute) {
138
            if (! $attribute->slug) {
139
                if ($attribute->exists && $attribute->getSlugOptions()->generateSlugsOnUpdate) {
140
                    $attribute->generateSlugOnUpdate();
141
                } elseif (! $attribute->exists && $attribute->getSlugOptions()->generateSlugsOnCreate) {
142
                    $attribute->generateSlugOnCreate();
143
                }
144
            }
145
        });
146
    }
147
148
    /**
149
     * Get the active pages.
150
     *
151
     * @param \Illuminate\Database\Eloquent\Builder $builder
152
     *
153
     * @return \Illuminate\Database\Eloquent\Builder
154
     */
155
    public function scopeActive(Builder $builder): Builder
156
    {
157
        return $builder->where('is_active', true);
158
    }
159
160
    /**
161
     * Get the inactive pages.
162
     *
163
     * @param \Illuminate\Database\Eloquent\Builder $builder
164
     *
165
     * @return \Illuminate\Database\Eloquent\Builder
166
     */
167
    public function scopeInactive(Builder $builder): Builder
168
    {
169
        return $builder->where('is_active', false);
170
    }
171
172
    /**
173
     * Set the translatable title attribute.
174
     *
175
     * @param string $value
176
     *
177
     * @return void
178
     */
179
    public function setTitleAttribute($value)
180
    {
181
        $this->attributes['title'] = json_encode(! is_array($value) ? [app()->getLocale() => $value] : $value);
182
    }
183
184
    /**
185
     * Set the translatable subtitle attribute.
186
     *
187
     * @param string $value
188
     *
189
     * @return void
190
     */
191
    public function setSubtitleAttribute($value)
192
    {
193
        $this->attributes['subtitle'] = ! empty($value) ? json_encode(! is_array($value) ? [app()->getLocale() => $value] : $value) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

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.

Loading history...
194
    }
195
196
    /**
197
     * Set the translatable excerpt attribute.
198
     *
199
     * @param string $value
200
     *
201
     * @return void
202
     */
203
    public function setExcerptAttribute($value)
204
    {
205
        $this->attributes['excerpt'] = ! empty($value) ? json_encode(! is_array($value) ? [app()->getLocale() => $value] : $value) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

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.

Loading history...
206
    }
207
208
    /**
209
     * Set the translatable content attribute.
210
     *
211
     * @param string $value
212
     *
213
     * @return void
214
     */
215
    public function setContentAttribute($value)
216
    {
217
        $this->attributes['content'] = ! empty($value) ? json_encode(! is_array($value) ? [app()->getLocale() => $value] : $value) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

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.

Loading history...
218
    }
219
220
    /**
221
     * Get the options for generating the slug.
222
     *
223
     * @return \Spatie\Sluggable\SlugOptions
224
     */
225
    public function getSlugOptions(): SlugOptions
226
    {
227
        return SlugOptions::create()
228
                          ->doNotGenerateSlugsOnUpdate()
229
                          ->generateSlugsFrom('title')
230
                          ->saveSlugsTo('slug');
231
    }
232
233
    /**
234
     * Active the page.
235
     *
236
     * @return $this
237
     */
238
    public function activate(): self
239
    {
240
        $this->update(['is_active' => true]);
241
242
        return $this;
243
    }
244
245
    /**
246
     * Deactivate the page.
247
     *
248
     * @return $this
249
     */
250
    public function deactivate(): self
251
    {
252
        $this->update(['is_active' => false]);
253
254
        return $this;
255
    }
256
}
257