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
7c04ff04
Commit
7c04ff04
authored
Jul 03, 2014
by
Tobias Munk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved functionality to behavior, added tests
parent
504d9fdb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
4 deletions
+15
-4
BaseInflector.php
framework/helpers/BaseInflector.php
+6
-4
InflectorTest.php
tests/unit/framework/helpers/InflectorTest.php
+9
-0
No files found.
framework/helpers/BaseInflector.php
View file @
7c04ff04
...
@@ -315,7 +315,7 @@ class BaseInflector
...
@@ -315,7 +315,7 @@ class BaseInflector
'-'
,
'-'
,
'_'
,
'_'
,
'.'
'.'
],
' '
,
preg_replace
(
'/(
[A-Z])
/'
,
' \0'
,
$name
))));
],
' '
,
preg_replace
(
'/(
?<![A-Z])[A-Z]
/'
,
' \0'
,
$name
))));
return
$ucwords
?
ucwords
(
$label
)
:
$label
;
return
$ucwords
?
ucwords
(
$label
)
:
$label
;
}
}
...
@@ -326,14 +326,16 @@ class BaseInflector
...
@@ -326,14 +326,16 @@ class BaseInflector
* For example, 'PostTag' will be converted to 'post-tag'.
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @param string $separator the character used to concatenate the words in the ID
* @param string $strict where to insert a separator between two consecutive uppercase chars, defaults to false
* @return string the resulting ID
* @return string the resulting ID
*/
*/
public
static
function
camel2id
(
$name
,
$separator
=
'-'
)
public
static
function
camel2id
(
$name
,
$separator
=
'-'
,
$strict
=
false
)
{
{
$regex
=
(
$strict
)
?
'/[A-Z]/'
:
'/(?<![A-Z])[A-Z]/'
;
if
(
$separator
===
'_'
)
{
if
(
$separator
===
'_'
)
{
return
trim
(
strtolower
(
preg_replace
(
'/([A-Z])/'
,
'_\0'
,
$name
)),
'_'
);
return
trim
(
strtolower
(
preg_replace
(
$regex
,
'_\0'
,
$name
)),
'_'
);
}
else
{
}
else
{
return
trim
(
strtolower
(
str_replace
(
'_'
,
$separator
,
preg_replace
(
'/([A-Z])/'
,
$separator
.
'\0'
,
$name
))),
$separator
);
return
trim
(
strtolower
(
str_replace
(
'_'
,
$separator
,
preg_replace
(
$regex
,
$separator
.
'\0'
,
$name
))),
$separator
);
}
}
}
}
...
...
tests/unit/framework/helpers/InflectorTest.php
View file @
7c04ff04
...
@@ -95,6 +95,12 @@ class InflectorTest extends TestCase
...
@@ -95,6 +95,12 @@ class InflectorTest extends TestCase
$this
->
assertEquals
(
'post-tag'
,
Inflector
::
camel2id
(
'postTag'
));
$this
->
assertEquals
(
'post-tag'
,
Inflector
::
camel2id
(
'postTag'
));
$this
->
assertEquals
(
'post_tag'
,
Inflector
::
camel2id
(
'postTag'
,
'_'
));
$this
->
assertEquals
(
'post_tag'
,
Inflector
::
camel2id
(
'postTag'
,
'_'
));
$this
->
assertEquals
(
'foo-ybar'
,
Inflector
::
camel2id
(
'FooYBar'
,
'-'
,
false
));
$this
->
assertEquals
(
'foo_ybar'
,
Inflector
::
camel2id
(
'fooYBar'
,
'_'
,
false
));
$this
->
assertEquals
(
'foo-y-bar'
,
Inflector
::
camel2id
(
'FooYBar'
,
'-'
,
true
));
$this
->
assertEquals
(
'foo_y_bar'
,
Inflector
::
camel2id
(
'fooYBar'
,
'_'
,
true
));
}
}
public
function
testId2camel
()
public
function
testId2camel
()
...
@@ -104,6 +110,9 @@ class InflectorTest extends TestCase
...
@@ -104,6 +110,9 @@ class InflectorTest extends TestCase
$this
->
assertEquals
(
'PostTag'
,
Inflector
::
id2camel
(
'post-tag'
));
$this
->
assertEquals
(
'PostTag'
,
Inflector
::
id2camel
(
'post-tag'
));
$this
->
assertEquals
(
'PostTag'
,
Inflector
::
id2camel
(
'post_tag'
,
'_'
));
$this
->
assertEquals
(
'PostTag'
,
Inflector
::
id2camel
(
'post_tag'
,
'_'
));
$this
->
assertEquals
(
'FooYBar'
,
Inflector
::
id2camel
(
'foo-y-bar'
));
$this
->
assertEquals
(
'FooYBar'
,
Inflector
::
id2camel
(
'foo_y_bar'
,
'_'
));
}
}
public
function
testHumanize
()
public
function
testHumanize
()
...
...
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