-
Notifications
You must be signed in to change notification settings - Fork 7
/
SlugBehavior.php
133 lines (114 loc) · 3.28 KB
/
SlugBehavior.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
124
125
126
127
128
129
130
131
132
133
<?php
/**
* SlugBehavior class file.
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @license http://www.opensource.org/licenses/bsd-license.php
*/
/**
* Class SlugBehavior
*
* @method CActiveRecord getOwner()
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @version 0.1
* @package yiiext/slug-behavior
*/
class SlugBehavior extends CBehavior
{
/** @var string */
public $sourceAttribute = 'title';
/** @var string */
public $slugAttribute = 'slug';
/** @var bool */
public $lowercase = true;
/** @var string */
public $delimiter = '-';
/** @var int */
public $length;
/** @var array */
public $replacements = array();
/** @var callable */
public $translator;
/** @var array */
public $scenarios = array('insert', 'update');
public function events()
{
return array(
'onBeforeValidate' => 'addSlugValidators',
);
}
public function addSlugValidators()
{
$owner = $this->getOwner();
if (in_array($owner->getScenario(), $this->scenarios)) {
$list = $owner->getValidatorList();
$list->add(CValidator::createValidator(
'validateExistsSlug',
$this,
$this->slugAttribute
));
$list->add(CValidator::createValidator(
'validateUniqueSlug',
$this,
$this->slugAttribute
));
}
}
public function validateExistsSlug()
{
$owner = $this->getOwner();
$title = $owner->getAttribute($this->sourceAttribute);
if (!empty($title)) {
$owner->setAttribute($this->slugAttribute, $this->generateSlug($title));
}
}
public function validateUniqueSlug()
{
$owner = $this->getOwner();
CValidator::createValidator('unique', $owner, $this->slugAttribute)
->validate($owner, $this->slugAttribute);
// Duplicate errors to source attribute, for show it in forms.
$owner->addErrors(array(
$this->sourceAttribute => $owner->getErrors($this->slugAttribute)
));
}
public function findBySlug($slug, $condition = '', array $params = array())
{
return $this->filterBySlug($slug)->find($condition, $params);
}
public function filterBySlug($slug, $operator = 'AND')
{
$owner = $this->getOwner();
$column = $owner->getDbConnection()
->quoteColumnName($owner->getTableAlias() . '.' . $this->slugAttribute);
$owner->getDbCriteria()
->addCondition($column . ' = :slug', $operator)
->params[':slug'] = $slug;
return $this->getOwner();
}
protected function generateSlug($string)
{
// Make sure string is in UTF-8 and strip invalid UTF-8 characters
$string = mb_convert_encoding((string) $string, 'UTF-8', mb_list_encodings());
// Make custom replacements
$string = preg_replace(array_keys($this->replacements), $this->replacements, $string);
if (is_callable($this->translator)) {
$string = call_user_func($this->translator, $string);
}
// Replace non-alphanumeric characters with our delimiter
$string = preg_replace('/[^\p{L}\p{Nd}]+/u', $this->delimiter, $string);
// Remove duplicate delimiters
$string = preg_replace('/(' . preg_quote($this->delimiter, '/') . '){2,}/', '$1', $string);
if ((int) $this->length > 0) {
$string = mb_substr($string, 0, $this->length, 'UTF-8');
}
// Remove delimiter from ends
$string = trim($string, $this->delimiter);
// Transform in lower-case
if ($this->lowercase) {
$string = mb_strtolower($string, 'UTF-8');
}
return $string;
}
}