1 | <?php |
||
56 | class SluggableBehavior extends AttributeBehavior |
||
57 | { |
||
58 | /** |
||
59 | * @var string the attribute that will receive the slug value |
||
60 | */ |
||
61 | public $slugAttribute = 'slug'; |
||
62 | /** |
||
63 | * @var string|array the attribute or list of attributes whose value will be converted into a slug |
||
64 | */ |
||
65 | public $attribute; |
||
66 | /** |
||
67 | * @var string|callable the value that will be used as a slug. This can be an anonymous function |
||
68 | * or an arbitrary value. If the former, the return value of the function will be used as a slug. |
||
69 | * The signature of the function should be as follows, |
||
70 | * |
||
71 | * ```php |
||
72 | * function ($event) |
||
73 | * { |
||
74 | * // return slug |
||
75 | * } |
||
76 | * ``` |
||
77 | */ |
||
78 | public $value; |
||
79 | /** |
||
80 | * @var boolean whether to generate a new slug if it has already been generated before. |
||
81 | * If true, the behavior will not generate a new slug even if [[attribute]] is changed. |
||
82 | * @since 2.0.2 |
||
83 | */ |
||
84 | public $immutable = false; |
||
85 | /** |
||
86 | * @var boolean whether to ensure generated slug value to be unique among owner class records. |
||
87 | * If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt |
||
88 | * generating unique slug value from based one until success. |
||
89 | */ |
||
90 | public $ensureUnique = false; |
||
91 | /** |
||
92 | * @var array configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default |
||
93 | * [[UniqueValidator]] will be used. |
||
94 | * @see UniqueValidator |
||
95 | */ |
||
96 | public $uniqueValidator = []; |
||
97 | /** |
||
98 | * @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated |
||
99 | * slug is not unique. This should be a PHP callable with following signature: |
||
100 | * |
||
101 | * ```php |
||
102 | * function ($baseSlug, $iteration, $model) |
||
103 | * { |
||
104 | * // return uniqueSlug |
||
105 | * } |
||
106 | * ``` |
||
107 | * |
||
108 | * If not set unique slug will be generated adding incrementing suffix to the base slug. |
||
109 | */ |
||
110 | public $uniqueSlugGenerator; |
||
111 | |||
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 5 | public function init() |
|
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | 5 | protected function getValue($event) |
|
151 | |||
152 | /** |
||
153 | * Checks whether the new slug generation is needed |
||
154 | * This method is called by [[getValue]] to check whether the new slug generation is needed. |
||
155 | * You may override it to customize checking. |
||
156 | * @return boolean |
||
157 | * @since 2.0.7 |
||
158 | */ |
||
159 | 5 | protected function isNewSlugNeeded() |
|
177 | |||
178 | /** |
||
179 | * This method is called by [[getValue]] to generate the slug. |
||
180 | * You may override it to customize slug generation. |
||
181 | * The default implementation calls [[\yii\helpers\Inflector::slug()]] on the input strings |
||
182 | * concatenated by dashes (`-`). |
||
183 | * @param array $slugParts an array of strings that should be concatenated and converted to generate the slug value. |
||
184 | * @return string the conversion result. |
||
185 | */ |
||
186 | 5 | protected function generateSlug($slugParts) |
|
190 | |||
191 | /** |
||
192 | * This method is called by [[getValue]] when [[ensureUnique]] is true to generate the unique slug. |
||
193 | * Calls [[generateUniqueSlug]] until generated slug is unique and returns it. |
||
194 | * @param string $slug basic slug value |
||
195 | * @return string unique slug |
||
196 | * @see getValue |
||
197 | * @see generateUniqueSlug |
||
198 | * @since 2.0.7 |
||
199 | */ |
||
200 | 3 | protected function makeUnique($slug) |
|
210 | |||
211 | /** |
||
212 | * Checks if given slug value is unique. |
||
213 | * @param string $slug slug value |
||
214 | * @return boolean whether slug is unique. |
||
215 | */ |
||
216 | 3 | protected function validateSlug($slug) |
|
234 | |||
235 | /** |
||
236 | * Generates slug using configured callback or increment of iteration. |
||
237 | * @param string $baseSlug base slug value |
||
238 | * @param integer $iteration iteration number |
||
239 | * @return string new slug value |
||
240 | * @throws \yii\base\InvalidConfigException |
||
241 | */ |
||
242 | 2 | protected function generateUniqueSlug($baseSlug, $iteration) |
|
249 | } |
||
250 |
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: