Completed
Push — develop ( d1a532...6baf48 )
by Abdelrahman
21:49
created

Page::entries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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