1 | <?php |
||
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) |
|
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() |
|
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) |
|
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) |
|
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) |
|
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) |
|
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: