mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-13 16:53:59 +02:00
Add dasherize, underscored and swapCase
This commit is contained in:
36
README.md
36
README.md
@@ -56,18 +56,44 @@ spaces, dashes and underscores, and removes spaces, dashes, underscores.
|
||||
S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase'
|
||||
```
|
||||
|
||||
TODO
|
||||
----
|
||||
|
||||
**dasherize**
|
||||
|
||||
S::dasherize($string [, $encoding])
|
||||
|
||||
Returns a lowercase and trimmed string seperated by dashes, with
|
||||
multibyte support. Dashes are inserted before uppercase characters
|
||||
(with the exception of the first character of the string), and in place
|
||||
of spaces as well as underscores.
|
||||
|
||||
```php
|
||||
S::dasherize('TestDCase'); // 'test-d-case'
|
||||
```
|
||||
|
||||
**underscored**
|
||||
|
||||
S::underscored($string [, $encoding])
|
||||
|
||||
Returns a lowercase and trimmed string seperated by underscores, with
|
||||
multibyte support. Underscores are inserted before uppercase characters
|
||||
(with the exception of the first character of the string), and in place
|
||||
of spaces as well as dashes.
|
||||
|
||||
```php
|
||||
S::underscored('TestUCase'); // 'test_u_case'
|
||||
```
|
||||
|
||||
**swapCase**
|
||||
|
||||
**dashes**
|
||||
S::swapCase($string [, $encoding])
|
||||
|
||||
**underscores**
|
||||
Returns a case swapped version of a string, with multibyte support.
|
||||
|
||||
```php
|
||||
S::swapCase('Ντανιλ', 'UTF-8'); // 'νΤΑΝΙΛ'
|
||||
```
|
||||
|
||||
TODO
|
||||
----
|
||||
|
||||
**title**
|
||||
|
||||
|
@@ -10,7 +10,6 @@
|
||||
"homepage": "http://www.danielstjules.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
|
@@ -32,6 +32,9 @@ class Stringy {
|
||||
if (sizeof($args) == 1 || !$args[1])
|
||||
$args[1] = mb_internal_encoding();
|
||||
|
||||
// Set character encoding for multibyte regex
|
||||
mb_regex_encoding($args[1]);
|
||||
|
||||
return forward_static_call(array(__CLASS__, $method), $args[0], $args[1]);
|
||||
}
|
||||
|
||||
@@ -106,6 +109,59 @@ class Stringy {
|
||||
|
||||
return self::upperCaseFirst($camelCase, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by dashes, with
|
||||
* multibyte support. Dashes are inserted before uppercase characters
|
||||
* (with the exception of the first character of the string), and in place
|
||||
* of spaces as well as underscores.
|
||||
*
|
||||
* @param string $string String to convert
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
private static function dasherize($string, $encoding) {
|
||||
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', trim($string));
|
||||
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
|
||||
|
||||
return mb_strtolower($dasherized, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by underscores, with
|
||||
* multibyte support. Underscores are inserted before uppercase characters
|
||||
* (with the exception of the first character of the string), and in place
|
||||
* of spaces as well as dashes.
|
||||
*
|
||||
* @param string $string String to convert
|
||||
* @return string Underscored string
|
||||
*/
|
||||
private static function underscored($string, $encoding) {
|
||||
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', trim($string));
|
||||
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
|
||||
|
||||
return mb_strtolower($underscored, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of a string, with multibyte support.
|
||||
*
|
||||
* @param string $string String to swap case
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
private static function swapCase($string, $encoding) {
|
||||
$swapped = preg_replace_callback(
|
||||
'/[\S]/u',
|
||||
function ($match) use (&$encoding) {
|
||||
if ($match[0] == mb_strtoupper($match[0], $encoding))
|
||||
return mb_strtolower($match[0], $encoding);
|
||||
else
|
||||
return mb_strtoupper($match[0], $encoding);
|
||||
},
|
||||
$string
|
||||
);
|
||||
|
||||
return $swapped;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@@ -77,7 +77,7 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
||||
array('string_with1number', 'stringWith1Number'),
|
||||
array('string-with-2-2 numbers', 'stringWith22Numbers'),
|
||||
array('camel σase', 'camelΣase', 'UTF-8'),
|
||||
array('Σamel case', 'σamelCase', 'UTF-8'),
|
||||
array('Στανιλ case', 'στανιλCase', 'UTF-8'),
|
||||
array('σamel Case', 'σamelCase', 'UTF-8')
|
||||
);
|
||||
|
||||
@@ -104,13 +104,90 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
||||
array('string_with1number', 'StringWith1Number'),
|
||||
array('string-with-2-2 numbers', 'StringWith22Numbers'),
|
||||
array('camel σase', 'CamelΣase', 'UTF-8'),
|
||||
array('σamel case', 'ΣamelCase', 'UTF-8'),
|
||||
array('στανιλ case', 'ΣτανιλCase', 'UTF-8'),
|
||||
array('Σamel Case', 'ΣamelCase', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
*/
|
||||
public function testDasherize($string, $expected, $encoding = null) {
|
||||
$result = S::dasherize($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForDasherize() {
|
||||
$testData = array(
|
||||
array('testCase', 'test-case'),
|
||||
array('Test-Case', 'test-case'),
|
||||
array('test case', 'test-case'),
|
||||
array('-test -case', '-test-case'),
|
||||
array('test - case', 'test-case'),
|
||||
array('test_case', 'test-case'),
|
||||
array('test c test', 'test-c-test'),
|
||||
array('TestDCase', 'test-d-case'),
|
||||
array('TestCCTest', 'test-c-c-test'),
|
||||
array('string_with1number', 'string-with1number'),
|
||||
array('String-with_2_2 numbers', 'string-with-2-2-numbers'),
|
||||
array('dash Σase', 'dash-σase', 'UTF-8'),
|
||||
array('Στανιλ case', 'στανιλ-case', 'UTF-8'),
|
||||
array('Σash Case', 'σash-case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
*/
|
||||
public function testUnderscored($string, $expected, $encoding = null) {
|
||||
$result = S::underscored($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUnderscored() {
|
||||
$testData = array(
|
||||
array('testCase', 'test_case'),
|
||||
array('Test-Case', 'test_case'),
|
||||
array('test case', 'test_case'),
|
||||
array('test -case', 'test_case'),
|
||||
array('-test - case', '_test_case'),
|
||||
array('test_case', 'test_case'),
|
||||
array(' test c test', 'test_c_test'),
|
||||
array('TestUCase', 'test_u_case'),
|
||||
array('TestCCTest', 'test_c_c_test'),
|
||||
array('string_with1number', 'string_with1number'),
|
||||
array('String-with_2_2 numbers', 'string_with_2_2_numbers'),
|
||||
array('dash Σase', 'dash_σase', 'UTF-8'),
|
||||
array('Στανιλ case', 'στανιλ_case', 'UTF-8'),
|
||||
array('Σash Case', 'σash_case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
*/
|
||||
public function testSwapCase($string, $expected, $encoding = null) {
|
||||
$result = S::swapCase($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForSwapCase() {
|
||||
$testData = array(
|
||||
array('testCase', 'TESTcASE'),
|
||||
array('Test-Case', 'tEST-cASE'),
|
||||
array(' - Σash Case', ' - σASH cASE', 'UTF-8'),
|
||||
array('Ντανιλ', 'νΤΑΝΙΛ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
Reference in New Issue
Block a user