Commit f27254b9 by Klimov Paul

`yii\behaviors\Sluggable` simplified

parent d362af6e
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace yii\behaviors; namespace yii\behaviors;
use yii\base\DynamicModel; use yii\base\DynamicModel;
use yii\base\Exception;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
use yii\helpers\Inflector; use yii\helpers\Inflector;
...@@ -93,8 +92,8 @@ class SluggableBehavior extends AttributeBehavior ...@@ -93,8 +92,8 @@ class SluggableBehavior extends AttributeBehavior
*/ */
public $uniqueValidatorConfig = []; public $uniqueValidatorConfig = [];
/** /**
* @var string|callable slug unique value generator. It is used in case [[unique]] enabled and generated * @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated
* slug is not unique. This can be a PHP callable with following signature: * slug is not unique. This should be a PHP callable with following signature:
* *
* ```php * ```php
* function ($baseSlug, $iteration) * function ($baseSlug, $iteration)
...@@ -103,12 +102,9 @@ class SluggableBehavior extends AttributeBehavior ...@@ -103,12 +102,9 @@ class SluggableBehavior extends AttributeBehavior
* } * }
* ``` * ```
* *
* Also one of the following predefined values can be used: * If not set unique slug will be generated adding incrementing suffix to the base slug.
* - 'increment' - adds incrementing suffix to the base slug
* - 'uniqueid' - adds part of uniqueId hash string to the base slug
* - 'timestamp' - adds current UNIX timestamp to the base slug
*/ */
public $uniqueSlugGenerator = 'increment'; public $uniqueSlugGenerator;
/** /**
...@@ -196,6 +192,7 @@ class SluggableBehavior extends AttributeBehavior ...@@ -196,6 +192,7 @@ class SluggableBehavior extends AttributeBehavior
} }
/** /**
* Generates slug using configured callback or increment of iteration.
* @param string $baseSlug base slug value * @param string $baseSlug base slug value
* @param integer $iteration iteration number * @param integer $iteration iteration number
* @return string slug suffix * @return string slug suffix
...@@ -203,62 +200,10 @@ class SluggableBehavior extends AttributeBehavior ...@@ -203,62 +200,10 @@ class SluggableBehavior extends AttributeBehavior
*/ */
private function generateUniqueSlug($baseSlug, $iteration) private function generateUniqueSlug($baseSlug, $iteration)
{ {
$generator = $this->uniqueSlugGenerator; if (is_callable($this->uniqueSlugGenerator)) {
switch ($generator) { return call_user_func($this->uniqueSlugGenerator, $baseSlug, $iteration);
case 'increment': } else {
return $this->generateUniqueSlugIncrement($baseSlug, $iteration); return $baseSlug . '-' . ($iteration + 1);
case 'uniqueid':
return $this->generateUniqueSlugUniqueId($baseSlug, $iteration);
case 'timestamp':
return $this->generateSuffixSlugTimestamp($baseSlug, $iteration);
default:
if (is_callable($generator)) {
return call_user_func($generator, $baseSlug, $iteration);
}
throw new InvalidConfigException("Unrecognized slug unique suffix generator '{$generator}'.");
}
}
/**
* Generates slug using increment of iteration.
* @param string $baseSlug base slug value
* @param integer $iteration iteration number
* @return string generated suffix.
*/
protected function generateUniqueSlugIncrement($baseSlug, $iteration)
{
return $baseSlug . '-' . ($iteration + 1);
}
/**
* Generates slug using unique id.
* @param string $baseSlug base slug value
* @param integer $iteration iteration number
* @throws \yii\base\Exception
* @return string generated suffix.
*/
protected function generateUniqueSlugUniqueId($baseSlug, $iteration)
{
static $uniqueId;
if ($iteration < 2) {
$uniqueId = sha1(uniqid(get_class($this), true));
}
$subStringLength = 6 + $iteration;
if ($subStringLength > strlen($uniqueId)) {
throw new Exception('Unique id is exhausted.');
} }
return $baseSlug . '-' . substr($uniqueId, 0, $subStringLength);
}
/**
* Generates slug using current timestamp.
* @param string $baseSlug base slug value
* @param integer $iteration iteration number
* @throws \yii\base\Exception
* @return string generated suffix.
*/
protected function generateSuffixSlugTimestamp($baseSlug, $iteration)
{
return $baseSlug . '-' . (time() + $iteration - 1);
} }
} }
...@@ -119,44 +119,6 @@ class SluggableBehaviorTest extends TestCase ...@@ -119,44 +119,6 @@ class SluggableBehaviorTest extends TestCase
} }
/** /**
* @depends testUniqueByIncrement
*/
public function testUniqueByUniqueId()
{
$name = 'test name';
$model1 = new ActiveRecordSluggableUnique();
$model1->name = $name;
$model1->save();
$model2 = new ActiveRecordSluggableUnique();
$model2->sluggable->uniqueSlugGenerator = 'uniqueid';
$model2->name = $name;
$model2->save();
$this->assertNotEquals($model2->slug, $model1->slug);
}
/**
* @depends testUniqueByIncrement
*/
public function testUniqueByTimestamp()
{
$name = 'test name';
$model1 = new ActiveRecordSluggableUnique();
$model1->name = $name;
$model1->save();
$model2 = new ActiveRecordSluggableUnique();
$model2->sluggable->uniqueSlugGenerator = 'timestamp';
$model2->name = $name;
$model2->save();
$this->assertNotEquals($model2->slug, $model1->slug);
}
/**
* @depends testSlug * @depends testSlug
*/ */
public function testUpdateUnique() public function testUpdateUnique()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment