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
d362af6e
Commit
d362af6e
authored
Aug 12, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`yii\behaviors\Sluggable` optimized
parent
b062a660
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
16 deletions
+33
-16
SluggableBehavior.php
framework/behaviors/SluggableBehavior.php
+32
-15
SluggableBehaviorTest.php
tests/unit/framework/behaviors/SluggableBehaviorTest.php
+1
-1
No files found.
framework/behaviors/SluggableBehavior.php
View file @
d362af6e
...
@@ -48,6 +48,7 @@ use yii\helpers\Inflector;
...
@@ -48,6 +48,7 @@ use yii\helpers\Inflector;
* ];
* ];
* }
* }
* ```
* ```
*
* @author Alexander Kochetov <creocoder@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
* @since 2.0
...
@@ -63,7 +64,7 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -63,7 +64,7 @@ class SluggableBehavior extends AttributeBehavior
*/
*/
public
$attribute
;
public
$attribute
;
/**
/**
* @var
mixed
the value that will be used as a slug. This can be an anonymous function
* @var
string|callable
the value that will be used as a slug. This can be an anonymous function
* or an arbitrary value. If the former, the return value of the function will be used as a slug.
* or an arbitrary value. If the former, the return value of the function will be used as a slug.
* The signature of the function should be as follows,
* The signature of the function should be as follows,
*
*
...
@@ -80,7 +81,7 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -80,7 +81,7 @@ class SluggableBehavior extends AttributeBehavior
* If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt
* If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt
* generating unique slug value from based one until success.
* generating unique slug value from based one until success.
*/
*/
public
$
u
nique
=
false
;
public
$
ensureU
nique
=
false
;
/**
/**
* @var array configuration for slug uniqueness validator. This configuration should not contain validator name
* @var array configuration for slug uniqueness validator. This configuration should not contain validator name
* and validated attributes - only options in format 'name => value' are allowed.
* and validated attributes - only options in format 'name => value' are allowed.
...
@@ -118,11 +119,7 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -118,11 +119,7 @@ class SluggableBehavior extends AttributeBehavior
parent
::
init
();
parent
::
init
();
if
(
empty
(
$this
->
attributes
))
{
if
(
empty
(
$this
->
attributes
))
{
if
(
$this
->
unique
)
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_VALIDATE
=>
$this
->
slugAttribute
];
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
$this
->
slugAttribute
];
}
else
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_VALIDATE
=>
$this
->
slugAttribute
];
}
}
}
if
(
$this
->
attribute
===
null
&&
$this
->
value
===
null
)
{
if
(
$this
->
attribute
===
null
&&
$this
->
value
===
null
)
{
...
@@ -135,23 +132,43 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -135,23 +132,43 @@ class SluggableBehavior extends AttributeBehavior
*/
*/
protected
function
getValue
(
$event
)
protected
function
getValue
(
$event
)
{
{
$isNewSlug
=
true
;
if
(
$this
->
attribute
!==
null
)
{
if
(
$this
->
attribute
!==
null
)
{
if
(
is_array
(
$this
->
attribute
))
{
if
(
is_array
(
$this
->
attribute
))
{
$attributes
=
$this
->
attribute
;
}
else
{
$attributes
=
[
$this
->
attribute
];
}
/* @var $owner BaseActiveRecord */
$owner
=
$this
->
owner
;
if
(
!
$owner
->
getIsNewRecord
()
&&
!
empty
(
$owner
->
{
$this
->
slugAttribute
}))
{
$isNewSlug
=
false
;
foreach
(
$attributes
as
$attribute
)
{
if
(
$owner
->
isAttributeChanged
(
$attribute
))
{
$isNewSlug
=
true
;
break
;
}
}
}
if
(
$isNewSlug
)
{
$slugParts
=
[];
$slugParts
=
[];
foreach
(
$
this
->
attribute
as
$attribute
)
{
foreach
(
$
attributes
as
$attribute
)
{
$slugParts
[]
=
$
this
->
owner
->
{
$attribute
};
$slugParts
[]
=
$owner
->
{
$attribute
};
}
}
$
this
->
value
=
Inflector
::
slug
(
implode
(
'-'
,
$slugParts
));
$
slug
=
Inflector
::
slug
(
implode
(
'-'
,
$slugParts
));
}
else
{
}
else
{
$
this
->
value
=
Inflector
::
slug
(
$this
->
owner
->
{
$this
->
attribute
})
;
$
slug
=
$owner
->
{
$this
->
slugAttribute
}
;
}
}
}
else
{
$slug
=
parent
::
getValue
(
$event
);
}
}
$slug
=
parent
::
getValue
(
$event
);
if
(
$this
->
unique
)
{
if
(
$this
->
ensureUnique
&&
$isNewSlug
)
{
$baseSlug
=
$slug
;
$baseSlug
=
$slug
;
$iteration
=
0
;
$iteration
=
0
;
while
(
!
$this
->
validateSlug
Unique
(
$slug
))
{
while
(
!
$this
->
validateSlug
(
$slug
))
{
$iteration
++
;
$iteration
++
;
$slug
=
$this
->
generateUniqueSlug
(
$baseSlug
,
$iteration
);
$slug
=
$this
->
generateUniqueSlug
(
$baseSlug
,
$iteration
);
}
}
...
@@ -164,7 +181,7 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -164,7 +181,7 @@ class SluggableBehavior extends AttributeBehavior
* @param string $slug slug value
* @param string $slug slug value
* @return boolean whether slug is unique.
* @return boolean whether slug is unique.
*/
*/
private
function
validateSlug
Unique
(
$slug
)
private
function
validateSlug
(
$slug
)
{
{
$validator
=
array_merge
(
$validator
=
array_merge
(
[
[
...
...
tests/unit/framework/behaviors/SluggableBehaviorTest.php
View file @
d362af6e
...
@@ -218,7 +218,7 @@ class ActiveRecordSluggableUnique extends ActiveRecordSluggable
...
@@ -218,7 +218,7 @@ class ActiveRecordSluggableUnique extends ActiveRecordSluggable
'sluggable'
=>
[
'sluggable'
=>
[
'class'
=>
SluggableBehavior
::
className
(),
'class'
=>
SluggableBehavior
::
className
(),
'attribute'
=>
'name'
,
'attribute'
=>
'name'
,
'
u
nique'
=>
true
,
'
ensureU
nique'
=>
true
,
],
],
];
];
}
}
...
...
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