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

Tag::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 October\Rain\Database\Traits\Sluggable;
8
use October\Rain\Database\Traits\Validation;
9
use RainLab\Blog\Models\Post;
10
use GinoPane\BlogTaxonomy\Plugin;
11
12
/**
13
 * Class Tag
14
 *
15
 * @property string slug
16
 *
17
 * @package GinoPane\BlogTaxonomy\Models
18
 */
19
class Tag extends ModelAbstract
20
{
21
    use Sluggable;
22
    use Validation;
23
    use PostsRelationScopeTrait;
24
25
    const TABLE_NAME = 'ginopane_blogtaxonomy_tags';
26
27
    const CROSS_REFERENCE_TABLE_NAME = 'ginopane_blogtaxonomy_post_tag';
28
29
    /**
30
     * @var string The database table used by the model
31
     */
32
    public $table = self::TABLE_NAME;
33
34
    /**
35
     * @var array
36
     */
37
    protected $slugs = ['slug' => 'name'];
38
39
    /**
40
     * Relations
41
     *
42
     * @var array
43
     */
44
    public $belongsToMany = [
45
        'posts' => [
46
            Post::class,
47
            'table' => self::CROSS_REFERENCE_TABLE_NAME,
48
            'order' => 'published_at desc'
49
        ]
50
    ];
51
52
    /**
53
     * Fillable fields
54
     *
55
     * @var array
56
     */
57
    public $fillable = [
58
        'name',
59
        'slug',
60
    ];
61
62
    /**
63
     * Validation rules
64
     *
65
     * @var array
66
     */
67
    public $rules = [
68
        'name' => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\- ]+$/i",
69
        'slug' => "required|unique:" . self::TABLE_NAME . "|min:3|regex:/^[a-z0-9\-]+$/i"
70
    ];
71
72
    /**
73
     * Validation messages
74
     *
75
     * @var array
76
     */
77
    public $customMessages = [
78
        'name.required' => Plugin::LOCALIZATION_KEY . 'form.tags.name_required',
79
        'name.unique'   => Plugin::LOCALIZATION_KEY . 'form.tags.name_unique',
80
        'name.regex'    => Plugin::LOCALIZATION_KEY . 'form.tags.name_invalid',
81
        'name.min'      => Plugin::LOCALIZATION_KEY . 'form.tags.name_too_short',
82
    ];
83
84
    /**
85
     * The attributes on which the post list can be ordered
86
     *
87
     * @var array
88
     */
89
     public static $sortingOptions = [
90
        'name asc' => Plugin::LOCALIZATION_KEY . 'order_options.name_asc',
91
        'name desc' => Plugin::LOCALIZATION_KEY . 'order_options.name_desc',
92
        'created_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_asc',
93
        'created_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_desc',
94
        'posts_count asc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_asc',
95
        'posts_count desc' => Plugin::LOCALIZATION_KEY . 'order_options.post_count_desc',
96
        'random' => Plugin::LOCALIZATION_KEY . 'order_options.random'
97
    ];
98
99
    protected function getModelUrlParams(array $params): array
100
    {
101
        return [
102
            array_get($params, 'tag', 'tag') => $this->slug
103
        ];
104
    }
105
}
106