Skip to content

Commit daaad92

Browse files
committed
Added 'unCamelCase' method + some PHPDoc aligments.
1 parent 30a8be7 commit daaad92

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/StringUtils.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class StringUtils
1010
/**
1111
* Method that checks if a string starts with another one.
1212
*
13-
* @param string $haystack The string in which we're searching in.
14-
* @param string $needle The string we are searching.
15-
* @param boolean $case If it's case sensitive or not.
16-
* @return boolean If the haystack starts by the needle.
13+
* @param string $haystack The string in which we're searching in.
14+
* @param string $needle The string we are searching.
15+
* @param boolean $case If it's case sensitive or not.
16+
* @return boolean If the haystack starts by the needle.
1717
*/
1818
public static function startsWith($haystack, $needle, $case = true)
1919
{
@@ -24,10 +24,10 @@ public static function startsWith($haystack, $needle, $case = true)
2424
/**
2525
* Method that checks if a string ends with another one.
2626
*
27-
* @param string $haystack The string in which we're searching in.
28-
* @param string $needle The string we are searching.
29-
* @param boolean $case If it's case sensitive or not.
30-
* @return boolean If the haystack ends by the needle.
27+
* @param string $haystack The string in which we're searching in.
28+
* @param string $needle The string we are searching.
29+
* @param boolean $case If it's case sensitive or not.
30+
* @return boolean If the haystack ends by the needle.
3131
*/
3232
public static function endsWith($haystack, $needle, $case = true)
3333
{
@@ -38,9 +38,9 @@ public static function endsWith($haystack, $needle, $case = true)
3838
/**
3939
* Method that returns a substring from the start of the haystack to the needle.
4040
*
41-
* @param string $haystack The string in which we're searching in.
42-
* @param string $needle The string we are searching.
43-
* @return string The string.
41+
* @param string $haystack The string in which we're searching in.
42+
* @param string $needle The string we are searching.
43+
* @return string The string.
4444
*/
4545
public static function cutBefore($haystack, $needle)
4646
{
@@ -51,9 +51,9 @@ public static function cutBefore($haystack, $needle)
5151
/**
5252
* Method that returns a substring from the end of the haystack to the needle.
5353
*
54-
* @param string $haystack The string in which we're searching in.
55-
* @param string $needle The string we are searching.
56-
* @return string The string.
54+
* @param string $haystack The string in which we're searching in.
55+
* @param string $needle The string we are searching.
56+
* @return string The string.
5757
*/
5858
public static function cutAfter($haystack, $needle)
5959
{
@@ -63,18 +63,34 @@ public static function cutAfter($haystack, $needle)
6363
}
6464

6565
/**
66-
* Returns the className of an object without its namespace
66+
* Method that returns the class name of an object without its namespace.
6767
*
68-
* @param $object
69-
* @param bool $lowerCase
70-
* @return string
68+
* @param mixed $object The object for which we want the class name.
69+
* @param bool $lowerCase If the class name should have the first char to lower case or not.
70+
* @return string The class name.
7171
*/
7272
public static function getClassName($object, $lowerCase = false)
7373
{
7474
$className = substr(strrchr(get_class($object), "\\"), 1);
7575
if ($lowerCase) {
7676
$className = lcfirst($className);
7777
}
78+
7879
return $className;
7980
}
81+
82+
/**
83+
* Method that reverses a camel case string.
84+
*
85+
* @param string $content The camel case string.
86+
* @param string $glue The glue for the compound words. Defaults to '_'.
87+
* @return string The un-camelcased string.
88+
*/
89+
public static function unCamelCase($content, $glue = '_')
90+
{
91+
$content = preg_replace('#(?<=[a-zA-Z])([A-Z])(?=[a-zA-Z])#e', "'$glue' . strtolower('$1')", $content);
92+
$content{0} = strtolower($content{0});
93+
94+
return $content;
95+
}
8096
}

0 commit comments

Comments
 (0)