1 | <?php |
||
61 | class SluggableBehavior extends AttributeBehavior |
||
62 | { |
||
63 | /** |
||
64 | * @var string the attribute that will receive the slug value |
||
65 | */ |
||
66 | public $slugAttribute = 'slug'; |
||
67 | /** |
||
68 | * @var string|array the attribute or list of attributes whose value will be converted into a slug |
||
69 | */ |
||
70 | public $attribute; |
||
71 | /** |
||
72 | * @var string|callable the value that will be used as a slug. This can be an anonymous function |
||
73 | * or an arbitrary value. If the former, the return value of the function will be used as a slug. |
||
74 | * The signature of the function should be as follows, |
||
75 | * |
||
76 | * ```php |
||
77 | * function ($event) |
||
78 | * { |
||
79 | * // return slug |
||
80 | * } |
||
81 | * ``` |
||
82 | */ |
||
83 | public $value; |
||
84 | /** |
||
85 | * @var bool whether to generate a new slug if it has already been generated before. |
||
86 | * If true, the behavior will not generate a new slug even if [[attribute]] is changed. |
||
87 | * @since 2.0.2 |
||
88 | */ |
||
89 | public $immutable = false; |
||
90 | /** |
||
91 | * @var bool whether to ensure generated slug value to be unique among owner class records. |
||
92 | * If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt |
||
93 | * generating unique slug value from based one until success. |
||
94 | */ |
||
95 | public $ensureUnique = false; |
||
96 | /** |
||
97 | * @var array configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default |
||
98 | * [[UniqueValidator]] will be used. |
||
99 | * @see UniqueValidator |
||
100 | */ |
||
101 | public $uniqueValidator = []; |
||
102 | /** |
||
103 | * @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated |
||
104 | * slug is not unique. This should be a PHP callable with following signature: |
||
105 | * |
||
106 | * ```php |
||
107 | * function ($baseSlug, $iteration, $model) |
||
108 | * { |
||
109 | * // return uniqueSlug |
||
110 | * } |
||
111 | * ``` |
||
112 | * |
||
113 | * If not set unique slug will be generated adding incrementing suffix to the base slug. |
||
114 | */ |
||
115 | public $uniqueSlugGenerator; |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | 6 | public function init() |
|
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 6 | protected function getValue($event) |
|
156 | |||
157 | /** |
||
158 | * Checks whether the new slug generation is needed |
||
159 | * This method is called by [[getValue]] to check whether the new slug generation is needed. |
||
160 | * You may override it to customize checking. |
||
161 | * @return bool |
||
162 | * @since 2.0.7 |
||
163 | */ |
||
164 | 6 | protected function isNewSlugNeeded() |
|
182 | |||
183 | /** |
||
184 | * This method is called by [[getValue]] to generate the slug. |
||
185 | * You may override it to customize slug generation. |
||
186 | * The default implementation calls [[\yii\helpers\Inflector::slug()]] on the input strings |
||
187 | * concatenated by dashes (`-`). |
||
188 | * @param array $slugParts an array of strings that should be concatenated and converted to generate the slug value. |
||
189 | * @return string the conversion result. |
||
190 | */ |
||
191 | 6 | protected function generateSlug($slugParts) |
|
195 | |||
196 | /** |
||
197 | * This method is called by [[getValue]] when [[ensureUnique]] is true to generate the unique slug. |
||
198 | * Calls [[generateUniqueSlug]] until generated slug is unique and returns it. |
||
199 | * @param string $slug basic slug value |
||
200 | * @return string unique slug |
||
201 | * @see getValue |
||
202 | * @see generateUniqueSlug |
||
203 | * @since 2.0.7 |
||
204 | */ |
||
205 | 6 | protected function makeUnique($slug) |
|
206 | { |
||
207 | 3 | $uniqueSlug = $slug; |
|
208 | 3 | $iteration = 0; |
|
209 | 3 | while (!$this->validateSlug($uniqueSlug)) { |
|
210 | 2 | $iteration++; |
|
211 | 2 | $uniqueSlug = $this->generateUniqueSlug($slug, $iteration); |
|
212 | 2 | } |
|
213 | 3 | return $uniqueSlug; |
|
214 | 6 | } |
|
215 | |||
216 | /** |
||
217 | * Checks if given slug value is unique. |
||
218 | * @param string $slug slug value |
||
219 | * @return bool whether slug is unique. |
||
220 | */ |
||
221 | 3 | protected function validateSlug($slug) |
|
239 | |||
240 | /** |
||
241 | * Generates slug using configured callback or increment of iteration. |
||
242 | * @param string $baseSlug base slug value |
||
243 | * @param int $iteration iteration number |
||
244 | * @return string new slug value |
||
245 | * @throws \yii\base\InvalidConfigException |
||
246 | */ |
||
247 | 2 | protected function generateUniqueSlug($baseSlug, $iteration) |
|
254 | } |
||
255 |
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: