Completed
Push — develop ( 04ec5c...00a280 )
by Abdelrahman
05:30
created

Page::setContentAttribute()   A

Complexity

Conditions 3
Paths 2

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 3
eloc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Pages\Models;
6
7
use Rinvex\Tenantable\Traits\Tenantable;
8
use Spatie\Sluggable\HasSlug;
9
use Spatie\Sluggable\SlugOptions;
10
use Spatie\EloquentSortable\Sortable;
11
use Illuminate\Database\Eloquent\Model;
12
use Rinvex\Cacheable\CacheableEloquent;
13
use Rinvex\Pages\Contracts\PageContract;
14
use Illuminate\Database\Eloquent\Builder;
15
use Rinvex\Support\Traits\HasTranslations;
16
use Rinvex\Support\Traits\ValidatingTrait;
17
use Spatie\EloquentSortable\SortableTrait;
18
19
/**
20
 * Rinvex\Pages\Models\Page.
21
 *
22
 * @property int            $id
23
 * @property string         $uri
24
 * @property string         $slug
25
 * @property string         $route
26
 * @property string         $domain
27
 * @property string         $middleware
28
 * @property array          $title
29
 * @property array          $subtitle
30
 * @property array          $excerpt
31
 * @property array          $content
32
 * @property string         $view
33
 * @property bool           $is_active
34
 * @property int            $sort_order
35
 * @property \Carbon\Carbon $created_at
36
 * @property \Carbon\Carbon $updated_at
37
 * @property \Carbon\Carbon $deleted_at
38
 *
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page active()
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page inactive()
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page ordered($direction = 'asc')
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereContent($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereCreatedAt($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDeletedAt($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDomain($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereExcerpt($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereId($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereIsActive($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereMiddleware($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSlug($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereRoute($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSortOrder($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSubtitle($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereTitle($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUpdatedAt($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUri($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereView($value)
58
 * @mixin \Eloquent
59
 */
60
class Page extends Model implements PageContract, Sortable
61
{
62
    use HasSlug;
63
    use Tenantable;
64
    use SortableTrait;
65
    use HasTranslations;
66
    use ValidatingTrait;
67
    use CacheableEloquent;
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected $fillable = [
73
        'uri',
74
        'slug',
75
        'title',
76
        'route',
77
        'subtitle',
78
        'domain',
79
        'middleware',
80
        'excerpt',
81
        'content',
82
        'view',
83
        'is_active',
84
        'sort_order',
85
    ];
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected $casts = [
91
        'uri' => 'string',
92
        'slug' => 'string',
93
        'route' => 'string',
94
        'domain' => 'string',
95
        'middleware' => 'string',
96
        'view' => 'string',
97
        'is_active' => 'boolean',
98
        'sort_order' => 'integer',
99
        'deleted_at' => 'datetime',
100
    ];
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected $observables = [
106
        'validating',
107
        'validated',
108
    ];
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public $translatable = [
114
        'title',
115
        'subtitle',
116
        'excerpt',
117
        'content',
118
    ];
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public $sortable = [
124
        'order_column_name' => 'sort_order',
125
    ];
126
127
    /**
128
     * The default rules that the model will validate against.
129
     *
130
     * @var array
131
     */
132
    protected $rules = [];
133
134
    /**
135
     * Whether the model should throw a
136
     * ValidationException if it fails validation.
137
     *
138
     * @var bool
139
     */
140
    protected $throwValidationExceptions = true;
141
142
    /**
143
     * Create a new Eloquent model instance.
144
     *
145
     * @param array $attributes
146
     */
147
    public function __construct(array $attributes = [])
148
    {
149
        parent::__construct($attributes);
150
151
        $this->setTable(config('rinvex.pages.tables.pages'));
152
        $this->setRules([
153
            '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...
154
            'slug' => 'required|alpha_dash|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 146 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...
155
            'route' => 'required|regex:/^([0-9a-z\._-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',route,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 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...
156
            'domain' => 'nullable|string|max:150',
157
            'middleware' => 'nullable|string|max:150',
158
            'title' => 'required|string|max:150',
159
            'subtitle' => 'nullable|string|max:150',
160
            'excerpt' => 'nullable|string|max:10000',
161
            'content' => 'nullable|string|max:10000000',
162
            'view' => 'required|string|max:150',
163
            'is_active' => 'sometimes|boolean',
164
            'sort_order' => 'nullable|integer|max:10000000',
165
        ]);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    protected static function boot()
172
    {
173
        parent::boot();
174
175
        // Auto generate slugs early before validation
176
        static::validating(function (self $model) {
177
            if ($model->exists && $model->getSlugOptions()->generateSlugsOnUpdate) {
178
                $model->generateSlugOnUpdate();
179
            } elseif (! $model->exists && $model->getSlugOptions()->generateSlugsOnCreate) {
180
                $model->generateSlugOnCreate();
181
            }
182
        });
183
    }
184
185
    /**
186
     * Get the active pages.
187
     *
188
     * @param \Illuminate\Database\Eloquent\Builder $builder
189
     *
190
     * @return \Illuminate\Database\Eloquent\Builder
191
     */
192
    public function scopeActive(Builder $builder): Builder
193
    {
194
        return $builder->where('is_active', true);
195
    }
196
197
    /**
198
     * Get the inactive pages.
199
     *
200
     * @param \Illuminate\Database\Eloquent\Builder $builder
201
     *
202
     * @return \Illuminate\Database\Eloquent\Builder
203
     */
204
    public function scopeInactive(Builder $builder): Builder
205
    {
206
        return $builder->where('is_active', false);
207
    }
208
209
    /**
210
     * Get the options for generating the slug.
211
     *
212
     * @return \Spatie\Sluggable\SlugOptions
213
     */
214
    public function getSlugOptions(): SlugOptions
215
    {
216
        return SlugOptions::create()
217
                          ->doNotGenerateSlugsOnUpdate()
218
                          ->generateSlugsFrom('title')
219
                          ->saveSlugsTo('slug');
220
    }
221
222
    /**
223
     * Active the page.
224
     *
225
     * @return static
226
     */
227
    public function activate(): self
228
    {
229
        $this->update(['is_active' => true]);
230
231
        return $this;
232
    }
233
234
    /**
235
     * Deactivate the page.
236
     *
237
     * @return static
238
     */
239
    public function deactivate(): self
240
    {
241
        $this->update(['is_active' => false]);
242
243
        return $this;
244
    }
245
}
246