Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f27254b9
Commit
f27254b9
authored
Aug 12, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`yii\behaviors\Sluggable` simplified
parent
d362af6e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
102 deletions
+9
-102
SluggableBehavior.php
framework/behaviors/SluggableBehavior.php
+9
-64
SluggableBehaviorTest.php
tests/unit/framework/behaviors/SluggableBehaviorTest.php
+0
-38
No files found.
framework/behaviors/SluggableBehavior.php
View file @
f27254b9
...
@@ -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 [[u
nique]] enabled and generated
* @var
callable slug unique value generator. It is used in case [[ensureU
nique]] 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
);
}
}
}
}
tests/unit/framework/behaviors/SluggableBehaviorTest.php
View file @
f27254b9
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment