Completed
Push — master ( fe1e1a...30a91f )
by Lorenzo
03:40
created

SlugOptions::allowDuplicateSlugs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Padosoft\Sluggable;
4
5
class SlugOptions
6
{
7
    /** @var string|array|callable */
8
    public $generateSlugFrom;
9
10
    /** @var string */
11
    public $slugField;
12
13
    /** @var string */
14
    public $slugCustomField;
15
16
    /** @var bool */
17
    public $generateUniqueSlugs = true;
18
19
    /** @var bool */
20
    public $generateSlugIfAllSourceFieldsEmpty = true;
21
22
    /** @var int */
23
    public $maximumLength = 251;
24
25
    /** @var string */
26
    public $separator = '-';
27
28
    /** @var int */
29
    public $randomUrlLen=50;
30
31
32
    /**
33
     * @return SlugOptions
34
     */
35
    public static function create(): SlugOptions
36
    {
37
        return new static();
38
    }
39
40
    /**
41
     * @param string|array|callable $fieldName
42
     *
43
     * @return \Padosoft\Sluggable\SlugOptions
44
     */
45
    public function generateSlugsFrom($fieldName): SlugOptions
46
    {
47
        $this->generateSlugFrom = $fieldName;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param string $fieldName
54
     * @return SlugOptions
55
     */
56
    public function saveSlugsTo(string $fieldName): SlugOptions
57
    {
58
        $this->slugField = $fieldName;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $fieldName
65
     * @return SlugOptions
66
     */
67
    public function saveCustomSlugsTo(string $fieldName): SlugOptions
68
    {
69
        $this->slugCustomField = $fieldName;
70
71
        return $this;
72
    }
73
74
    public function allowDuplicateSlugs(): SlugOptions
75
    {
76
        $this->generateUniqueSlugs = false;
77
78
        return $this;
79
    }
80
81
    public function disallowSlugIfAllSourceFieldsEmpty(): SlugOptions
82
    {
83
        $this->generateSlugIfAllSourceFieldsEmpty = false;
84
85
        return $this;
86
    }
87
88
    public function allowSlugIfAllSourceFieldsEmpty(): SlugOptions
89
    {
90
        $this->generateSlugIfAllSourceFieldsEmpty = true;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param int $maximumLength
97
     * @return SlugOptions
98
     */
99
    public function slugsShouldBeNoLongerThan(int $maximumLength): SlugOptions
100
    {
101
        $this->maximumLength = $maximumLength;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param int $maximumLength
108
     * @return SlugOptions
109
     */
110
    public function randomSlugsShouldBeNoLongerThan(int $maximumLength): SlugOptions
111
    {
112
        $this->randomUrlLen = $maximumLength;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $separator
119
     * @return SlugOptions
120
     */
121
    public function slugsSeparator(string $separator): SlugOptions
122
    {
123
        $this->separator = $separator ?? '-';
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get the options for generating the slug.
130
     */
131
    public function getSlugOptionsDefault(): SlugOptions
132
    {
133
        return SlugOptions::create()
134
            ->generateSlugsFrom([
135
                'titolo',
136
                'title',
137
                ['nome', 'cognome'],
138
                ['first_name', 'last_name'],
139
                'nome',
140
                'name',
141
                'descr',
142
                'descrizione',
143
                'codice',
144
                'pcode',
145
                'id',
146
            ])
147
            ->saveSlugsTo('slug')
148
            ->saveCustomSlugsTo('slug_custom')
149
            ->slugsShouldBeNoLongerThan(251);
150
    }
151
}
152