1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neurony\Url\Options; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class SlugOptions |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The field used to generate the slug from. |
11
|
|
|
* |
12
|
|
|
* @var string|array|callable |
13
|
|
|
*/ |
14
|
|
|
private $fromField; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The field where to store the generated slug. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $toField; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Flag whether slugs should be unique or not. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $uniqueSlugs = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The separator used between words in the slug. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $slugSeparator = '-'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The language used to transform the UTF-8 slug to ASCII. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $slugLanguage = 'en'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Flag whether to generate slug on model create event or not. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $generateSlugOnCreate = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Flag whether to generate slug on model update event or not. |
53
|
|
|
* |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private $generateSlugOnUpdate = true; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the value of a property of this class. |
60
|
|
|
* |
61
|
|
|
* @param $name |
62
|
|
|
* @return mixed |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function __get($name) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if (property_exists(static::class, $name)) { |
68
|
|
|
return $this->{$name}; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new Exception( |
72
|
|
|
'The property "'.$name.'" does not exist in class "'.static::class.'"' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get a fresh instance of this class. |
78
|
|
|
* |
79
|
|
|
* @return SlugOptions |
80
|
|
|
*/ |
81
|
|
|
public static function instance(): self |
82
|
|
|
{ |
83
|
|
|
return new static(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the $fromField to work with in the Neurony\Url\Traits\HasSlug trait. |
88
|
|
|
* |
89
|
|
|
* @param string|array|callable $field |
90
|
|
|
* @return SlugOptions |
91
|
|
|
*/ |
92
|
|
|
public function generateSlugFrom($field): self |
93
|
|
|
{ |
94
|
|
|
if (is_string($field)) { |
95
|
|
|
$field = [$field]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->fromField = $field; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the $toField to work with in the Neurony\Url\Traits\HasSlug trait. |
105
|
|
|
* |
106
|
|
|
* @param string $field |
107
|
|
|
* @return SlugOptions |
108
|
|
|
*/ |
109
|
|
|
public function saveSlugTo(string $field): self |
110
|
|
|
{ |
111
|
|
|
$this->toField = $field; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the $uniqueSlugs to work with in the Neurony\Url\Traits\HasSlug trait. |
118
|
|
|
* |
119
|
|
|
* @return SlugOptions |
120
|
|
|
*/ |
121
|
|
|
public function allowDuplicateSlugs(): self |
122
|
|
|
{ |
123
|
|
|
$this->uniqueSlugs = false; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the $slugSeparator to work with in the Neurony\Url\Traits\HasSlug trait. |
130
|
|
|
* |
131
|
|
|
* @param string $separator |
132
|
|
|
* @return SlugOptions |
133
|
|
|
*/ |
134
|
|
|
public function usingSeparator(string $separator): self |
135
|
|
|
{ |
136
|
|
|
$this->slugSeparator = $separator; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the $slugLanguage to work with in the Neurony\Url\Traits\HasSlug trait. |
143
|
|
|
* |
144
|
|
|
* @param string $separator |
145
|
|
|
* @return SlugOptions |
146
|
|
|
*/ |
147
|
|
|
public function usingLanguage(string $separator): self |
148
|
|
|
{ |
149
|
|
|
$this->slugLanguage = $separator; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the $generateSlugOnCreate to work with in the Neurony\Url\Traits\HasSlug trait. |
156
|
|
|
* |
157
|
|
|
* @return SlugOptions |
158
|
|
|
*/ |
159
|
|
|
public function doNotGenerateSlugOnCreate(): self |
160
|
|
|
{ |
161
|
|
|
$this->generateSlugOnCreate = false; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the $generateSlugOnUpdate to work with in the Neurony\Url\Traits\HasSlug trait. |
168
|
|
|
* |
169
|
|
|
* @return SlugOptions |
170
|
|
|
*/ |
171
|
|
|
public function doNotGenerateSlugOnUpdate(): self |
172
|
|
|
{ |
173
|
|
|
$this->generateSlugOnUpdate = false; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.