1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\behaviors; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\db\BaseActiveRecord; |
12
|
|
|
use yii\helpers\Inflector; |
13
|
|
|
use yii\validators\UniqueValidator; |
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL. |
18
|
|
|
* |
19
|
|
|
* To use SluggableBehavior, insert the following code to your ActiveRecord class: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* use yii\behaviors\SluggableBehavior; |
23
|
|
|
* |
24
|
|
|
* public function behaviors() |
25
|
|
|
* { |
26
|
|
|
* return [ |
27
|
|
|
* [ |
28
|
|
|
* 'class' => SluggableBehavior::className(), |
29
|
|
|
* 'attribute' => 'title', |
30
|
|
|
* // 'slugAttribute' => 'slug', |
31
|
|
|
* ], |
32
|
|
|
* ]; |
33
|
|
|
* } |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* By default, SluggableBehavior will fill the `slug` attribute with a value that can be used a slug in a URL |
37
|
|
|
* when the associated AR object is being validated. |
38
|
|
|
* |
39
|
|
|
* Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore |
40
|
|
|
* not be validated, i.e. the `slug` attribute should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model. |
41
|
|
|
* |
42
|
|
|
* If your attribute name is different, you may configure the [[slugAttribute]] property like the following: |
43
|
|
|
* |
44
|
|
|
* ```php |
45
|
|
|
* public function behaviors() |
46
|
|
|
* { |
47
|
|
|
* return [ |
48
|
|
|
* [ |
49
|
|
|
* 'class' => SluggableBehavior::className(), |
50
|
|
|
* 'slugAttribute' => 'alias', |
51
|
|
|
* ], |
52
|
|
|
* ]; |
53
|
|
|
* } |
54
|
|
|
* ``` |
55
|
|
|
* |
56
|
|
|
* @author Alexander Kochetov <[email protected]> |
57
|
|
|
* @author Paul Klimov <[email protected]> |
58
|
|
|
* @since 2.0 |
59
|
|
|
*/ |
60
|
|
|
class SluggableBehavior extends AttributeBehavior |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* @var string the attribute that will receive the slug value |
64
|
|
|
*/ |
65
|
|
|
public $slugAttribute = 'slug'; |
66
|
|
|
/** |
67
|
|
|
* @var string|array the attribute or list of attributes whose value will be converted into a slug |
68
|
|
|
*/ |
69
|
|
|
public $attribute; |
70
|
|
|
/** |
71
|
|
|
* @var string|callable the value that will be used as a slug. This can be an anonymous function |
72
|
|
|
* or an arbitrary value. If the former, the return value of the function will be used as a slug. |
73
|
|
|
* The signature of the function should be as follows, |
74
|
|
|
* |
75
|
|
|
* ```php |
76
|
|
|
* function ($event) |
77
|
|
|
* { |
78
|
|
|
* // return slug |
79
|
|
|
* } |
80
|
|
|
* ``` |
81
|
|
|
*/ |
82
|
|
|
public $value; |
83
|
|
|
/** |
84
|
|
|
* @var boolean whether to generate a new slug if it has already been generated before. |
85
|
|
|
* If true, the behavior will not generate a new slug even if [[attribute]] is changed. |
86
|
|
|
* @since 2.0.2 |
87
|
|
|
*/ |
88
|
|
|
public $immutable = false; |
89
|
|
|
/** |
90
|
|
|
* @var boolean whether to ensure generated slug value to be unique among owner class records. |
91
|
|
|
* If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt |
92
|
|
|
* generating unique slug value from based one until success. |
93
|
|
|
*/ |
94
|
|
|
public $ensureUnique = false; |
95
|
|
|
/** |
96
|
|
|
* @var array configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default |
97
|
|
|
* [[UniqueValidator]] will be used. |
98
|
|
|
* @see UniqueValidator |
99
|
|
|
*/ |
100
|
|
|
public $uniqueValidator = []; |
101
|
|
|
/** |
102
|
|
|
* @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated |
103
|
|
|
* slug is not unique. This should be a PHP callable with following signature: |
104
|
|
|
* |
105
|
|
|
* ```php |
106
|
|
|
* function ($baseSlug, $iteration, $model) |
107
|
|
|
* { |
108
|
|
|
* // return uniqueSlug |
109
|
|
|
* } |
110
|
|
|
* ``` |
111
|
|
|
* |
112
|
|
|
* If not set unique slug will be generated adding incrementing suffix to the base slug. |
113
|
|
|
*/ |
114
|
|
|
public $uniqueSlugGenerator; |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
5 |
|
public function init() |
121
|
3 |
|
{ |
122
|
5 |
|
parent::init(); |
123
|
|
|
|
124
|
5 |
|
if (empty($this->attributes)) { |
125
|
5 |
|
$this->attributes = [BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->slugAttribute]; |
126
|
5 |
|
} |
127
|
|
|
|
128
|
5 |
|
if ($this->attribute === null && $this->value === null) { |
129
|
|
|
throw new InvalidConfigException('Either "attribute" or "value" property must be specified.'); |
130
|
|
|
} |
131
|
5 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritdoc |
135
|
|
|
*/ |
136
|
5 |
|
protected function getValue($event) |
137
|
|
|
{ |
138
|
5 |
|
if ($this->attribute !== null) { |
139
|
5 |
|
if ($this->isNewSlugNeeded()) { |
140
|
5 |
|
$slugParts = []; |
141
|
5 |
|
foreach ((array) $this->attribute as $attribute) { |
142
|
5 |
|
$slugParts[] = $this->owner->{$attribute}; |
143
|
5 |
|
} |
144
|
|
|
|
145
|
5 |
|
$slug = $this->generateSlug($slugParts); |
146
|
5 |
|
} else { |
147
|
1 |
|
return $this->owner->{$this->slugAttribute}; |
148
|
|
|
} |
149
|
5 |
|
} else { |
150
|
|
|
$slug = parent::getValue($event); |
151
|
|
|
} |
152
|
|
|
|
153
|
5 |
|
return $this->ensureUnique ? $this->makeUnique($slug) : $slug; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Checks whether the new slug generation is needed |
158
|
|
|
* This method is called by [[getValue]] to check whether the new slug generation is needed. |
159
|
|
|
* You may override it to customize checking. |
160
|
|
|
* @return boolean |
161
|
|
|
* @since 2.0.7 |
162
|
|
|
*/ |
163
|
5 |
|
protected function isNewSlugNeeded() |
164
|
|
|
{ |
165
|
5 |
|
if (empty($this->owner->{$this->slugAttribute})) { |
166
|
5 |
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if ($this->immutable) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
foreach ((array)$this->attribute as $attribute) { |
174
|
1 |
|
if ($this->owner->isAttributeChanged($attribute)) { |
|
|
|
|
175
|
1 |
|
return true; |
176
|
|
|
} |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* This method is called by [[getValue]] to generate the slug. |
184
|
|
|
* You may override it to customize slug generation. |
185
|
|
|
* The default implementation calls [[\yii\helpers\Inflector::slug()]] on the input strings |
186
|
|
|
* concatenated by dashes (`-`). |
187
|
|
|
* @param array $slugParts an array of strings that should be concatenated and converted to generate the slug value. |
188
|
|
|
* @return string the conversion result. |
189
|
|
|
*/ |
190
|
5 |
|
protected function generateSlug($slugParts) |
191
|
|
|
{ |
192
|
5 |
|
return Inflector::slug(implode('-', $slugParts)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* This method is called by [[getValue]] when [[ensureUnique]] is true to generate the unique slug. |
197
|
|
|
* Calls [[generateUniqueSlug]] until generated slug is unique and returns it. |
198
|
|
|
* @param string $slug basic slug value |
199
|
|
|
* @return string unique slug |
200
|
|
|
* @see getValue |
201
|
|
|
* @see generateUniqueSlug |
202
|
|
|
* @since 2.0.7 |
203
|
|
|
*/ |
204
|
3 |
|
protected function makeUnique($slug) |
205
|
|
|
{ |
206
|
3 |
|
$uniqueSlug = $slug; |
207
|
3 |
|
$iteration = 0; |
208
|
3 |
|
while (!$this->validateSlug($uniqueSlug)) { |
209
|
2 |
|
$iteration++; |
210
|
2 |
|
$uniqueSlug = $this->generateUniqueSlug($slug, $iteration); |
211
|
2 |
|
} |
212
|
3 |
|
return $uniqueSlug; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Checks if given slug value is unique. |
217
|
|
|
* @param string $slug slug value |
218
|
|
|
* @return boolean whether slug is unique. |
219
|
|
|
*/ |
220
|
3 |
|
protected function validateSlug($slug) |
221
|
|
|
{ |
222
|
|
|
/* @var $validator UniqueValidator */ |
223
|
|
|
/* @var $model BaseActiveRecord */ |
224
|
3 |
|
$validator = Yii::createObject(array_merge( |
225
|
|
|
[ |
226
|
3 |
|
'class' => UniqueValidator::className(), |
227
|
3 |
|
], |
228
|
3 |
|
$this->uniqueValidator |
229
|
3 |
|
)); |
230
|
|
|
|
231
|
3 |
|
$model = clone $this->owner; |
232
|
3 |
|
$model->clearErrors(); |
|
|
|
|
233
|
3 |
|
$model->{$this->slugAttribute} = $slug; |
234
|
|
|
|
235
|
3 |
|
$validator->validateAttribute($model, $this->slugAttribute); |
236
|
3 |
|
return !$model->hasErrors(); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Generates slug using configured callback or increment of iteration. |
241
|
|
|
* @param string $baseSlug base slug value |
242
|
|
|
* @param integer $iteration iteration number |
243
|
|
|
* @return string new slug value |
244
|
|
|
* @throws \yii\base\InvalidConfigException |
245
|
|
|
*/ |
246
|
2 |
|
protected function generateUniqueSlug($baseSlug, $iteration) |
247
|
|
|
{ |
248
|
2 |
|
if (is_callable($this->uniqueSlugGenerator)) { |
249
|
1 |
|
return call_user_func($this->uniqueSlugGenerator, $baseSlug, $iteration, $this->owner); |
250
|
|
|
} |
251
|
1 |
|
return $baseSlug . '-' . ($iteration + 1); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: