mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 08:14:06 +02:00
Remove __callStatic, stop throwing exception if argument 0 isn't a string (no scalar type hinting, so we'll allow type juggling)
This commit is contained in:
32
README.md
32
README.md
@@ -1,6 +1,6 @@
|
|||||||
# Stringy
|
# Stringy
|
||||||
|
|
||||||
A PHP library with a variety of multibyte string manipulation functions. Inspired by underscore.string.js.
|
A PHP library with a variety of string manipulation functions with multibyte support. Inspired by underscore.string.js.
|
||||||
|
|
||||||
* [Requiring/Loading](#requiringloading)
|
* [Requiring/Loading](#requiringloading)
|
||||||
* [Methods](#methods)
|
* [Methods](#methods)
|
||||||
@@ -47,13 +47,11 @@ use Stringy\Stringy as S;
|
|||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
|
|
||||||
*Note: All methods will throw a InvalidArgumentException if $string is not of type
|
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*
|
||||||
string. Furthermore, if if $encoding is not given, it defaults to
|
|
||||||
mb_internal_encoding().*
|
|
||||||
|
|
||||||
##### upperCaseFirst
|
##### upperCaseFirst
|
||||||
|
|
||||||
S::upperCaseFirst($string [, $encoding])
|
S::upperCaseFirst(string $str [, string $encoding])
|
||||||
|
|
||||||
Converts the first character of the supplied string to upper case, with
|
Converts the first character of the supplied string to upper case, with
|
||||||
support for multibyte strings.
|
support for multibyte strings.
|
||||||
@@ -64,7 +62,7 @@ S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
|||||||
|
|
||||||
##### lowerCaseFirst
|
##### lowerCaseFirst
|
||||||
|
|
||||||
S::lowerCaseFirst($string [, $encoding])
|
S::lowerCaseFirst(string $str [, string $encoding])
|
||||||
|
|
||||||
Converts the first character of the supplied string to lower case, with
|
Converts the first character of the supplied string to lower case, with
|
||||||
support for multibyte strings.
|
support for multibyte strings.
|
||||||
@@ -75,7 +73,7 @@ S::lowerCaseFirst('Σ test', 'UTF-8'); // 'σ test'
|
|||||||
|
|
||||||
##### camelize
|
##### camelize
|
||||||
|
|
||||||
S::camelize($string [, $encoding])
|
S::camelize(string $str [, string $encoding])
|
||||||
|
|
||||||
Returns a camelCase version of a supplied string, with multibyte support.
|
Returns a camelCase version of a supplied string, with multibyte support.
|
||||||
Trims surrounding spaces, capitalizes letters following digits, spaces,
|
Trims surrounding spaces, capitalizes letters following digits, spaces,
|
||||||
@@ -87,7 +85,7 @@ S::camelize('Camel-Case'); // 'camelCase'
|
|||||||
|
|
||||||
##### upperCamelize
|
##### upperCamelize
|
||||||
|
|
||||||
S::upperCamelize($string [, $encoding])
|
S::upperCamelize(string $str [, string $encoding])
|
||||||
|
|
||||||
Returns an UpperCamelCase version of a supplied string, with multibyte
|
Returns an UpperCamelCase version of a supplied string, with multibyte
|
||||||
support. Trims surrounding spaces, capitalizes letters following digits,
|
support. Trims surrounding spaces, capitalizes letters following digits,
|
||||||
@@ -99,7 +97,7 @@ S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase'
|
|||||||
|
|
||||||
##### dasherize
|
##### dasherize
|
||||||
|
|
||||||
S::dasherize($string [, $encoding])
|
S::dasherize(string $str [, string $encoding])
|
||||||
|
|
||||||
Returns a lowercase and trimmed string seperated by dashes, with
|
Returns a lowercase and trimmed string seperated by dashes, with
|
||||||
multibyte support. Dashes are inserted before uppercase characters
|
multibyte support. Dashes are inserted before uppercase characters
|
||||||
@@ -112,7 +110,7 @@ S::dasherize('TestDCase'); // 'test-d-case'
|
|||||||
|
|
||||||
##### underscored
|
##### underscored
|
||||||
|
|
||||||
S::underscored($string [, $encoding])
|
S::underscored(string $str [, string $encoding])
|
||||||
|
|
||||||
Returns a lowercase and trimmed string seperated by underscores, with
|
Returns a lowercase and trimmed string seperated by underscores, with
|
||||||
multibyte support. Underscores are inserted before uppercase characters
|
multibyte support. Underscores are inserted before uppercase characters
|
||||||
@@ -125,7 +123,7 @@ S::underscored('TestUCase'); // 'test_u_case'
|
|||||||
|
|
||||||
##### swapCase
|
##### swapCase
|
||||||
|
|
||||||
S::swapCase($string [, $encoding])
|
S::swapCase(string $str [, string $encoding])
|
||||||
|
|
||||||
Returns a case swapped version of a string.
|
Returns a case swapped version of a string.
|
||||||
|
|
||||||
@@ -135,7 +133,7 @@ S::swapCase('Ντανιλ', 'UTF-8'); // 'νΤΑΝΙΛ'
|
|||||||
|
|
||||||
##### titleize
|
##### titleize
|
||||||
|
|
||||||
S::titleize($string [, $encoding [, $ignore]])
|
S::titleize(string $str [, array $ignore [, string $encoding]])
|
||||||
|
|
||||||
Capitalizes the first letter of each word in a string, after trimming.
|
Capitalizes the first letter of each word in a string, after trimming.
|
||||||
Ignores the case of other letters, allowing for the use of acronyms.
|
Ignores the case of other letters, allowing for the use of acronyms.
|
||||||
@@ -144,13 +142,13 @@ capitalized.
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||||
S::titleize('i like to watch DVDs at home', 'UTF-8', $ignore);
|
S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
|
||||||
// 'I Like to Watch DVDs at Home'
|
// 'I Like to Watch DVDs at Home'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### humanize
|
##### humanize
|
||||||
|
|
||||||
S::humanize($string [, $encoding])
|
S::humanize(string $str [, string $encoding])
|
||||||
|
|
||||||
Capitalizes the first word of a string, replaces underscores with spaces,
|
Capitalizes the first word of a string, replaces underscores with spaces,
|
||||||
and strips '_id'.
|
and strips '_id'.
|
||||||
@@ -161,7 +159,7 @@ S::humanize('author_id'); // 'Author'
|
|||||||
|
|
||||||
##### tidy
|
##### tidy
|
||||||
|
|
||||||
S::tidy($string)
|
S::tidy(string $str)
|
||||||
|
|
||||||
Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||||
(and commonly used in Word documents) with their ASCII equivalents.
|
(and commonly used in Word documents) with their ASCII equivalents.
|
||||||
@@ -172,7 +170,7 @@ S::tidy('“I see…”'); // '"I see..."'
|
|||||||
|
|
||||||
##### clean
|
##### clean
|
||||||
|
|
||||||
S::clean($string)
|
S::clean(string $str)
|
||||||
|
|
||||||
Trims the string and replaces consecutive whitespace characters with a
|
Trims the string and replaces consecutive whitespace characters with a
|
||||||
single space.
|
single space.
|
||||||
@@ -183,7 +181,7 @@ S::clean(' Ο συγγραφέας '); // 'Ο συγγραφέας'
|
|||||||
|
|
||||||
##### standardize
|
##### standardize
|
||||||
|
|
||||||
S::standardize($string)
|
S::standardize(string $str)
|
||||||
|
|
||||||
Converts some non-ASCII characters to their closest ASCII counterparts.
|
Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||||
|
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "danielstjules/stringy",
|
"name": "danielstjules/stringy",
|
||||||
"description": "A PHP library with a variety of multibyte string manipulation functions.",
|
"description": "A PHP library with a variety of string manipulation functions with multibyte support.",
|
||||||
"keywords": ["multibyte", "string", "manipulation", "utf-8", "helpers", "utils"],
|
"keywords": [
|
||||||
|
"multibyte", "string", "manipulation", "utility", "methods", "utf-8",
|
||||||
|
"helpers", "utils"
|
||||||
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
@@ -4,50 +4,18 @@ namespace Stringy;
|
|||||||
|
|
||||||
class Stringy {
|
class Stringy {
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks that the first argument ($ars[0]) supplied to the private method
|
|
||||||
* is a string, and throws an exception if not. And if not provided, sets
|
|
||||||
* the second argument ($ars[1]) to mb_internal_encoding(). It then calls
|
|
||||||
* the static method.
|
|
||||||
*
|
|
||||||
* @param string $method Private static method being called
|
|
||||||
* @param array $args Array of arguments supplied to the method call
|
|
||||||
* @return string String returned by the private method called
|
|
||||||
* @throws BadMethodCallException If $method doesn't exist
|
|
||||||
* @throws InvalidArgumentException If $args[0] is not of type string
|
|
||||||
*/
|
|
||||||
public static function __callStatic($method, $args) {
|
|
||||||
if (!method_exists(__CLASS__, $method))
|
|
||||||
throw new \BadMethodCallException("Method doesn't exist");
|
|
||||||
|
|
||||||
if (!is_string($args[0])) {
|
|
||||||
// Scalar type hinting isn't allowed, so have to throw exceptions
|
|
||||||
$message = sprintf('Argument of type string expected, instead ' .
|
|
||||||
'received an argument of type %s', gettype($args[0]));
|
|
||||||
|
|
||||||
throw new \InvalidArgumentException($message, $args[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the character encoding ($args[1]/$encoding) if not provided
|
|
||||||
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(array(__CLASS__, $method), $args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the first character of the supplied string to upper case.
|
* Converts the first character of the supplied string to upper case.
|
||||||
*
|
*
|
||||||
* @param string $string String to modify
|
* @param string $str String to modify
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String with the first character being upper case
|
* @return string String with the first character being upper case
|
||||||
*/
|
*/
|
||||||
private static function upperCaseFirst($string, $encoding) {
|
public static function upperCaseFirst($str, $encoding = null) {
|
||||||
$first = mb_substr($string, 0, 1, $encoding);
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
$rest = mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
|
|
||||||
|
$first = mb_substr($str, 0, 1, $encoding);
|
||||||
|
$rest = mb_substr($str, 1, mb_strlen($str, $encoding) - 1, $encoding);
|
||||||
|
|
||||||
return mb_strtoupper($first, $encoding) . $rest;
|
return mb_strtoupper($first, $encoding) . $rest;
|
||||||
}
|
}
|
||||||
@@ -55,13 +23,15 @@ class Stringy {
|
|||||||
/**
|
/**
|
||||||
* Converts the first character of the supplied string to lower case.
|
* Converts the first character of the supplied string to lower case.
|
||||||
*
|
*
|
||||||
* @param string $string String to modify
|
* @param string $str String to modify
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String with the first character being lower case
|
* @return string String with the first character being lower case
|
||||||
*/
|
*/
|
||||||
private static function lowerCaseFirst($string, $encoding) {
|
public static function lowerCaseFirst($str, $encoding = null) {
|
||||||
$first = mb_substr($string, 0, 1, $encoding);
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
$rest = mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
|
|
||||||
|
$first = mb_substr($str, 0, 1, $encoding);
|
||||||
|
$rest = mb_substr($str, 1, mb_strlen($str, $encoding) - 1, $encoding);
|
||||||
|
|
||||||
return mb_strtolower($first, $encoding) . $rest;
|
return mb_strtolower($first, $encoding) . $rest;
|
||||||
}
|
}
|
||||||
@@ -71,17 +41,19 @@ class Stringy {
|
|||||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||||
* underscores, and removes spaces, dashes, underscores.
|
* underscores, and removes spaces, dashes, underscores.
|
||||||
*
|
*
|
||||||
* @param string $string String to convert to camelCase
|
* @param string $str String to convert to camelCase
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String in camelCase
|
* @return string String in camelCase
|
||||||
*/
|
*/
|
||||||
private static function camelize($string, $encoding) {
|
public static function camelize($str, $encoding = null) {
|
||||||
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
|
||||||
$camelCase = preg_replace_callback(
|
$camelCase = preg_replace_callback(
|
||||||
'/[-_\s]+(.)?/u',
|
'/[-_\s]+(.)?/u',
|
||||||
function ($matches) use (&$encoding) {
|
function ($matches) use (&$encoding) {
|
||||||
return $matches[1] ? mb_strtoupper($matches[1], $encoding) : "";
|
return $matches[1] ? mb_strtoupper($matches[1], $encoding) : "";
|
||||||
},
|
},
|
||||||
self::lowerCaseFirst(trim($string), $encoding)
|
self::lowerCaseFirst(trim($str), $encoding)
|
||||||
);
|
);
|
||||||
|
|
||||||
$camelCase = preg_replace_callback(
|
$camelCase = preg_replace_callback(
|
||||||
@@ -100,12 +72,13 @@ class Stringy {
|
|||||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||||
* underscores, and removes spaces, dashes, underscores.
|
* underscores, and removes spaces, dashes, underscores.
|
||||||
*
|
*
|
||||||
* @param string $string String to convert to UpperCamelCase
|
* @param string $str String to convert to UpperCamelCase
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String in UpperCamelCase
|
* @return string String in UpperCamelCase
|
||||||
*/
|
*/
|
||||||
private static function upperCamelize($string, $encoding) {
|
public static function upperCamelize($str, $encoding = null) {
|
||||||
$camelCase = self::camelize($string, $encoding);
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
$camelCase = self::camelize($str, $encoding);
|
||||||
|
|
||||||
return self::upperCaseFirst($camelCase, $encoding);
|
return self::upperCaseFirst($camelCase, $encoding);
|
||||||
}
|
}
|
||||||
@@ -115,12 +88,15 @@ class Stringy {
|
|||||||
* inserted before uppercase characters (with the exception of the first
|
* inserted before uppercase characters (with the exception of the first
|
||||||
* character of the string), and in place of spaces as well as underscores.
|
* character of the string), and in place of spaces as well as underscores.
|
||||||
*
|
*
|
||||||
* @param string $string String to convert
|
* @param string $str String to convert
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string Dasherized string
|
* @return string Dasherized string
|
||||||
*/
|
*/
|
||||||
private static function dasherize($string, $encoding) {
|
public static function dasherize($str, $encoding = null) {
|
||||||
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', trim($string));
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
mb_regex_encoding($encoding);
|
||||||
|
|
||||||
|
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', trim($str));
|
||||||
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
|
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
|
||||||
|
|
||||||
return mb_strtolower($dasherized, $encoding);
|
return mb_strtolower($dasherized, $encoding);
|
||||||
@@ -132,12 +108,15 @@ class Stringy {
|
|||||||
* of the first character of the string), and in place of spaces as well as
|
* of the first character of the string), and in place of spaces as well as
|
||||||
* dashes.
|
* dashes.
|
||||||
*
|
*
|
||||||
* @param string $string String to convert
|
* @param string $str String to convert
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string Underscored string
|
* @return string Underscored string
|
||||||
*/
|
*/
|
||||||
private static function underscored($string, $encoding) {
|
public static function underscored($str, $encoding = null) {
|
||||||
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', trim($string));
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
mb_regex_encoding($encoding);
|
||||||
|
|
||||||
|
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', trim($str));
|
||||||
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
|
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
|
||||||
|
|
||||||
return mb_strtolower($underscored, $encoding);
|
return mb_strtolower($underscored, $encoding);
|
||||||
@@ -146,11 +125,13 @@ class Stringy {
|
|||||||
/**
|
/**
|
||||||
* Returns a case swapped version of a string.
|
* Returns a case swapped version of a string.
|
||||||
*
|
*
|
||||||
* @param string $string String to swap case
|
* @param string $str String to swap case
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String with each character's case swapped
|
* @return string String with each character's case swapped
|
||||||
*/
|
*/
|
||||||
private static function swapCase($string, $encoding) {
|
public static function swapCase($str, $encoding = null) {
|
||||||
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
|
||||||
$swapped = preg_replace_callback(
|
$swapped = preg_replace_callback(
|
||||||
'/[\S]/u',
|
'/[\S]/u',
|
||||||
function ($match) use (&$encoding) {
|
function ($match) use (&$encoding) {
|
||||||
@@ -159,7 +140,7 @@ class Stringy {
|
|||||||
else
|
else
|
||||||
return mb_strtoupper($match[0], $encoding);
|
return mb_strtoupper($match[0], $encoding);
|
||||||
},
|
},
|
||||||
$string
|
$str
|
||||||
);
|
);
|
||||||
|
|
||||||
return $swapped;
|
return $swapped;
|
||||||
@@ -171,12 +152,14 @@ class Stringy {
|
|||||||
* Also accepts an array, $ignore, allowing you to list words not to be
|
* Also accepts an array, $ignore, allowing you to list words not to be
|
||||||
* capitalized.
|
* capitalized.
|
||||||
*
|
*
|
||||||
* @param string $string String to titleize
|
* @param string $str String to titleize
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @param array $ignore An array of words not to capitalize
|
* @param array $ignore An array of words not to capitalize
|
||||||
* @return string Titleized string
|
* @return string Titleized string
|
||||||
*/
|
*/
|
||||||
private static function titleize($string, $encoding, $ignore = null) {
|
public static function titleize($str, $ignore = null, $encoding = null) {
|
||||||
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
|
||||||
$titleized = preg_replace_callback(
|
$titleized = preg_replace_callback(
|
||||||
'/([\S]+)/u',
|
'/([\S]+)/u',
|
||||||
function ($match) use (&$encoding, &$ignore) {
|
function ($match) use (&$encoding, &$ignore) {
|
||||||
@@ -184,7 +167,7 @@ class Stringy {
|
|||||||
return $match[0];
|
return $match[0];
|
||||||
return Stringy::upperCaseFirst($match[0], $encoding);
|
return Stringy::upperCaseFirst($match[0], $encoding);
|
||||||
},
|
},
|
||||||
trim($string)
|
trim($str)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $titleized;
|
return $titleized;
|
||||||
@@ -194,13 +177,13 @@ class Stringy {
|
|||||||
* Capitalizes the first word of a string, replaces underscores with spaces,
|
* Capitalizes the first word of a string, replaces underscores with spaces,
|
||||||
* and strips '_id'.
|
* and strips '_id'.
|
||||||
*
|
*
|
||||||
* @param string $string String to humanize
|
* @param string $str String to humanize
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string A humanized string
|
* @return string A humanized string
|
||||||
*/
|
*/
|
||||||
private static function humanize($string, $encoding) {
|
public static function humanize($str, $encoding = null) {
|
||||||
$humanized = mb_ereg_replace('_id', '', $string);
|
$humanized = str_replace('_id', '', $str);
|
||||||
$humanized = mb_ereg_replace('_', ' ', $humanized);
|
$humanized = str_replace('_', ' ', $humanized);
|
||||||
|
|
||||||
return self::upperCaseFirst(trim($humanized), $encoding);
|
return self::upperCaseFirst(trim($humanized), $encoding);
|
||||||
}
|
}
|
||||||
@@ -209,12 +192,12 @@ class Stringy {
|
|||||||
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||||
* (and commonly used in Word documents) with their ASCII equivalents.
|
* (and commonly used in Word documents) with their ASCII equivalents.
|
||||||
*
|
*
|
||||||
* @param string $string String to remove special chars
|
* @param string $str String to remove special chars
|
||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string String with those characters removed
|
* @return string String with those characters removed
|
||||||
*/
|
*/
|
||||||
private static function tidy($string) {
|
public static function tidy($str) {
|
||||||
$tidied = preg_replace('/\x{2026}/u', '...', $string);
|
$tidied = preg_replace('/\x{2026}/u', '...', $str);
|
||||||
$tidied = preg_replace('/[\x{201C}\x{201D}]/u', '"', $tidied);
|
$tidied = preg_replace('/[\x{201C}\x{201D}]/u', '"', $tidied);
|
||||||
$tidied = preg_replace('/[\x{2018}\x{2019}]/u', "'", $tidied);
|
$tidied = preg_replace('/[\x{2018}\x{2019}]/u', "'", $tidied);
|
||||||
$tidied = preg_replace('/[\x{2013}\x{2014}]/u', '-', $tidied);
|
$tidied = preg_replace('/[\x{2013}\x{2014}]/u', '-', $tidied);
|
||||||
@@ -226,20 +209,20 @@ class Stringy {
|
|||||||
* Trims the string and replaces consecutive whitespace characters with a
|
* Trims the string and replaces consecutive whitespace characters with a
|
||||||
* single space.
|
* single space.
|
||||||
*
|
*
|
||||||
* @param string $string The string to cleanup whitespace
|
* @param string $str The string to cleanup whitespace
|
||||||
* @return string The trimmed string with condensed whitespace
|
* @return string The trimmed string with condensed whitespace
|
||||||
*/
|
*/
|
||||||
private static function clean($string) {
|
public static function clean($str) {
|
||||||
return preg_replace('/\s+/u', ' ', trim($string));
|
return preg_replace('/\s+/u', ' ', trim($str));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts some non-ASCII characters to their closest ASCII counterparts.
|
* Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||||
*
|
*
|
||||||
* @param string $string A string with non-ASCII characters
|
* @param string $str A string with non-ASCII characters
|
||||||
* @return string The string after the replacements
|
* @return string The string after the replacements
|
||||||
*/
|
*/
|
||||||
private static function standardize($string) {
|
public static function standardize($str) {
|
||||||
$charsArray = array(
|
$charsArray = array(
|
||||||
'a' => array('à', 'á', 'â', 'ã', 'ă', 'ä', 'å', 'ą'),
|
'a' => array('à', 'á', 'â', 'ã', 'ă', 'ä', 'å', 'ą'),
|
||||||
'c' => array('ć', 'č', 'ç'),
|
'c' => array('ć', 'č', 'ç'),
|
||||||
@@ -274,10 +257,10 @@ class Stringy {
|
|||||||
);
|
);
|
||||||
|
|
||||||
foreach ($charsArray as $key => $value) {
|
foreach ($charsArray as $key => $value) {
|
||||||
$string = str_replace($value, $key, $string);
|
$str = str_replace($value, $key, $str);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,20 +7,6 @@ use Stringy\Stringy as S;
|
|||||||
|
|
||||||
class StringyTestCase extends PHPUnit_Framework_TestCase {
|
class StringyTestCase extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException BadMethodCallException
|
|
||||||
*/
|
|
||||||
public function testExceptionOnUndefinedMethod() {
|
|
||||||
S::doesntExist('test', 'UTF-8');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testExceptionWithInvalidArgument() {
|
|
||||||
S::camelize(1, 'UTF-8');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider stringsForUpperCaseFirst
|
* @dataProvider stringsForUpperCaseFirst
|
||||||
*/
|
*/
|
||||||
@@ -199,8 +185,8 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @dataProvider stringsForTitleize
|
* @dataProvider stringsForTitleize
|
||||||
*/
|
*/
|
||||||
public function testTitleize($string, $expected, $encoding = null, $ignore = null) {
|
public function testTitleize($string, $expected, $ignore = null, $encoding = null) {
|
||||||
$result = S::titleize($string, $encoding, $ignore);
|
$result = S::titleize($string, $ignore, $encoding);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,10 +195,10 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
$testData = array(
|
$testData = array(
|
||||||
array('testing the method', 'Testing The Method'),
|
array('testing the method', 'Testing The Method'),
|
||||||
array('testing the method', 'Testing the Method', 'UTF-8', $ignore),
|
array('testing the method', 'Testing the Method', $ignore, 'UTF-8'),
|
||||||
array('i like to watch DVDs at home', 'I Like to Watch DVDs at Home',
|
array('i like to watch DVDs at home', 'I Like to Watch DVDs at Home',
|
||||||
'UTF-8', $ignore),
|
$ignore, 'UTF-8'),
|
||||||
array(' Θα ήθελα να φύγει ', 'Θα Ήθελα Να Φύγει', 'UTF-8')
|
array(' Θα ήθελα να φύγει ', 'Θα Ήθελα Να Φύγει', null, 'UTF-8')
|
||||||
);
|
);
|
||||||
|
|
||||||
return $testData;
|
return $testData;
|
||||||
|
Reference in New Issue
Block a user