From 635df30c3543685497f553210fd9417f745ab694 Mon Sep 17 00:00:00 2001 From: Semenov Date: Wed, 23 Sep 2020 17:53:01 +0300 Subject: [PATCH] dev --- src/YaSlugBehavior.php | 73 ++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/src/YaSlugBehavior.php b/src/YaSlugBehavior.php index ff690de..d084b43 100644 --- a/src/YaSlugBehavior.php +++ b/src/YaSlugBehavior.php @@ -5,14 +5,15 @@ * @copyright (c) 2010 SkeekS * @date 04.11.2017 */ + namespace skeeks\yii2\yaslug; + use Cocur\Slugify\RuleProvider\RuleProviderInterface; use Cocur\Slugify\Slugify; - +use yii\base\Event; use yii\base\InvalidConfigException; -use yii\db\BaseActiveRecord; use yii\behaviors\AttributeBehavior; -use yii\base\Event; +use yii\db\BaseActiveRecord; /** * Class SlugBehavior @@ -24,31 +25,31 @@ class YaSlugBehavior extends AttributeBehavior * The attribute to be generated * @var string */ - public $slugAttribute = ''; + public $slugAttribute = ''; /** * The attribute from which will be generated * @var string */ - public $attribute = ''; + public $attribute = ''; /** * Slug attribute must be unique * @var bool */ - public $ensureUnique = true; + public $ensureUnique = true; /** * Maximum length of attribute slug * @var int */ - public $maxLength = 64; + public $maxLength = 64; /** * Min length of attribute slug * @var int */ - public $minLength = 3; + public $minLength = 3; /** * @var @@ -66,13 +67,12 @@ public function init() throw new InvalidConfigException('Incorrectly configured behavior.'); } - if (empty($this->attributes)) - { + if (empty($this->attributes)) { $this->attributes = - [ - BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->slugAttribute], - BaseActiveRecord::EVENT_BEFORE_UPDATE => [$this->slugAttribute], - ]; + [ + BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->slugAttribute], + BaseActiveRecord::EVENT_BEFORE_UPDATE => [$this->slugAttribute], + ]; } } @@ -82,47 +82,39 @@ public function init() */ public function getValue($event) { - if (!$this->value) - { - if ($this->owner->{$this->slugAttribute}) - { + if (!$this->value) { + if ($this->owner->{$this->slugAttribute}) { $slug = YaSlugHelper::slugify($this->owner->{$this->slugAttribute}); - } else - { + } else { $slug = YaSlugHelper::slugify($this->owner->{$this->attribute}); } if (strlen($slug) < $this->minLength) { - $slug = $slug . "-" . md5(microtime()); + $slug = $slug . "-".md5(microtime()); } if (strlen($slug) > $this->maxLength) { $slug = substr($slug, 0, $this->maxLength); + //Это обрежет попавшие по краям символы + $slug = YaSlugHelper::slugify($slug); } - if ($this->ensureUnique) - { - if (!$this->owner->isNewRecord) - { + if ($this->ensureUnique) { + if (!$this->owner->isNewRecord) { if ($founded = $this->owner->find()->where([ - $this->slugAttribute => $slug - ])->andWhere(["!=", "id", $this->owner->id])->one()) - { - if ($last = $this->owner->find()->orderBy('id DESC')->limit(1)->one()) - { - $slug = $slug . '-' . substr(md5(microtime()), 0, 5); + $this->slugAttribute => $slug, + ])->andWhere(["!=", "id", $this->owner->id])->one()) { + if ($last = $this->owner->find()->orderBy('id DESC')->limit(1)->one()) { + $slug = $slug.'-'.substr(md5(microtime()), 0, 5); return YaSlugHelper::slugify($slug); } } - } else - { + } else { if ($founded = $this->owner->find()->where([ - $this->slugAttribute => $slug - ])->one()) - { - if ($last = $this->owner->find()->orderBy('id DESC')->limit(1)->one()) - { - $slug = $slug . '-' . substr(md5(microtime()), 0, 5); + $this->slugAttribute => $slug, + ])->one()) { + if ($last = $this->owner->find()->orderBy('id DESC')->limit(1)->one()) { + $slug = $slug.'-'.substr(md5(microtime()), 0, 5); return YaSlugHelper::slugify($slug); } } @@ -130,8 +122,7 @@ public function getValue($event) } return $slug; - } else - { + } else { return call_user_func($this->value, $event); } }