SlugOptions::saveSlugsTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    /** @var bool */
32
    public $slugifySlugSourceString = true;//if setted to false don't call Str::slug on the slug generated automatically
33
34
    /** @var bool */
35
    public $slugifyCustomSlug = true;//if setted to false don't call Str::slug on the slug custom field
36
37
    /**
38
     * @return SlugOptions
39
     */
40
    public static function create(): SlugOptions
41
    {
42
        return new static();
43
    }
44
45
    /**
46
     * @param bool $bool
47
     * @return $this
48
     */
49
    public function slugifySourceString($bool)
50
    {
51
        $this->slugifySlugSourceString = $bool ? true : false;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param bool $bool
58
     * @return $this
59
     */
60
    public function slugifyCustomSlug($bool)
61
    {
62
        $this->slugifyCustomSlug = $bool ? true : false;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string|array|callable $fieldName
69
     *
70
     * @return \Padosoft\Sluggable\SlugOptions
71
     */
72
    public function generateSlugsFrom($fieldName): SlugOptions
73
    {
74
        $this->generateSlugFrom = $fieldName;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $fieldName
81
     * @return SlugOptions
82
     */
83
    public function saveSlugsTo(string $fieldName): SlugOptions
84
    {
85
        $this->slugField = $fieldName;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $fieldName
92
     * @return SlugOptions
93
     */
94
    public function saveCustomSlugsTo(string $fieldName): SlugOptions
95
    {
96
        $this->slugCustomField = $fieldName;
97
98
        return $this;
99
    }
100
101
    public function allowDuplicateSlugs(): SlugOptions
102
    {
103
        $this->generateUniqueSlugs = false;
104
105
        return $this;
106
    }
107
108
    public function disallowSlugIfAllSourceFieldsEmpty(): SlugOptions
109
    {
110
        $this->generateSlugIfAllSourceFieldsEmpty = false;
111
112
        return $this;
113
    }
114
115
    public function allowSlugIfAllSourceFieldsEmpty(): SlugOptions
116
    {
117
        $this->generateSlugIfAllSourceFieldsEmpty = true;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param int $maximumLength
124
     * @return SlugOptions
125
     */
126
    public function slugsShouldBeNoLongerThan(int $maximumLength): SlugOptions
127
    {
128
        $this->maximumLength = $maximumLength;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param int $maximumLength
135
     * @return SlugOptions
136
     */
137
    public function randomSlugsShouldBeNoLongerThan(int $maximumLength): SlugOptions
138
    {
139
        $this->randomUrlLen = $maximumLength;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $separator
146
     * @return SlugOptions
147
     */
148
    public function slugsSeparator(string $separator): SlugOptions
149
    {
150
        $this->separator = $separator ?? '-';
151
152
        return $this;
153
    }
154
155
    /**
156
     * Get the options for generating the slug.
157
     */
158
    public function getSlugOptionsDefault(): SlugOptions
159
    {
160
        return SlugOptions::create()
161
            ->generateSlugsFrom([
162
                'titolo',
163
                'title',
164
                ['nome', 'cognome'],
165
                ['first_name', 'last_name'],
166
                'nome',
167
                'name',
168
                'descr',
169
                'descrizione',
170
                'codice',
171
                'pcode',
172
                'id',
173
            ])
174
            ->saveSlugsTo('slug')
175
            ->saveCustomSlugsTo('slug_custom')
176
            ->slugsShouldBeNoLongerThan(251);
177
    }
178
}
179