1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-01 17:12:42 +02:00

20 Commits
1.2.1 ... 1.3.0

Author SHA1 Message Date
Daniel St. Jules
9f930262af Update changelog for 1.3.0 2013-12-16 21:04:30 -05:00
Daniel St. Jules
c230885a13 Require objects to have a __toString method, update readme 2013-12-15 01:08:23 -05:00
Daniel St. Jules
1f25d0c793 Merge Pull Request #18 2013-12-15 00:15:34 -05:00
Daniel St. Jules
0e70d72352 Merge pull request #16 from ivank/patch-2
Support Bulgarian in toAscii
2013-12-13 06:32:34 -08:00
Ivan Kerin
d54628e6ae Support Bulgarian in toAscii
The “proper” bulgarian translitaration should encode ь to y [1],
However, since its not really that important to understand the actual
word, very rearly used and breaks compatibility with Russian, it might
be best to just leave it be, since Russian is a much larger torget.

[1] http://en.wikipedia.org/wiki/Romanization_of_Bulgarian
2013-12-13 11:05:23 +00:00
gotterdemarung
7335ee5c88 fixed T_OBJECT_OPERATOR issue in PHP 5.3 2013-12-10 23:10:44 +02:00
gotterdemarung
5e01d895db titleize refactoring to avoid visibility problems in PHP 5.3 2013-12-10 22:59:10 +02:00
gotterdemarung
3458fd79fd Stringy->$str became private to ensure its sting content and guarantee zero-exception __toString() execution 2013-12-10 22:34:19 +02:00
gotterdemarung
6e5e5e055f primitives check in constructor & toString 2013-12-10 22:19:42 +02:00
gotterdemarung
88d5973701 Implicit cast argument to string in constructor 2013-12-10 22:15:58 +02:00
gotterdemarung
75404a0338 Stringy must correctly handle non-string arguments and implicitly cast to string to avoid problems in __toString() method 2013-12-10 22:15:28 +02:00
Daniel St. Jules
bc96cdad0c Merge pull request #17 from floriansemm/master
fix typo in pad function call
2013-12-09 20:55:26 -08:00
Florian Semm
6014ff52df fix typo in pad function call 2013-12-09 15:15:45 +01:00
Daniel St. Jules
6ba29637b2 Updated changelog for 1.2.2 2013-12-04 23:36:17 -05:00
Daniel St. Jules
2ada00ff62 Add logo 2013-12-01 15:43:03 -05:00
Daniel St. Jules
2a197244a5 Update readme and comments for new slugify param 2013-11-27 01:14:09 -05:00
Daniel St. Jules
8b1a91fe0c Merge pull request #15 from glynnforrest/slug
Adding optional $replacement parameter to slugify.
2013-11-26 22:03:02 -08:00
Glynn Forrest
906f217dc1 Adding optional $replacement parameter to slugify. 2013-11-26 14:11:41 +00:00
Daniel St. Jules
d4baab7583 Merge pull request #14 from sergebezborodov/master
Use late static bindings in Stringy::create
2013-11-05 20:00:18 -08:00
Serge Bezborodov
0c8f0e9083 changes in create function 2013-11-01 09:55:38 +02:00
7 changed files with 190 additions and 102 deletions

View File

@@ -1,3 +1,17 @@
### 1.3.0 (2013-12-16)
* Additional Bulgarian support for toAscii
* str property made private
* Constructor casts first argument to string
* Constructor throws an InvalidArgumentException when given an array
* Constructor throws an InvalidArgumentException when given an object without
a __toString method
### 1.2.2 (2013-12-04)
* Updated create function to use late static binding
* Added optional $replacement param to slugify
### 1.2.1 (2013-10-11)
* Cleaned up tests

View File

