Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeks-semenov committed Sep 23, 2020
1 parent 6fbe770 commit 635df30
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions src/YaSlugBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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],
];
}
}

Expand All @@ -82,56 +82,47 @@ 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);
}
}
}
}

return $slug;
} else
{
} else {
return call_user_func($this->value, $event);
}
}
Expand Down

0 comments on commit 635df30

Please sign in to comment.