Completed
Pull Request — master (#61)
by
unknown
01:33
created

SlugOptions::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace Spatie\Sluggable;
4
5
class SlugOptions
6
{
7
    /** @var array|callable */
8
    public $generateSlugFrom;
9
10
    /** @var string */
11
    public $slugField;
12
13
    /** @var bool */
14
    public $generateUniqueSlugs = true;
15
16
    /** @var int */
17
    public $maximumLength = 250;
18
19
    /** @var bool */
20
    public $generateSlugsOnCreate = true;
21
22
    /** @var bool */
23
    public $generateSlugsOnUpdate = true;
24
25
    /** @var string */
26
    public $slugSeparator = '-';
27
28
    /** @var string */
29
    public $slugLanguage = 'en';
30
31
    /** @var \Closure */
32
    public $slugScopeTo;
33
34
    public static function create(): self
35
    {
36
        return new static();
37
    }
38
39
    public function __construct()
40
    {
41
        $this->setDefaultScope();
42
    }
43
44
    /**
45
     * set default scope, so we have always a closure
46
     */
47
    protected function setDefaultScope() {
48
        $this->slugScopeTo = function ($query) {
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
50
        };
51
    }
52
53
    /**
54
     * @param string|array|callable $fieldName
55
     *
56
     * @return \Spatie\Sluggable\SlugOptions
57
     */
58
    public function generateSlugsFrom($fieldName): self
59
    {
60
        if (is_string($fieldName)) {
61
            $fieldName = [$fieldName];
62
        }
63
64
        $this->generateSlugFrom = $fieldName;
65
66
        return $this;
67
    }
68
69
    public function saveSlugsTo(string $fieldName): self
70
    {
71
        $this->slugField = $fieldName;
72
73
        return $this;
74
    }
75
76
    public function allowDuplicateSlugs(): self
77
    {
78
        $this->generateUniqueSlugs = false;
79
80
        return $this;
81
    }
82
83
    public function slugsShouldBeNoLongerThan(int $maximumLength): self
84
    {
85
        $this->maximumLength = $maximumLength;
86
87
        return $this;
88
    }
89
90
    public function doNotGenerateSlugsOnCreate(): self
91
    {
92
        $this->generateSlugsOnCreate = false;
93
94
        return $this;
95
    }
96
97
    public function doNotGenerateSlugsOnUpdate(): self
98
    {
99
        $this->generateSlugsOnUpdate = false;
100
101
        return $this;
102
    }
103
104
    public function usingSeparator(string $separator): self
105
    {
106
        $this->slugSeparator = $separator;
107
108
        return $this;
109
    }
110
111
    public function usingLanguage(string $language): self
112
    {
113
        $this->slugLanguage = $language;
114
115
        return $this;
116
    }
117
118
    /**
119
     *
120
     * @param \Closure $closure
121
     * @return SlugOptions
122
     */
123
    public function scopeTo(\Closure $closure): self
124
    {
125
        $this->slugScopeTo = $closure;
126
127
        return $this;
128
    }
129
}
130