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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
7b98b424
Commit
7b98b424
authored
May 22, 2013
by
Antonio Ramirez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added inflection methods from StringHelper + some comment fixes
parent
641e6ee7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
107 additions
and
29 deletions
+107
-29
Inflector.php
framework/yii/helpers/Inflector.php
+0
-0
Inflector.php
framework/yii/helpers/base/Inflector.php
+58
-24
InflectorTest.php
tests/unit/framework/helpers/InflectorTest.php
+49
-5
No files found.
yii/helpers/Inflector.php
→
framework/
yii/helpers/Inflector.php
View file @
7b98b424
File moved
yii/helpers/base/Inflector.php
→
framework/
yii/helpers/base/Inflector.php
View file @
7b98b424
...
...
@@ -290,10 +290,11 @@ class Inflector
);
/**
* Returns the plural of a $word
*
* @param string $word the word to pluralize
* @return string
* Converts a word to its plural form.
* Note that this is for English only!
* For example, 'apple' will become 'apples', and 'child' will become 'children'.
* @param string $word the word to be pluralized
* @return string the pluralized word
*/
public
static
function
pluralize
(
$word
)
{
...
...
@@ -319,7 +320,6 @@ class Inflector
/**
* Returns the singular of the $word
*
* @param string $word the english word to singularize
* @return string Singular noun.
*/
...
...
@@ -356,7 +356,6 @@ class Inflector
/**
* Converts an underscored or CamelCase word into a English
* sentence.
*
* @param string $words
* @param bool $ucAll whether to set all words to uppercase
* @return string
...
...
@@ -370,11 +369,9 @@ class Inflector
/**
* Returns given word as CamelCased
*
* Converts a word like "send_email" to "SendEmail". It
* will remove non alphanumeric character from the word, so
* "who's online" will be converted to "WhoSOnline"
*
* @see variablize
* @param string $word the word to CamelCase
* @return string
...
...
@@ -385,19 +382,64 @@ class Inflector
}
/**
* Converts a CamelCase name into space-separated words.
* For example, 'PostTag' will be converted to 'Post Tag'.
* @param string $name the string to be converted
* @param boolean $ucwords whether to capitalize the first letter in each word
* @return string the resulting words
*/
public
static
function
camel2words
(
$name
,
$ucwords
=
true
)
{
$label
=
trim
(
strtolower
(
str_replace
(
array
(
'-'
,
'_'
,
'.'
),
' '
,
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
' \0'
,
$name
))));
return
$ucwords
?
ucwords
(
$label
)
:
$label
;
}
/**
* Converts a CamelCase name into an ID in lowercase.
* Words in the ID may be concatenated using the specified character (defaults to '-').
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @return string the resulting ID
*/
public
static
function
camel2id
(
$name
,
$separator
=
'-'
)
{
if
(
$separator
===
'_'
)
{
return
trim
(
strtolower
(
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
'_\0'
,
$name
)),
'_'
);
}
else
{
return
trim
(
strtolower
(
str_replace
(
'_'
,
$separator
,
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
$separator
.
'\0'
,
$name
))),
$separator
);
}
}
/**
* Converts an ID into a CamelCase name.
* Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
* For example, 'post-tag' is converted to 'PostTag'.
* @param string $id the ID to be converted
* @param string $separator the character used to separate the words in the ID
* @return string the resulting CamelCase name
*/
public
static
function
id2camel
(
$id
,
$separator
=
'-'
)
{
return
str_replace
(
' '
,
''
,
ucwords
(
implode
(
' '
,
explode
(
$separator
,
$id
))));
}
/**
* Converts any "CamelCased" into an "underscored_word".
*
* @param string $words the word(s) to underscore
* @return string
*/
public
static
function
underscore
(
$words
)
{
return
strtolower
(
preg_replace
(
'/(?<=\\w)([A-Z])/'
,
'_\\1'
,
$words
));
return
strtolower
(
preg_replace
(
'/(?<=\\w)([A-Z])/'
,
'_\\1'
,
$words
));
}
/**
* Returns a human-readable string from $word
*
* @param string $word the string to humanize
* @param bool $ucAll whether to set all words to uppercase or not
* @return string
...
...
@@ -409,12 +451,10 @@ class Inflector
}
/**
* Same as camelize but first char is in lowercase
*
* Same as camelize but first char is in lowercase.
* Converts a word like "send_email" to "sendEmail". It
* will remove non alphanumeric character from the word, so
* "who's online" will be converted to "whoSOnline"
*
* @param string $word to lowerCamelCase
* @return string
*/
...
...
@@ -427,7 +467,6 @@ class Inflector
/**
* Converts a class name to its table name (pluralized)
* naming conventions. For example, converts "Person" to "people"
*
* @param string $class_name the class name for getting related table_name
* @return string
*/
...
...
@@ -439,10 +478,9 @@ class Inflector
/**
* Returns a string with all spaces converted to given replacement and
* non word characters removed. Maps special characters to ASCII using
* `Inflector::$transliteration`.
*
* @param string $string An arbitrary string to convert.
* @param string $replacement The replacement to use for spaces.
* `Inflector::$transliteration`
* @param string $string An arbitrary string to convert
* @param string $replacement The replacement to use for spaces
* @return string The converted string.
*/
public
static
function
slug
(
$string
,
$replacement
=
'-'
)
...
...
@@ -459,7 +497,6 @@ class Inflector
/**
* Converts a table name to its class name. For example, converts "people" to "Person"
*
* @param string $table_name
* @return string
*/
...
...
@@ -469,10 +506,7 @@ class Inflector
}
/**
* Converts number to its ordinal English form.
*
* This method converts 13 to 13th, 2 to 2nd ...
*
* Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
* @param int $number the number to get its ordinal value
* @return string
*/
...
...
tests/unit/framework/helpers/InflectorTest.php
View file @
7b98b424
...
...
@@ -12,10 +12,29 @@ class InflectorTest extends TestCase
public
function
testPluralize
()
{
$this
->
assertEquals
(
"people"
,
Inflector
::
pluralize
(
'person'
));
$this
->
assertEquals
(
"fish"
,
Inflector
::
pluralize
(
'fish'
));
$this
->
assertEquals
(
"men"
,
Inflector
::
pluralize
(
'man'
));
$this
->
assertEquals
(
"tables"
,
Inflector
::
pluralize
(
'table'
));
$testData
=
array
(
'move'
=>
'moves'
,
'foot'
=>
'feet'
,
'child'
=>
'children'
,
'human'
=>
'humans'
,
'man'
=>
'men'
,
'staff'
=>
'staff'
,
'tooth'
=>
'teeth'
,
'person'
=>
'people'
,
'mouse'
=>
'mice'
,
'touch'
=>
'touches'
,
'hash'
=>
'hashes'
,
'shelf'
=>
'shelves'
,
'potato'
=>
'potatoes'
,
'bus'
=>
'buses'
,
'test'
=>
'tests'
,
'car'
=>
'cars'
,
);
foreach
(
$testData
as
$testIn
=>
$testOut
)
{
$this
->
assertEquals
(
$testOut
,
Inflector
::
pluralize
(
$testIn
));
$this
->
assertEquals
(
ucfirst
(
$testOut
),
ucfirst
(
Inflector
::
pluralize
(
$testIn
)));
}
}
public
function
testSingularize
()
...
...
@@ -42,10 +61,35 @@ class InflectorTest extends TestCase
$this
->
assertEquals
(
"me_my_self_and_i"
,
Inflector
::
underscore
(
'MeMySelfAndI'
));
}
public
function
testCamel2words
()
{
$this
->
assertEquals
(
'Camel Case'
,
Inflector
::
camel2words
(
'camelCase'
));
$this
->
assertEquals
(
'Lower Case'
,
Inflector
::
camel2words
(
'lower_case'
));
$this
->
assertEquals
(
'Tricky Stuff It Is Testing'
,
Inflector
::
camel2words
(
' tricky_stuff.it-is testing... '
));
}
public
function
testCamel2id
()
{
$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'
,
'_'
));
}
public
function
testId2camel
()
{
$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'
,
'_'
));
}
public
function
testHumanize
()
{
$this
->
assertEquals
(
"Me my self and i"
,
Inflector
::
humanize
(
'me_my_self_and_i'
));
$this
->
assertEquals
(
"Me My Self And
i
"
,
Inflector
::
humanize
(
'me_my_self_and_i'
),
true
);
$this
->
assertEquals
(
"Me My Self And
I
"
,
Inflector
::
humanize
(
'me_my_self_and_i'
),
true
);
}
public
function
testVariablize
()
...
...
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