|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
-
-
- namespace yii\behaviors;
-
- use yii\base\InvalidConfigException;
- use yii\db\BaseActiveRecord;
- use yii\helpers\Inflector;
- use yii\validators\UniqueValidator;
- use Yii;
-
-
- class SluggableBehavior extends AttributeBehavior
- {
-
-
- public $slugAttribute = 'slug';
-
-
- public $attribute;
-
-
- public $value;
-
-
- public $immutable = false;
-
-
- public $ensureUnique = false;
-
-
- public $uniqueValidator = [];
-
-
- public $uniqueSlugGenerator;
-
-
-
-
- public function init()
- {
- parent::init();
-
- if (empty($this->attributes)) {
- $this->attributes = [BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->slugAttribute];
- }
-
- if ($this->attribute === null && $this->value === null) {
- throw new InvalidConfigException('Either "attribute" or "value" property must be specified.');
- }
- }
-
-
-
- protected function getValue($event)
- {
- if ($this->attribute !== null) {
- if ($this->isNewSlugNeeded()) {
- $slugParts = [];
- foreach ((array) $this->attribute as $attribute) {
- $slugParts[] = $this->owner->{$attribute};
- }
-
- $slug = $this->generateSlug($slugParts);
- } else {
- return $this->owner->{$this->slugAttribute};
- }
- } else {
- $slug = parent::getValue($event);
- }
-
- return $this->ensureUnique ? $this->makeUnique($slug) : $slug;
- }
-
-
-
- protected function isNewSlugNeeded()
- {
- if (empty($this->owner->{$this->slugAttribute})) {
- return true;
- }
-
- if ($this->immutable) {
- return false;
- }
-
- foreach ((array)$this->attribute as $attribute) {
- if ($this->owner->isAttributeChanged($attribute)) {
- return true;
- }
- }
-
- return false;
- }
-
-
-
- protected function generateSlug($slugParts)
- {
- return Inflector::slug(implode('-', $slugParts));
- }
-
-
-
- protected function makeUnique($slug)
- {
- $uniqueSlug = $slug;
- $iteration = 0;
- while (!$this->validateSlug($uniqueSlug)) {
- $iteration++;
- $uniqueSlug = $this->generateUniqueSlug($slug, $iteration);
- }
- return $uniqueSlug;
- }
-
-
-
- protected function validateSlug($slug)
- {
-
-
- $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();
- }
-
-
-
- protected function generateUniqueSlug($baseSlug, $iteration)
- {
- if (is_callable($this->uniqueSlugGenerator)) {
- return call_user_func($this->uniqueSlugGenerator, $baseSlug, $iteration, $this->owner);
- }
- return $baseSlug . '-' . ($iteration + 1);
- }
- }
|