Completed
Push — master ( a30fc0...84b57d )
by Gino
01:44
created

Series::setUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Models;
4
5
use Model;
6
use Cms\Classes\Controller;
7
use RainLab\Blog\Models\Post;
8
use GinoPane\BlogTaxonomy\Plugin;
9
use Illuminate\Support\Facades\DB;
10
use October\Rain\Database\Traits\Sluggable;
11
use October\Rain\Database\Traits\Validation;
12
13
/**
14
 * Class Series
15
 *
16
 * @property string slug
17
 *
18
 * @package GinoPane\BlogTaxonomy\Models
19
 */
20
class Series extends ModelAbstract
21
{
22
    use Validation;
23
    use Sluggable;
24
    use PostsRelationScopeTrait;
25
26
    const TABLE_NAME = 'ginopane_blogtaxonomy_series';
27
28
    /**
29
     * The database table used by the model
30
     *
31
     * @var string
32
     */
33
    public $table = self::TABLE_NAME;
34
35
    /**
36
     * Relations
37
     *
38
     * @var array
39
     */
40
    public $hasMany = [
41
        'posts' => [
42
            Post::class,
43
            'key' => self::TABLE_NAME . "_id"
44
        ],
45
    ];
46
47
    /**
48
     * Validation rules
49
     *
50
     * @var array
51
     */
52
    public $rules = [
53
        'title' => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\- ]+$/i",
54
        'slug'  => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\-]+$/i"
55
    ];
56
57
    /**
58
     * Validation messages
59
     *
60
     * @var array
61
     */
62
    public $customMessages = [
63
        'title.required' => Plugin::LOCALIZATION_KEY . 'form.series.title_required',
64
        'title.unique'   => Plugin::LOCALIZATION_KEY . 'form.series.title_unique',
65
        'title.regex'    => Plugin::LOCALIZATION_KEY . 'form.series.title_invalid',
66
        'title.min'      => Plugin::LOCALIZATION_KEY . 'form.series.title_too_short',
67
    ];
68
69
    /**
70
     * The attributes on which the post list can be ordered
71
     *
72
     * @var array
73
     */
74
    public static $sortingOptions = [
75
        'title asc' => Plugin::LOCALIZATION_KEY . 'order_options.title_asc',
76
        'title desc' => Plugin::LOCALIZATION_KEY . 'order_options.title_desc',
77
        'created_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_asc',
78
        'created_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_desc',
79
        'posts_count asc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_asc',
80
        'posts_count desc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_desc',
81
        'random' => Plugin::LOCALIZATION_KEY . 'order_options.random'
82
    ];
83
84
    /**
85
     * @var array
86
     */
87
    protected $slugs = ['slug' => 'title'];
88
89
    /**
90
     * @return mixed
91
     */
92
    public function getPostCountAttribute()
93
    {
94
        return $this->posts()->isPublished()->count();
95
    }
96
97
    protected function getModelUrlParams(array $params): array
98
    {
99
        return [
100
            array_get($params, 'series', 'series') => $this->slug
101
        ];
102
    }
103
}
104