@@ -1,4 +1,4 @@
# Stringy
![Stringy](http://danielstjules.com/stringy/logo.png)
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
@@ -67,13 +67,15 @@ A PHP library with a variety of string manipulation functions with multibyte sup
## Requiring/Loading
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
If you're using Composer to manage dependencies, you can include the following
in your composer.json file:
"require": {
"danielstjules/stringy": "dev-master"
}
Then, after running `composer update` or `php composer.phar update`, you can load the class using Composer's autoloading:
Then, after running `composer update` or `php composer.phar update`, you can
load the class using Composer's autoloading:
```php
require 'vendor/autoload.php';
@@ -106,9 +108,9 @@ use Stringy\Stringy as S;
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
```
`Stringy\Stringy` contains a __toString() method, which returns the current
string when the object is used in a string context. Its $str property is also
public, and can be accessed directly if required, ie: `S::create('foo')->str // 'foo'`
`Stringy\Stringy` has a __toString() method, which returns the current string
when the object is used in a string context, ie:
`(string) S::create('foo') // 'foo'`
Using the static wrapper, an alternative is the following:
@@ -120,8 +122,10 @@ echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
## Methods
In the list below, any static method other than S::create refers to a
method in `Stringy\StaticStringy`. For all others, they're found in `Stringy\Stringy`. Furthermore, all methods that return a Stringy object or string do not modify the original.
In the list below, any static method other than S::create refers to a method in
`Stringy\StaticStringy`. For all others, they're found in `Stringy\Stringy`.
Furthermore, all methods that return a Stringy object or string do not modify
the original.
*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*
@@ -200,11 +204,13 @@ S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
#### create
S::create(string $str, [, $encoding ])
S::create(mixed $str, [, $encoding ])
Creates a Stringy object and assigns both str and encoding properties
the supplied values. If $encoding is not specified, it defaults to
mb_internal_encoding(). It then returns the initialized object.
the supplied values. $str is cast to a string prior to assignment, and if
$encoding is not specified, it defaults to mb_internal_encoding(). It
then returns the initialized object. Throws an InvalidArgumentException
if the first argument is an array or object without a __toString method.
```php
$stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
@@ -503,7 +509,7 @@ string used for padding is a space, and the default type (one of 'left',
$padType isn't one of those 3 values.
```php
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left');
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
```
@@ -639,7 +645,8 @@ $stringy->shuffle()
S::shuffle(string $str [, string $encoding ])
A multibyte str_shuffle() function. It returns a string with its characters in random order.
A multibyte str_shuffle() function. It returns a string with its characters in
random order.
```php
S::create('fòô bàř', 'UTF-8')->shuffle();
@@ -648,14 +655,15 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
#### slugify
$stringy->slugify()
$stringy->slugify([ string $replacement = '-' ])
S::slugify(string $str)
S::slugify(string $str [, string $replacement = '-' ])
Converts the string into an URL slug. This includes replacing non-ASCII
characters with their closest ASCII equivalents, removing non-alphanumeric
and non-ASCII characters, and replacing whitespace with dashes. The string
is also converted to lowercase.
and non-ASCII characters, and replacing whitespace with $replacement.
The replacement defaults to a single dash, and the string is also
converted to lowercase.
```php
S::create('Using strings like fòô bàř')->slugify();

View File

@@ -13,7 +13,7 @@ class StaticStringy
*/
public static function upperCaseFirst($str, $encoding = null)
{
return Stringy::create($str, $encoding)->upperCaseFirst()->str;
return (string) Stringy::create($str, $encoding)->upperCaseFirst();
}
/**
@@ -25,7 +25,7 @@ class StaticStringy
*/
public static function lowerCaseFirst($str, $encoding = null)
{
return Stringy::create($str, $encoding)->lowerCaseFirst()->str;
return (string) Stringy::create($str, $encoding)->lowerCaseFirst();
}
/**
@@ -39,7 +39,7 @@ class StaticStringy
*/
public static function camelize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->camelize()->str;
return (string) Stringy::create($str, $encoding)->camelize();
}
/**
@@ -53,7 +53,7 @@ class StaticStringy
*/
public static function upperCamelize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->upperCamelize()->str;
return (string) Stringy::create($str, $encoding)->upperCamelize();
}
/**
@@ -67,7 +67,7 @@ class StaticStringy
*/
public static function dasherize($str, $encoding = null)
{
return Stringy::create($str, $encoding)->dasherize()->str;
return (string) Stringy::create($str, $encoding)->dasherize();
}
/**
@@ -82,7 +82,7 @@ class StaticStringy
*/
public static function underscored($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->underscored()->str;
return (string) Stringy::create($str, $encoding)->underscored();
}
/**
@@ -94,7 +94,7 @@ class StaticStringy
*/
public static function swapCase($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->swapCase()->str;
return (string) Stringy::create($str, $encoding)->swapCase();
}
/**
@@ -110,7 +110,7 @@ class StaticStringy
*/
public static function titleize($str, $ignore = null, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->titleize($ignore)->str;
return (string) Stringy::create($str, $encoding)->titleize($ignore);
}
/**
@@ -123,7 +123,7 @@ class StaticStringy
*/
public static function humanize($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->humanize()->str;
return (string) Stringy::create($str, $encoding)->humanize();
}
/**
@@ -136,7 +136,7 @@ class StaticStringy
*/
public static function tidy($str)
{
return $result = Stringy::create($str)->tidy()->str;
return (string) Stringy::create($str)->tidy();
}
/**
@@ -150,7 +150,7 @@ class StaticStringy
*/
public static function collapseWhitespace($str, $encoding = null)
{
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
return (string) Stringy::create($str, $encoding)->collapseWhitespace();
}
/**
@@ -162,7 +162,7 @@ class StaticStringy
*/
public static function toAscii($str)
{
return $result = Stringy::create($str)->toAscii()->str;
return (string) Stringy::create($str)->toAscii();
}
/**
@@ -178,14 +178,14 @@ class StaticStringy
* @param string $padType One of 'left', 'right', 'both'
* @param string $encoding The character encoding
* @return string The padded string
* @throws InvalidArgumentException If $padType isn't one of 'right',
* @throws \InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both'
*/
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
$encoding = null)
{
return $result = Stringy::create($str, $encoding)
->pad($length, $padStr, $padType)->str;
return (string) Stringy::create($str, $encoding)
->pad($length, $padStr, $padType);
}
/**
@@ -200,7 +200,7 @@ class StaticStringy
*/
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padLeft($length, $padStr)->str;
return (string) Stringy::create($str, $encoding)->padLeft($length, $padStr);
}
/**
@@ -215,7 +215,7 @@ class StaticStringy
*/
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padRight($length, $padStr)->str;
return (string) Stringy::create($str, $encoding)->padRight($length, $padStr);
}
/**
@@ -230,7 +230,7 @@ class StaticStringy
*/
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
{
return Stringy::create($str, $encoding)->padBoth($length, $padStr)->str;
return (string) Stringy::create($str, $encoding)->padBoth($length, $padStr);
}
/**
@@ -279,7 +279,7 @@ class StaticStringy
*/
public static function toSpaces($str, $tabLength = 4)
{
return Stringy::create($str)->toSpaces($tabLength)->str;
return (string) Stringy::create($str)->toSpaces($tabLength);
}
/**
@@ -293,7 +293,7 @@ class StaticStringy
*/
public static function toTabs($str, $tabLength = 4)
{
return Stringy::create($str)->toTabs($tabLength)->str;
return (string) Stringy::create($str)->toTabs($tabLength);
}
/**
@@ -306,7 +306,7 @@ class StaticStringy
*/
public static function toLowerCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->toLowerCase()->str;
return (string) Stringy::create($str, $encoding)->toLowerCase();
}
/**
@@ -319,21 +319,23 @@ class StaticStringy
*/
public static function toUpperCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->toUpperCase()->str;
return (string) Stringy::create($str, $encoding)->toUpperCase();
}
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string
* is also converted to lowercase.
* and non-ASCII characters, and replacing whitespace with $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $str Text to transform into an URL slug
* @param string $str Text to transform into an URL slug
* @param string $replacement The string used to replace whitespace
* @return string The corresponding URL slug
*/
public static function slugify($str)
public static function slugify($str, $replacement = '-')
{
return Stringy::create($str)->slugify()->str;
return (string) Stringy::create($str)->slugify($replacement);
}
/**
@@ -362,7 +364,7 @@ class StaticStringy
*/
public static function surround($str, $substring)
{
return Stringy::create($str)->surround($substring)->str;
return (string) Stringy::create($str)->surround($substring);
}
/**
@@ -376,7 +378,7 @@ class StaticStringy
*/
public static function insert($str, $substring, $index, $encoding = null)
{
return Stringy::create($str, $encoding)->insert($substring, $index)->str;
return (string) Stringy::create($str, $encoding)->insert($substring, $index);
}
/**
@@ -393,8 +395,8 @@ class StaticStringy
public static function truncate($str, $length, $substring = '',
$encoding = null)
{
return Stringy::create($str, $encoding)
->truncate($length, $substring)->str;
return (string) Stringy::create($str, $encoding)
->truncate($length, $substring);
}
/**
@@ -412,8 +414,8 @@ class StaticStringy
public static function safeTruncate($str, $length, $substring = '',
$encoding = null)
{
return Stringy::create($str, $encoding)
->safeTruncate($length, $substring)->str;
return (string) Stringy::create($str, $encoding)
->safeTruncate($length, $substring);
}
/**
@@ -425,7 +427,7 @@ class StaticStringy
*/
public static function reverse($str, $encoding = null)
{
return Stringy::create($str, $encoding)->reverse()->str;
return (string) Stringy::create($str, $encoding)->reverse();
}
/**
@@ -438,7 +440,7 @@ class StaticStringy
*/
public static function shuffle($str, $encoding = null)
{
return Stringy::create($str, $encoding)->shuffle()->str;
return (string) Stringy::create($str, $encoding)->shuffle();
}
/**
@@ -462,8 +464,8 @@ class StaticStringy
*/
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonPrefix($otherStr)->str;
return (string) Stringy::create($str, $encoding)
->longestCommonPrefix($otherStr);
}
/**
@@ -476,8 +478,8 @@ class StaticStringy
*/
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonSuffix($otherStr)->str;
return (string) Stringy::create($str, $encoding)
->longestCommonSuffix($otherStr);
}
/**
@@ -492,8 +494,8 @@ class StaticStringy
public static function longestCommonSubstring($str, $otherStr,
$encoding = null)
{
return Stringy::create($str, $encoding)
->longestCommonSubstring($otherStr)->str;
return (string) Stringy::create($str, $encoding)
->longestCommonSubstring($otherStr);
}
/**
@@ -521,7 +523,7 @@ class StaticStringy
*/
public static function substr($str, $start, $length = null, $encoding = null)
{
return Stringy::create($str, $encoding)->substr($start, $length)->str;
return (string) Stringy::create($str, $encoding)->substr($start, $length);
}
/**
@@ -534,7 +536,7 @@ class StaticStringy
*/
public static function at($str, $index, $encoding = null)
{
return Stringy::create($str, $encoding)->at($index)->str;
return (string) Stringy::create($str, $encoding)->at($index);
}
/**
@@ -547,7 +549,7 @@ class StaticStringy
*/
public static function first($str, $n, $encoding = null)
{
return Stringy::create($str, $encoding)->first($n)->str;
return (string) Stringy::create($str, $encoding)->first($n);
}
/**
@@ -560,7 +562,7 @@ class StaticStringy
*/
public static function last($str, $n, $encoding = null)
{
return Stringy::create($str, $encoding)->last($n)->str;
return (string) Stringy::create($str, $encoding)->last($n);
}
/**
@@ -574,7 +576,7 @@ class StaticStringy
*/
public static function ensureLeft($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->ensureLeft($substring)->str;
return (string) Stringy::create($str, $encoding)->ensureLeft($substring);
}
/**
@@ -588,7 +590,7 @@ class StaticStringy
*/
public static function ensureRight($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->ensureRight($substring)->str;
return (string) Stringy::create($str, $encoding)->ensureRight($substring);
}
/**
@@ -601,7 +603,7 @@ class StaticStringy
*/
public static function removeLeft($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeLeft($substring)->str;
return (string) Stringy::create($str, $encoding)->removeLeft($substring);
}
/**
@@ -614,7 +616,7 @@ class StaticStringy
*/
public static function removeRight($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeRight($substring)->str;
return (string) Stringy::create($str, $encoding)->removeRight($substring);
}
/**
@@ -742,7 +744,7 @@ class StaticStringy
*/
public static function replace($str, $search, $replacement, $encoding = null)
{
return Stringy::create($str, $encoding)->replace($search, $replacement)->str;
return (string) Stringy::create($str, $encoding)->replace($search, $replacement);
}
/**
@@ -761,7 +763,7 @@ class StaticStringy
public static function regexReplace($str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
return Stringy::create($str, $encoding)->regexReplace($pattern,
$replacement, $options, $encoding)->str;
return (string) Stringy::create($str, $encoding)->regexReplace($pattern,
$replacement, $options, $encoding);
}
}

View File

@@ -4,36 +4,54 @@ namespace Stringy;
class Stringy
{
public $str;
private $str;
public $encoding;
/**
* Initializes a Stringy object and assigns both str and encoding properties
* the supplied values. If $encoding is not specified, it defaults to
* mb_internal_encoding().
* the supplied values. $str is cast to a string prior to assignment, and if
* $encoding is not specified, it defaults to mb_internal_encoding(). Throws
* an InvalidArgumentException if the first argument is an array or object
* without a __toString method.
*
* @param string $str String to modify
* @param string $encoding The character encoding
* @param mixed $str Value to modify, after being cast to string
* @param string $encoding The character encoding
* @throws \InvalidArgumentException if an array or object without a
* __toString method is passed as the first argument
*/
public function __construct($str, $encoding = null)
{
$this->str = $str;
if (is_array($str)) {
throw new \InvalidArgumentException(
'Passed value cannot be an array'
);
} else if (is_object($str) && !method_exists($str, '__toString')) {
throw new \InvalidArgumentException(
'Passed object must have a __toString method'
);
}
$this->str = (string) $str;
$this->encoding = $encoding ?: mb_internal_encoding();
}
/**
* Creates a Stringy object and assigns both str and encoding properties
* the supplied values. If $encoding is not specified, it defaults to
* mb_internal_encoding(). It then returns the initialized object.
* the supplied values. $str is cast to a string prior to assignment, and if
* $encoding is not specified, it defaults to mb_internal_encoding(). It
* then returns the initialized object. Throws an InvalidArgumentException
* if the first argument is an array or object without a __toString method.
*
* @param string $str String to modify
* @param mixed $str Value to modify, after being cast to string
* @param string $encoding The character encoding
* @return Stringy A Stringy object
* @throws \InvalidArgumentException if an array or object without a
* __toString method is passed as the first argument
*/
public static function create($str, $encoding = null)
{
return new self($str, $encoding);
return new static($str, $encoding);
}
/**
@@ -203,21 +221,23 @@ class Stringy
*/
public function titleize($ignore = null)
{
$stringy = self::create($this->str, $this->encoding)->trim();
$encoding = $stringy->encoding;
$buffer = $this->trim();
$encoding = $this->encoding;
$stringy->str = preg_replace_callback(
$buffer = preg_replace_callback(
'/([\S]+)/u',
function ($match) use (&$encoding, &$ignore, &$stringy) {
if ($ignore && in_array($match[0], $ignore))
function ($match) use (&$encoding, &$ignore) {
if ($ignore && in_array($match[0], $ignore)) {
return $match[0];
$stringy->str = $match[0];
return $stringy->upperCaseFirst();
} else {
$stringy = new Stringy($match[0], $encoding);
return (string) $stringy->upperCaseFirst();
}
},
$stringy->str
$buffer
);
return $stringy;
return new Stringy($buffer, $encoding);
}
/**
@@ -287,7 +307,7 @@ class Stringy
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
'ᾶ', 'ᾷ', 'а'),
'ᾶ', 'ᾷ', 'а', 'ъ'),
'b' => array('б', 'β'),
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'),
@@ -326,7 +346,7 @@ class Stringy
'zh' => array('ж'),
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ',
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ъ'),
'B' => array('Б'),
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'),
@@ -577,18 +597,20 @@ class Stringy
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string
* is also converted to lowercase.
* and non-ASCII characters, and replacing whitespace with $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $replacement The string used to replace whitespace
* @return Stringy Object whose $str has been converted to an URL slug
*/
public function slugify()
public function slugify($replacement = '-')
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/[^a-zA-Z\d -]/u', '', $stringy->toAscii());
$stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '', $stringy->toAscii());
$stringy->str = $stringy->collapseWhitespace()->str;
$stringy->str = str_replace(' ', '-', strtolower($stringy->str));
$stringy->str = str_replace(' ', $replacement, strtolower($stringy->str));
return $stringy;
}

View File

@@ -330,7 +330,10 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
array('numbers-1234', 'numbers 1234'),
array('perevirka-ryadka', 'перевірка рядка'),
array('bukvar-s-bukvoy-y', 'букварь с буквой ы')
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
array('barzi-i-yarostni', 'бързи и яростни'),
array('foo:bar:baz', 'Foo bar baz', ':'),
array('a_string_with_underscores', 'A_string with_underscores', '_')
);
}

View File

@@ -245,9 +245,9 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str)
public function testSlugify($expected, $str, $replacement = '-')
{
$result = S::slugify($str);
$result = S::slugify($str, $replacement);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}

View File

@@ -11,15 +11,54 @@ class StringyTestCase extends CommonTest
{
$stringy = new S('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', $stringy->str);
$this->assertEquals('foo bar', (string) $stringy);
$this->assertEquals('UTF-8', $stringy->encoding);
}
/**
* @expectedException InvalidArgumentException
*/
public function testConstructWithArray()
{
(string) new S(array());
$this->fail('Expecting exception when the constructor is passed an array');
}
/**
* @expectedException InvalidArgumentException
*/
public function testMissingToString()
{
(string) new S(new stdClass());
$this->fail('Expecting exception when the constructor is passed an ' .
'object without a __toString method');
}
/**
* @dataProvider toStringProvider()
*/
public function testToString($expected, $str)
{
$this->assertEquals($expected, (string) new S($str));
}
public function toStringProvider()
{
return array(
array('', null),
array('', false),
array('1', true),
array('-9', -9),
array('1.18', 1.18),
array(' string ', ' string ')
);
}
public function testCreate()
{
$stringy = S::create('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', $stringy->str);
$this->assertEquals('foo bar', (string) $stringy);
$this->assertEquals('UTF-8', $stringy->encoding);
}
@@ -312,10 +351,10 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str)
public function testSlugify($expected, $str, $replacement = '-')
{
$stringy = S::create($str);
$result = $stringy->slugify();
$result = $stringy->slugify($replacement);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);