-
Notifications
You must be signed in to change notification settings - Fork 6
/
SluggableBehavior.php
123 lines (107 loc) · 3.42 KB
/
SluggableBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace app\components;
use yii\db\BaseActiveRecord;
use yii\helpers\Inflector;
use Yii;
use yii\validators\UniqueValidator;
/**
* Class SluggableBehavior
* @package app\components
* @author Artem Voitko <r3verser@gmail.com>
*
*/
class SluggableBehavior extends \yii\behaviors\SluggableBehavior
{
/**
* Transliterator
* @var string
*/
public $transliterator;
/**
* Update slug attribute even it already exists
* @var bool
*/
public $forceUpdate = true;
/**
* @inheritdoc
*/
protected function getValue($event)
{
$isNewSlug = true;
if ($this->attribute !== null) {
$attributes = (array)$this->attribute;
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if (!$owner->getIsNewRecord() && !empty($owner->{$this->slugAttribute})) {
$isNewSlug = false;
foreach ($attributes as $attribute) {
if ($owner->isAttributeChanged($attribute) && $this->forceUpdate) {
$isNewSlug = true;
break;
}
}
}
if ($isNewSlug) {
$slugParts = [];
foreach ($attributes as $attribute) {
$slugParts[] = $owner->{$attribute};
}
$oldTransliterator = Inflector::$transliterator;
if (isset($this->transliterator)) {
Inflector::$transliterator = $this->transliterator;
}
$slug = Inflector::slug(implode('-', $slugParts));
Inflector::$transliterator = $oldTransliterator;
} else {
$slug = $owner->{$this->slugAttribute};
}
} else {
$slug = parent::getValue($event);
}
if ($this->ensureUnique && $isNewSlug) {
$baseSlug = $slug;
$iteration = 0;
while (!$this->validateSlug($slug)) {
$iteration++;
$slug = $this->generateUniqueSlug($baseSlug, $iteration);
}
}
return $slug;
}
/**
* Checks if given slug value is unique.
* @param string $slug slug value
* @return boolean whether slug is unique.
*/
protected function validateSlug($slug)
{
/* @var $validator UniqueValidator */
/* @var $model BaseActiveRecord */
$validator = Yii::createObject(array_merge(
[
'class' => UniqueValidator::className()
],
$this->uniqueValidator
));
$model = clone $this->owner;
$model->clearErrors();
$model->{$this->slugAttribute} = $slug;
$validator->validateAttribute($model, $this->slugAttribute);
return !$model->hasErrors();
}
/**
* Generates slug using configured callback or increment of iteration.
* @param string $baseSlug base slug value
* @param integer $iteration iteration number
* @return string new slug value
* @throws \yii\base\InvalidConfigException
*/
protected function generateUniqueSlug($baseSlug, $iteration)
{
if (is_callable($this->uniqueSlugGenerator)) {
return call_user_func($this->uniqueSlugGenerator, $baseSlug, $iteration, $this->owner);
} else {
return $baseSlug . '-' . ($iteration + 1);
}
}
}