Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1162 from eddieajau/robschley-normalise

Removed JString usage in JStringNormalise
  • Loading branch information...
commit 6888cf1fe9fd1be0e9884e8d307e87233262c2fd 2 parents 23825d6 + 3e3ca46
@ianmacl ianmacl authored
View
60 libraries/joomla/string/normalise.php
@@ -21,21 +21,36 @@
/**
* Method to convert a string from camel case.
*
- * @param string $input The string input.
+ * This method offers two modes. Grouped allows for splitting on groups of uppercase characters as follows:
+ *
+ * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def")
+ * "JFooBar" becomes array("J", "Foo", "Bar")
+ * "J001FooBar002" becomes array("J001", "Foo", "Bar002")
+ * "abcDef" becomes array("abc", "Def")
+ * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl")
+ * "ThisIsA_NASAAstronaut" becomes array("This", "Is", "A_NASA", "Astronaut"))
+ * "JohnFitzgerald_Kennedy" becomes array("John", "Fitzgerald_Kennedy"))
+ *
+ * Non-grouped will split strings at each uppercase character.
+ *
+ * @param string $input The string input (ASCII only).
+ * @param boolean $grouped Optionally allows splitting on groups of uppercase characters.
*
* @return string The space separated string.
*
* @since 12.1
*/
- public static function fromCamelCase($input)
+ public static function fromCamelCase($input, $grouped = false)
{
- return JString::trim(preg_replace('#([A-Z])#', ' $1', $input));
+ return $grouped
+ ? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input)
+ : trim(preg_replace('#([A-Z])#', ' $1', $input));
}
/**
* Method to convert a string into camel case.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The camel case string.
*
@@ -45,8 +60,8 @@ public static function toCamelCase($input)
{
// Convert words to uppercase and then remove spaces.
$input = self::toSpaceSeparated($input);
- $input = JString::ucwords($input);
- $input = JString::str_ireplace(' ', '', $input);
+ $input = ucwords($input);
+ $input = str_ireplace(' ', '', $input);
return $input;
}
@@ -54,7 +69,7 @@ public static function toCamelCase($input)
/**
* Method to convert a string into dash separated form.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The dash separated string.
*
@@ -63,10 +78,7 @@ public static function toCamelCase($input)
public static function toDashSeparated($input)
{
// Convert spaces and underscores to dashes.
- $input = JString::str_ireplace(array(' ', '_'), '-', $input);
-
- // Remove duplicate dashes.
- $input = preg_replace('#-+#', '-', $input);
+ $input = preg_replace('#[ \-_]+#', '-', $input);
return $input;
}
@@ -74,7 +86,7 @@ public static function toDashSeparated($input)
/**
* Method to convert a string into space separated form.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The space separated string.
*
@@ -83,10 +95,7 @@ public static function toDashSeparated($input)
public static function toSpaceSeparated($input)
{
// Convert underscores and dashes to spaces.
- $input = JString::str_ireplace(array('_', '-'), ' ', $input);
-
- // Remove duplicate spaces.
- $input = preg_replace('#\s+#', ' ', $input);
+ $input = preg_replace('#[ \-_]+#', ' ', $input);
return $input;
}
@@ -94,7 +103,7 @@ public static function toSpaceSeparated($input)
/**
* Method to convert a string into underscore separated form.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The underscore separated string.
*
@@ -103,10 +112,7 @@ public static function toSpaceSeparated($input)
public static function toUnderscoreSeparated($input)
{
// Convert spaces and dashes to underscores.
- $input = JString::str_ireplace(array(' ', '-'), '_', $input);
-
- // Remove duplicate underscores.
- $input = preg_replace('#_+#', '_', $input);
+ $input = preg_replace('#[ \-_]+#', '_', $input);
return $input;
}
@@ -114,7 +120,7 @@ public static function toUnderscoreSeparated($input)
/**
* Method to convert a string into variable form.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The variable string.
*
@@ -130,11 +136,11 @@ public static function toVariable($input)
$input = preg_replace('#^[0-9]+.*$#', '', $input);
// Lowercase the first character.
- $first = JString::substr($input, 0, 1);
- $first = JString::strtolower($first);
+ $first = substr($input, 0, 1);
+ $first = strtolower($first);
// Replace the first character with the lowercase character.
- $input = JString::substr_replace($input, $first, 0, 1);
+ $input = substr_replace($input, $first, 0, 1);
return $input;
}
@@ -142,7 +148,7 @@ public static function toVariable($input)
/**
* Method to convert a string into key form.
*
- * @param string $input The string input.
+ * @param string $input The string input (ASCII only).
*
* @return string The key string.
*
@@ -152,7 +158,7 @@ public static function toKey($input)
{
// Remove spaces and dashes, then convert to lower case.
$input = self::toUnderscoreSeparated($input);
- $input = JString::strtolower($input);
+ $input = strtolower($input);
return $input;
}
View
5 libraries/joomla/string/string.php
@@ -78,11 +78,14 @@
*
* @return array The splitted string.
*
+ * @deprecated 12.3
* @since 11.3
*/
public static function splitCamelCase($string)
{
- return preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $string);
+ JLog::add('JString::splitCamelCase has been deprecated. Use JStringNormalise::fromCamelCase.', JLog::WARNING, 'deprecated');
+
+ return JStringNormalise::fromCamelCase($string, true);
}
/**
View
39 tests/suites/unit/joomla/string/JStringNormaliseTest.php
@@ -18,8 +18,27 @@
*/
class JStringNormaliseTest extends PHPUnit_Framework_TestCase
{
+ /**
+ * @return array
+ *
+ * @since 11.3
+ */
+ public function getFromCamelCaseData()
+ {
+ return array(
+ // string, expected
+ array('FooBarABCDef', array('Foo', 'Bar', 'ABC', 'Def')),
+ array('JFooBar', array('J', 'Foo', 'Bar')),
+ array('J001FooBar002', array('J001', 'Foo', 'Bar002')),
+ array('abcDef', array('abc', 'Def')),
+ array('abc_defGhi_Jkl', array('abc_def', 'Ghi_Jkl')),
+ array('ThisIsA_NASAAstronaut', array('This', 'Is', 'A_NASA', 'Astronaut')),
+ array('JohnFitzgerald_Kennedy', array('John', 'Fitzgerald_Kennedy')),
+ );
+ }
+
/**
- * Method to test JStringNormalise::fromCamelCase().
+ * Method to test JStringNormalise::fromCamelCase(string, false).
*
* @param string $expected The expected value from the method.
* @param string $input The input value for the method.
@@ -29,11 +48,27 @@ class JStringNormaliseTest extends PHPUnit_Framework_TestCase
* @dataProvider seedFromCamelCase
* @since 11.3
*/
- public function testFromCamelCase($expected, $input)
+ public function testFromCamelCase_nongrouped($expected, $input)
{
$this->assertEquals($expected, JStringNormalise::fromCamelcase($input));
}
+ /**
+ * Method to test JStringNormalise::fromCamelCase(string, true).
+ *
+ * @param string $expected The expected value from the method.
+ * @param string $input The input value for the method.
+ *
+ * @return void
+ *
+ * @dataProvider getFromCamelCaseData
+ * @since 11.3
+ */
+ public function testFromCamelCase_grouped($input, $expected)
+ {
+ $this->assertEquals($expected, JStringNormalise::fromCamelcase($input, true));
+ }
+
/**
* Method to test JStringNormalise::toCamelCase().
*
View
24 tests/suites/unit/joomla/string/JStringTest.php
@@ -25,16 +25,6 @@ class JStringTest extends PHPUnit_Framework_TestCase
/**
* @return array
*
- * @since 11.3
- */
- public function getSplitCamelCaseData()
- {
- return JStringTest_DataSet::$splitCamelCase;
- }
-
- /**
- * @return array
- *
* @since 11.2
*/
public function getIncrementData()
@@ -265,20 +255,6 @@ public function getValidData()
/**
* @return void
*
- * @dataProvider getSplitCamelCaseData
- * @since 11.3
- */
- public function testSplitCamelCase($string, $expected)
- {
- $this->assertThat(
- JString::splitCamelCase($string),
- $this->equalTo($expected)
- );
- }
-
- /**
- * @return void
- *
* @dataProvider getIncrementData
* @since 11.2
*/
View
19 tests/suites/unit/joomla/string/TestHelpers/JString-helper-dataset.php
@@ -17,25 +17,6 @@
class JStringTest_DataSet
{
/**
- * Tests for JString::splitCamelCase.
- *
- * Each element contains $string, $expect
- *
- * @var array
- * @since 11.3
- */
- static public $splitCamelCase = array(
- // string, expected
- array('FooBarABCDef', array('Foo', 'Bar', 'ABC', 'Def')),
- array('JFooBar', array('J', 'Foo', 'Bar')),
- array('J001FooBar002', array('J001', 'Foo', 'Bar002')),
- array('abcDef', array('abc', 'Def')),
- array('abc_defGhi_Jkl', array('abc_def', 'Ghi_Jkl')),
- array('ThisIsA_NASAAstronaut', array('This', 'Is', 'A_NASA', 'Astronaut')),
- array('JohnFitzgerald_Kennedy', array('John', 'Fitzgerald_Kennedy')),
- );
-
- /**
* Tests for JString::increment.
*
* Each element contains $haystack, $needle, $offset, $expect,
Please sign in to comment.
Something went wrong with that request. Please try again.