1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-05 18:52:59 +02:00

27 Commits
2.x ... 3.1.0

Author SHA1 Message Date
Daniel St. Jules
df24ab62d2 3.1.0 2017-06-11 21:10:27 -04:00
Daniel St. Jules
f9c2e0ae36 Remove ь/Ь from default mappping, add to bg-specific 2017-06-11 21:06:36 -04:00
Daniel St. Jules
0c0b352009 Update mapping of ЬЪ/ьъ from latin B/b to Y/y 2017-06-11 20:55:10 -04:00
Daniel St. Jules
f0a5c0ec80 Update table of contents 2017-06-11 20:43:36 -04:00
Daniel St. Jules
8ef4bddd15 Add support to slugify 2017-06-11 20:41:02 -04:00
Daniel St. Jules
d9cee68330 Merge pull request #164 from iipavlov/hotfix/cyrillic_ascii_transliteration 2017-05-10 10:41:34 -04:00
iipavlov
2838f3412a Added bg specific transliteration according BGN/PCGN (2013) 2017-05-05 18:20:56 +03:00
iipavlov
50f0cb6356 Moved the cyrillic letters ь and ъ from the latin b tto y. 2017-05-05 17:54:57 +03:00
Daniel St. Jules
d6dabeee67 Merge pull request #163 from emirb/patch-1
Add PHP 7.1 to Travis build
2017-04-22 00:37:48 -04:00
Emir Beganović
44f492c025 Add PHP 7.1 to Travis build 2017-04-14 17:46:53 +02:00
Daniel St. Jules
2288363663 3.0.1 2017-04-12 11:20:39 -04:00
Daniel St. Jules
f22117862b Use normal replacement for @ in slugify 2017-04-12 11:19:35 -04:00
Daniel St. Jules
eb13cc35d6 Don't replace @ in toAscii 2017-04-12 11:09:07 -04:00
Daniel St. Jules
9b6bddcb20 Fix list items 2017-04-09 21:57:32 -04:00
Daniel St. Jules
67f16c9423 Use HTML anchors in readme table 2017-04-09 21:56:46 -04:00
Daniel St. Jules
3708b6ba90 Update alignment in phpdocs 2017-03-13 23:10:57 -04:00
Daniel St. Jules
5c50a58bb8 Fix readme typo 2017-03-08 19:56:22 -05:00
Daniel St. Jules
8b59cd2233 3.0.0 2017-03-08 19:52:40 -05:00
Daniel St. Jules
63d81901ad Merge pull request #160 from danielstjules/3.x-dev
3.x
2017-03-08 19:44:04 -05:00
Daniel St. Jules
a2624d4969 Docs fixes 2017-03-08 19:23:05 -05:00
Daniel St. Jules
72102ae911 toAscii changes: rename locale to language, correct some transliterations 2017-03-08 19:16:09 -05:00
Daniel St. Jules
bf2a302aa1 Breaking change: Fix docs, force static methods to return strings
rather than Stringy instances
2017-03-06 15:11:14 -05:00
Alexey Shockov
5303203a72 Return real strings from StaticStringy 2017-03-06 14:00:09 -05:00
Daniel St. Jules
e01ba7f848 Use short array syntax 2017-03-06 13:43:47 -05:00
Daniel St. Jules
f9f3b7f2f2 Link to 2.x documentation 2017-03-06 13:15:11 -05:00
Daniel St. Jules
75b0cd2e0d Drop php 5.3 support 2017-03-06 13:14:15 -05:00
Daniel St. Jules
b0976c72b7 Fix #116 with breaking change: add locale parameter to toAscii
This fixes a previous regression in handling of some umlaut
characters, and commits a breaking change by adding the param
before $removeUnsupported
2017-03-06 13:01:09 -05:00
8 changed files with 1509 additions and 1401 deletions

View File

@@ -10,10 +10,6 @@ sudo: false
matrix: matrix:
fast_finish: true fast_finish: true
include: include:
- php: 5.3
env: polyfill='true'
- php: 5.3
env: polyfill='false'
- php: 5.4 - php: 5.4
env: polyfill='true' env: polyfill='true'
- php: 5.4 - php: 5.4
@@ -30,6 +26,10 @@ matrix:
env: polyfill='true' env: polyfill='true'
- php: 7.0 - php: 7.0
env: polyfill='false' env: polyfill='false'
- php: 7.1
env: polyfill='true'
- php: 7.1
env: polyfill='false'
install: travis_retry composer install --no-interaction --prefer-source install: travis_retry composer install --no-interaction --prefer-source

View File

@@ -1,3 +1,20 @@
### 3.1.0 (2017-06-11)
* Add $language support to slugify
* Add bg specific transliteration
* ЬЪ/ьъ handling is now language-specific
### 3.0.1 (2017-04-12)
* Don't replace @ in toAscii
* Use normal replacement for @ in slugify, e.g. user@home => user-home
### 3.0.0 (2017-03-08)
* Breaking change: added $language parameter to toAscii, before
$removeUnsupported
* Breaking change: dropped PHP 5.3 support
* Breaking change: any StaticStringy methods that previously returned instances
of Stringy now return strings
### 2.4.0 (2017-03-02) ### 2.4.0 (2017-03-02)
* Add startsWithAny * Add startsWithAny

228
README.md
View File

@@ -1,13 +1,16 @@
![Stringy](http://danielstjules.com/github/stringy-logo.png) ![Stringy](http://danielstjules.com/github/stringy-logo.png)
A PHP string manipulation library with multibyte support. Compatible with PHP A PHP string manipulation library with multibyte support. Compatible with PHP
5.3+, PHP 7, and HHVM. Refer to the [1.x branch](https://github.com/danielstjules/Stringy/tree/1.x) 5.4+, PHP 7+, and HHVM.
for older documentation.
``` php ``` php
s('string')->toTitleCase()->ensureRight('y') == 'Stringy' s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
``` ```
Refer to the [1.x branch](https://github.com/danielstjules/Stringy/tree/1.x) or
[2.x branch](https://github.com/danielstjules/Stringy/tree/2.x) for older
documentation.
[![Build Status](https://api.travis-ci.org/danielstjules/Stringy.svg?branch=master)](https://travis-ci.org/danielstjules/Stringy) [![Build Status](https://api.travis-ci.org/danielstjules/Stringy.svg?branch=master)](https://travis-ci.org/danielstjules/Stringy)
[![Total Downloads](https://poser.pugx.org/danielstjules/stringy/downloads)](https://packagist.org/packages/danielstjules/stringy) [![Total Downloads](https://poser.pugx.org/danielstjules/stringy/downloads)](https://packagist.org/packages/danielstjules/stringy)
[![License](https://poser.pugx.org/danielstjules/stringy/license)](https://packagist.org/packages/danielstjules/stringy) [![License](https://poser.pugx.org/danielstjules/stringy/license)](https://packagist.org/packages/danielstjules/stringy)
@@ -23,126 +26,127 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
* [Instance methods](#instance-methods) * [Instance methods](#instance-methods)
<table> <table>
<tr> <tr>
<td>[append](#appendstring-string)</td> <td><a href="#appendstring-string">append</a></td>
<td>[at](#atint-index)</td> <td><a href="#atint-index">at</a></td>
<td>[between](#betweenstring-start-string-end--int-offset)</td> <td><a href="#betweenstring-start-string-end--int-offset">between</a></td>
<td>[camelize](#camelize)</td> <td><a href="#camelize">camelize</a></td>
</tr> </tr>
<tr> <tr>
<td>[chars](#chars)</td> <td><a href="#chars">chars</a></td>
<td>[collapseWhitespace](#collapsewhitespace)</td> <td><a href="#collapsewhitespace">collapseWhitespace</a></td>
<td>[contains](#containsstring-needle--boolean-casesensitive--true-)</td> <td><a href="#containsstring-needle--boolean-casesensitive--true-">contains</a></td>
<td>[containsAll](#containsallarray-needles--boolean-casesensitive--true-)</td> <td><a href="#containsallarray-needles--boolean-casesensitive--true-">containsAll</a></td>
</tr> </tr>
<tr> <tr>
<td>[containsAny](#containsanyarray-needles--boolean-casesensitive--true-)</td> <td><a href="#containsanyarray-needles--boolean-casesensitive--true-">containsAny</a></td>
<td>[countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-)</td> <td><a href="#countsubstrstring-substring--boolean-casesensitive--true-">countSubstr</a></td>
<td>[dasherize](#dasherize)</td> <td><a href="#dasherize">dasherize</a></td>
<td>[delimit](#delimitint-delimiter)</td> <td><a href="#delimitint-delimiter">delimit</a></td>
</tr> </tr>
<tr> <tr>
<td>[endsWith](#endswithstring-substring--boolean-casesensitive--true-)</td> <td><a href="#endswithstring-substring--boolean-casesensitive--true-">endsWith</a></td>
<td>[endsWithAny](#endsWithAnystring-substrings--boolean-casesensitive--true-)</td> <td><a href="#endswithanystring-substrings--boolean-casesensitive--true-">endsWithAny</a></td>
<td>[ensureLeft](#ensureleftstring-substring)</td> <td><a href="#ensureleftstring-substring">ensureLeft</a></td>
<td>[ensureRight](#ensurerightstring-substring)</td> <td><a href="#ensurerightstring-substring">ensureRight</a></td>
</tr> </tr>
<tr> <tr>
<td>[first](#firstint-n)</td> <td><a href="#firstint-n">first</a></td>
<td>[getEncoding](#getencoding)</td> <td><a href="#getencoding">getEncoding</a></td>
<td>[hasLowerCase](#haslowercase)</td> <td><a href="#haslowercase">hasLowerCase</a></td>
<td>[hasUpperCase](#hasuppercase)</td> <td><a href="#hasuppercase">hasUpperCase</a></td>
</tr> </tr>
<tr> <tr>
<td>[htmlDecode](#htmldecode)</td> <td><a href="#htmldecode">htmlDecode</a></td>
<td>[htmlEncode](#htmlencode)</td> <td><a href="#htmlencode">htmlEncode</a></td>
<td>[humanize](#humanize)</td> <td><a href="#humanize">humanize</a></td>
<td>[indexOf](#indexofstring-needle--offset--0-)</td> <td><a href="#indexofstring-needle--offset--0-">indexOf</a></td>
</tr> </tr>
<tr> <tr>
<td>[indexOfLast](#indexoflaststring-needle--offset--0-)</td> <td><a href="#indexoflaststring-needle--offset--0-">indexOfLast</a></td>
<td>[insert](#insertint-index-string-substring)</td> <td><a href="#insertint-index-string-substring">insert</a></td>
<td>[isAlpha](#isalpha)</td> <td><a href="#isalpha">isAlpha</a></td>
<td>[isAlphanumeric](#isalphanumeric)</td> <td><a href="#isalphanumeric">isAlphanumeric</a></td>
</tr> </tr>
<tr> <tr>
<td>[isBase64](#isbase64)</td> <td><a href="#isbase64">isBase64</a></td>
<td>[isBlank](#isblank)</td> <td><a href="#isblank">isBlank</a></td>
<td>[isHexadecimal](#ishexadecimal)</td> <td><a href="#ishexadecimal">isHexadecimal</a></td>
<td>[isJson](#isjson)</td> <td><a href="#isjson">isJson</a></td>
</tr> </tr>
<tr> <tr>
<td>[isLowerCase](#islowercase)</td> <td><a href="#islowercase">isLowerCase</a></td>
<td>[isSerialized](#isserialized)</td> <td><a href="#isserialized">isSerialized</a></td>
<td>[isUpperCase](#isuppercase)</td> <td><a href="#isuppercase">isUpperCase</a></td>
<td>[last](#last)</td> <td><a href="#lastint-n">last</a></td>
</tr> </tr>
<tr> <tr>
<td>[length](#length)</td> <td><a href="#length">length</a></td>
<td>[lines](#lines)</td> <td><a href="#lines">lines</a></td>
<td>[longestCommonPrefix](#longestcommonprefixstring-otherstr)</td> <td><a href="#longestcommonprefixstring-otherstr">longestCommonPrefix</a></td>
<td>[longestCommonSuffix](#longestcommonsuffixstring-otherstr)</td> <td><a href="#longestcommonsuffixstring-otherstr">longestCommonSuffix</a></td>
</tr> </tr>
<tr> <tr>
<td>[longestCommonSubstring](#longestcommonsubstringstring-otherstr)</td> <td><a href="#longestcommonsubstringstring-otherstr">longestCommonSubstring</a></td>
<td>[lowerCaseFirst](#lowercasefirst)</td> <td><a href="#lowercasefirst">lowerCaseFirst</a></td>
<td>[pad](#padint-length--string-padstr-----string-padtype--right-)</td> <td><a href="#padint-length--string-padstr-----string-padtype--right-">pad</a></td>
<td>[padBoth](#padbothint-length--string-padstr----)</td> <td><a href="#padbothint-length--string-padstr----">padBoth</a></td>
</tr> </tr>
<tr> <tr>
<td>[padLeft](#padleftint-length--string-padstr----)</td> <td><a href="#padleftint-length--string-padstr----">padLeft</a></td>
<td>[padRight](#padrightint-length--string-padstr----)</td> <td><a href="#padrightint-length--string-padstr----">padRight</a></td>
<td>[prepend](#prependstring-string)</td> <td><a href="#prependstring-string">prepend</a></td>
<td>[regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)</td> <td><a href="#regexreplacestring-pattern-string-replacement--string-options--msr">regexReplace</a></td>
</tr> </tr>
<tr> <tr>
<td>[removeLeft](#removeleftstring-substring)</td> <td><a href="#removeleftstring-substring">removeLeft</a></td>
<td>[removeRight](#removerightstring-substring)</td> <td><a href="#removerightstring-substring">removeRight</a></td>
<td>[repeat](#repeatmultiplier)</td> <td><a href="#repeatint-multiplier">repeat</a></td>
<td>[replace](#replacestring-search-string-replacement)</td> <td><a href="#replacestring-search-string-replacement">replace</a></td>
</tr> </tr>
<tr> <tr>
<td>[reverse](#reverse)</td> <td><a href="#reverse">reverse</a></td>
<td>[safeTruncate](#safetruncateint-length--string-substring---)</td> <td><a href="#safetruncateint-length--string-substring---">safeTruncate</a></td>
<td>[shuffle](#shuffle)</td> <td><a href="#shuffle">shuffle</a></td>
<td>[slugify](#slugify-string-replacement----)</td> <td><a href="#slugify-string-replacement-----string-language--en">slugify</a></td>
</tr> </tr>
<tr> <tr>
<td>[startsWith](#startswithstring-substring--boolean-casesensitive--true-)</td> <td><a href="#sliceint-start--int-end-">slice</a></td>
<td>[startsWithAny](#startswithanystring-substrings--boolean-casesensitive--true-)</td> <td><a href="#splitstring-pattern--int-limit-">split</a></td>
<td>[slice](#sliceint-start--int-end-)</td> <td><a href="#startswithstring-substring--boolean-casesensitive--true-">startsWith</a></td>
<td>[split](#splitstring-pattern--int-limit-)</td> <td><a href="#startswithanystring-substrings--boolean-casesensitive--true-">startsWithAny</a></td>
</tr> </tr>
<tr> <tr>
<td>[stripWhitespace](#stripwhitespace)</td> <td><a href="#stripwhitespace">stripWhitespace</a></td>
<td>[substr](#substrint-start--int-length-)</td> <td><a href="#substrint-start--int-length-">substr</a></td>
<td>[surround](#surroundstring-substring)</td> <td><a href="#surroundstring-substring">surround</a></td>
<td>[swapCase](#swapcase)</td> <td><a href="#swapcase">swapCase</a></td>
</tr> </tr>
<tr> <tr>
<td>[tidy](#tidy)</td> <td><a href="#tidy">tidy</a></td>
<td>[titleize](#titleize-array-ignore)</td> <td><a href="#titleize-array-ignore">titleize</a></td>
<td>[toAscii](#toascii)</td> <td><a href="#toascii-string-language--en--bool-removeunsupported--true-">toAscii</a></td>
<td>[toBoolean](#toboolean)</td> <td><a href="#toboolean">toBoolean</a></td>
</tr> </tr>
<tr> <tr>
<td>[toLowerCase](#tolowercase)</td> <td><a href="#tolowercase">toLowerCase</a></td>
<td>[toSpaces](#tospaces-tablength--4-)</td> <td><a href="#tospaces-tablength--4-">toSpaces</a></td>
<td>[toTabs](#totabs-tablength--4-)</td> <td><a href="#totabs-tablength--4-">toTabs</a></td>
<td>[toTitleCase](#totitlecase)</td> <td><a href="#totitlecase">toTitleCase</a></td>
</tr> </tr>
<tr> <tr>
<td>[toUpperCase](#touppercase)</td> <td><a href="#touppercase">toUpperCase</a></td>
<td>[trim](#trim-string-chars)</td> <td><a href="#trim-string-chars">trim</a></td>
<td>[trimLeft](#trimleft-string-chars)</td> <td><a href="#trimleft-string-chars">trimLeft</a></td>
<td>[trimRight](#trimright-string-chars)</td> <td><a href="#trimright-string-chars">trimRight</a></td>
</tr> </tr>
<tr> <tr>
<td>[truncate](#truncateint-length--string-substring---)</td> <td><a href="#truncateint-length--string-substring---">truncate</a></td>
<td>[underscored](#underscored)</td> <td><a href="#underscored">underscored</a></td>
<td>[upperCamelize](#uppercamelize)</td> <td><a href="#uppercamelize">upperCamelize</a></td>
<td>[upperCaseFirst](#uppercasefirst)</td> <td><a href="#uppercasefirst">upperCaseFirst</a></td>
</tr> </tr>
</table> </table>
* [Extensions](#extensions) * [Extensions](#extensions)
* [Tests](#tests) * [Tests](#tests)
* [License](#license) * [License](#license)
@@ -176,7 +180,7 @@ in your composer.json file:
```json ```json
"require": { "require": {
"danielstjules/stringy": "~2.4" "danielstjules/stringy": "~3.1.0"
} }
``` ```
@@ -433,7 +437,7 @@ default, the comparison is case-sensitive, but can be made insensitive by
setting $caseSensitive to false. setting $caseSensitive to false.
```php ```php
s('fòôbàř')->endsWith('bàř', true); // true s('fòôbàř')->endsWith('bàř'); // true
``` ```
##### endsWithAny(string[] $substrings [, boolean $caseSensitive = true ]) ##### endsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
@@ -443,7 +447,7 @@ By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false. by setting $caseSensitive to false.
```php ```php
s('fòôbàř')->endsWith(['bàř', 'baz'], true); // true s('fòôbàř')->endsWithAny(['bàř', 'baz']); // true
``` ```
##### ensureLeft(string $substring) ##### ensureLeft(string $substring)
@@ -806,38 +810,19 @@ random order.
s('fòôbàř')->shuffle(); // 'àôřbòf' s('fòôbàř')->shuffle(); // 'àôřbòf'
``` ```
##### slugify([, string $replacement = '-' ]) ##### slugify([, string $replacement = '-' [, string $language = 'en']])
Converts the string into an URL slug. This includes replacing non-ASCII Converts the string into an URL slug. This includes replacing non-ASCII
characters with their closest ASCII equivalents, removing remaining characters with their closest ASCII equivalents, removing remaining
non-ASCII and non-alphanumeric characters, and replacing whitespace with non-ASCII and non-alphanumeric characters, and replacing whitespace with
$replacement. The replacement defaults to a single dash, and the string $replacement. The replacement defaults to a single dash, and the string
is also converted to lowercase. is also converted to lowercase. The language of the source string can
also be supplied for language-specific transliteration.
```php ```php
s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar' s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar'
``` ```
##### startsWith(string $substring [, boolean $caseSensitive = true ])
Returns true if the string begins with $substring, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
s('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
```
##### startsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
Returns true if the string begins with any of $substrings, false
otherwise. By default the comparison is case-sensitive, but can be made
insensitive by setting $caseSensitive to false.
```php
s('FÒÔbàřbaz')->startsWith(['fòô', 'bàř'], false); // true
```
##### slice(int $start [, int $end ]) ##### slice(int $start [, int $end ])
Returns the substring beginning at $start, and up to, but not including Returns the substring beginning at $start, and up to, but not including
@@ -859,6 +844,26 @@ results.
s('foo,bar,baz')->split(',', 2); // ['foo', 'bar'] s('foo,bar,baz')->split(',', 2); // ['foo', 'bar']
``` ```
##### startsWith(string $substring [, boolean $caseSensitive = true ])
Returns true if the string begins with $substring, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
s('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
```
##### startsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
Returns true if the string begins with any of $substrings, false
otherwise. By default the comparison is case-sensitive, but can be made
insensitive by setting $caseSensitive to false.
```php
s('FÒÔbàřbaz')->startsWithAny(['fòô', 'bàř'], false); // true
```
##### stripWhitespace() ##### stripWhitespace()
Strip all whitespace characters. This includes tabs and newline Strip all whitespace characters. This includes tabs and newline
@@ -916,14 +921,19 @@ s('i like to watch television')->titleize($ignore);
// 'I Like to Watch Television' // 'I Like to Watch Television'
``` ```
##### toAscii() ##### toAscii([, string $language = 'en' [, bool $removeUnsupported = true ]])
Returns an ASCII version of the string. A set of non-ASCII characters are Returns an ASCII version of the string. A set of non-ASCII characters are
replaced with their closest ASCII counterparts, and the rest are removed replaced with their closest ASCII counterparts, and the rest are removed
unless instructed otherwise. by default. The language or locale of the source string can be supplied
for language-specific transliteration in any of the following formats:
en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
to "aeoeue" rather than "aou" as in other languages.
```php ```php
s('fòôbàř')->toAscii(); // 'foobar' s('fòôbàř')->toAscii(); // 'foobar'
s('äöü')->toAscii(); // 'aou'
s('äöü')->toAscii('de'); // 'aeoeue'
``` ```
##### toBoolean() ##### toBoolean()

View File

@@ -15,7 +15,7 @@
} }
], ],
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.4.0",
"symfony/polyfill-mbstring": "~1.1" "symfony/polyfill-mbstring": "~1.1"
}, },
"require-dev": { "require-dev": {

View File

@@ -16,13 +16,14 @@ use ReflectionMethod;
* @method static string chars(string $str, string $encoding = null) * @method static string chars(string $str, string $encoding = null)
* @method static string collapseWhitespace(string $str, string $encoding = null) * @method static string collapseWhitespace(string $str, string $encoding = null)
* @method static bool contains(string $str, string $needle, bool $caseSensitive = true, string $encoding = null) * @method static bool contains(string $str, string $needle, bool $caseSensitive = true, string $encoding = null)
* @method static bool containsAll(string $str, string $needle, bool $caseSensitive = true, string $encoding = null) * @method static bool containsAll(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
* @method static bool containsAny(string $str, string $needle, bool $caseSensitive = true, string $encoding = null) * @method static bool containsAny(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
* @method static int count(string $str, string $encoding = null) * @method static int count(string $str, string $encoding = null)
* @method static int countSubstr(string $str, string $substring, bool $caseSensitive = true, string $encoding = null) * @method static int countSubstr(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static string dasherize(string $str, string $encoding = null) * @method static string dasherize(string $str, string $encoding = null)
* @method static string delimit(string $str, string $delimiter, string $encoding = null) * @method static string delimit(string $str, string $delimiter, string $encoding = null)
* @method static bool endsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null) * @method static bool endsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static bool endsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
* @method static string ensureLeft(string $str, string $substring, string $encoding = null) * @method static string ensureLeft(string $str, string $substring, string $encoding = null)
* @method static string ensureRight(string $str, string $substring, string $encoding = null) * @method static string ensureRight(string $str, string $substring, string $encoding = null)
* @method static string first(string $str, int $n, string $encoding = null) * @method static string first(string $str, int $n, string $encoding = null)
@@ -45,7 +46,7 @@ use ReflectionMethod;
* @method static bool isUpperCase(string $str, string $encoding = null) * @method static bool isUpperCase(string $str, string $encoding = null)
* @method static string last(string $str, string $encoding = null) * @method static string last(string $str, string $encoding = null)
* @method static int length(string $str, string $encoding = null) * @method static int length(string $str, string $encoding = null)
* @method static Stringy[] lines(string $str, string $encoding = null) * @method static string[] lines(string $str, string $encoding = null)
* @method static string longestCommonPrefix(string $str, string $otherStr, string $encoding = null) * @method static string longestCommonPrefix(string $str, string $otherStr, string $encoding = null)
* @method static string longestCommonSuffix(string $str, string $otherStr, string $encoding = null) * @method static string longestCommonSuffix(string $str, string $otherStr, string $encoding = null)
* @method static string longestCommonSubstring(string $str, string $otherStr, string $encoding = null) * @method static string longestCommonSubstring(string $str, string $otherStr, string $encoding = null)
@@ -64,15 +65,17 @@ use ReflectionMethod;
* @method static string safeTruncate(string $str, int $length, string $substring = '', string $encoding = null) * @method static string safeTruncate(string $str, int $length, string $substring = '', string $encoding = null)
* @method static string shuffle(string $str, string $encoding = null) * @method static string shuffle(string $str, string $encoding = null)
* @method static string slugify(string $str, string $replacement = '-', string $encoding = null) * @method static string slugify(string $str, string $replacement = '-', string $encoding = null)
* @method static bool startsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static string slice(string $str, int $start, int $end = null, string $encoding = null) * @method static string slice(string $str, int $start, int $end = null, string $encoding = null)
* @method static string split(string $str, string $pattern, int $limit = null, string $encoding = null) * @method static string split(string $str, string $pattern, int $limit = null, string $encoding = null)
* @method static bool startsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
* @method static bool startsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
* @method static string stripWhitespace(string $str, string $encoding = null)
* @method static string substr(string $str, int $start, int $length = null, string $encoding = null) * @method static string substr(string $str, int $start, int $length = null, string $encoding = null)
* @method static string surround(string $str, string $substring, string $encoding = null) * @method static string surround(string $str, string $substring, string $encoding = null)
* @method static string swapCase(string $str, string $encoding = null) * @method static string swapCase(string $str, string $encoding = null)
* @method static string tidy(string $str, string $encoding = null) * @method static string tidy(string $str, string $encoding = null)
* @method static string titleize(string $str, string $encoding = null) * @method static string titleize(string $str, string $encoding = null)
* @method static string toAscii(string $str, bool $removeUnsupported = true, string $encoding = null) * @method static string toAscii(string $str, string $language = 'en', bool $removeUnsupported = true, string $encoding = null)
* @method static bool toBoolean(string $str, string $encoding = null) * @method static bool toBoolean(string $str, string $encoding = null)
* @method static string toLowerCase(string $str, string $encoding = null) * @method static string toLowerCase(string $str, string $encoding = null)
* @method static string toSpaces(string $str, int $tabLength = 4, string $encoding = null) * @method static string toSpaces(string $str, int $tabLength = 4, string $encoding = null)
@@ -143,6 +146,16 @@ class StaticStringy
$stringy = Stringy::create($str, $encoding); $stringy = Stringy::create($str, $encoding);
return call_user_func_array(array($stringy, $name), $args); $result = call_user_func_array([$stringy, $name], $args);
$cast = function($val) {
if (is_object($val) && $val instanceof Stringy) {
return (string) $val;
} else {
return $val;
}
};
return is_array($result) ? array_map($cast, $result) : $cast($result);
} }
} }

View File

@@ -62,8 +62,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* then returns the initialized object. Throws an InvalidArgumentException * then returns the initialized object. Throws an InvalidArgumentException
* if the first argument is an array or object without a __toString method. * if the first argument is an array or object without a __toString method.
* *
* @param mixed $str Value to modify, after being cast to string * @param mixed $str Value to modify, after being cast to string
* @param string $encoding The character encoding * @param string $encoding The character encoding
* @return static A Stringy object * @return static A Stringy object
* @throws \InvalidArgumentException if an array or object without a * @throws \InvalidArgumentException if an array or object without a
* __toString method is passed as the first argument * __toString method is passed as the first argument
@@ -86,7 +86,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns a new string with $string appended. * Returns a new string with $string appended.
* *
* @param string $string The string to append * @param string $string The string to append
* @return static Object with appended $string * @return static Object with appended $string
*/ */
public function append($string) public function append($string)
@@ -97,7 +97,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns the character at $index, with indexes starting at 0. * Returns the character at $index, with indexes starting at 0.
* *
* @param int $index Position of the character * @param int $index Position of the character
* @return static The character at $index * @return static The character at $index
*/ */
public function at($index) public function at($index)
@@ -174,7 +174,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function chars() public function chars()
{ {
$chars = array(); $chars = [];
for ($i = 0, $l = $this->length(); $i < $l; $i++) { for ($i = 0, $l = $this->length(); $i < $l; $i++) {
$chars[] = $this->at($i)->str; $chars[] = $this->at($i)->str;
} }
@@ -219,9 +219,9 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* default the comparison is case-sensitive, but can be made insensitive by * default the comparison is case-sensitive, but can be made insensitive by
* setting $caseSensitive to false. * setting $caseSensitive to false.
* *
* @param array $needles Substrings to look for * @param string[] $needles Substrings to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity * @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str contains $needle * @return bool Whether or not $str contains $needle
*/ */
public function containsAll($needles, $caseSensitive = true) public function containsAll($needles, $caseSensitive = true)
{ {
@@ -243,9 +243,9 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* default the comparison is case-sensitive, but can be made insensitive by * default the comparison is case-sensitive, but can be made insensitive by
* setting $caseSensitive to false. * setting $caseSensitive to false.
* *
* @param array $needles Substrings to look for * @param string[] $needles Substrings to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity * @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str contains $needle * @return bool Whether or not $str contains $needle
*/ */
public function containsAny($needles, $caseSensitive = true) public function containsAny($needles, $caseSensitive = true)
{ {
@@ -311,7 +311,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* of the first character of the string), and in place of spaces, dashes, * of the first character of the string), and in place of spaces, dashes,
* and underscores. Alpha delimiters are not converted to lowercase. * and underscores. Alpha delimiters are not converted to lowercase.
* *
* @param string $delimiter Sequence used to separate parts of the string * @param string $delimiter Sequence used to separate parts of the string
* @return static Object with a delimited $str * @return static Object with a delimited $str
*/ */
public function delimit($delimiter) public function delimit($delimiter)
@@ -382,7 +382,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Ensures that the string begins with $substring. If it doesn't, it's * Ensures that the string begins with $substring. If it doesn't, it's
* prepended. * prepended.
* *
* @param string $substring The substring to add if not present * @param string $substring The substring to add if not present
* @return static Object with its $str prefixed by the $substring * @return static Object with its $str prefixed by the $substring
*/ */
public function ensureLeft($substring) public function ensureLeft($substring)
@@ -400,7 +400,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Ensures that the string ends with $substring. If it doesn't, it's * Ensures that the string ends with $substring. If it doesn't, it's
* appended. * appended.
* *
* @param string $substring The substring to add if not present * @param string $substring The substring to add if not present
* @return static Object with its $str suffixed by the $substring * @return static Object with its $str suffixed by the $substring
*/ */
public function ensureRight($substring) public function ensureRight($substring)
@@ -417,7 +417,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns the first $n characters of the string. * Returns the first $n characters of the string.
* *
* @param int $n Number of characters to retrieve from the start * @param int $n Number of characters to retrieve from the start
* @return static Object with its $str being the first $n chars * @return static Object with its $str being the first $n chars
*/ */
public function first($n) public function first($n)
@@ -484,7 +484,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* http://php.net/manual/en/function.html-entity-decode.php * http://php.net/manual/en/function.html-entity-decode.php
* *
* @param int|null $flags Optional flags * @param int|null $flags Optional flags
* @return static Object with the resulting $str after being html decoded. * @return static Object with the resulting $str after being html decoded.
*/ */
public function htmlDecode($flags = ENT_COMPAT) public function htmlDecode($flags = ENT_COMPAT)
{ {
@@ -499,7 +499,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* for a list of flags. * for a list of flags.
* *
* @param int|null $flags Optional flags * @param int|null $flags Optional flags
* @return static Object with the resulting $str after being html encoded. * @return static Object with the resulting $str after being html encoded.
*/ */
public function htmlEncode($flags = ENT_COMPAT) public function htmlEncode($flags = ENT_COMPAT)
{ {
@@ -516,7 +516,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function humanize() public function humanize()
{ {
$str = str_replace(array('_id', '_'), array('', ' '), $this->str); $str = str_replace(['_id', '_'], ['', ' '], $this->str);
return static::create($str, $this->encoding)->trim()->upperCaseFirst(); return static::create($str, $this->encoding)->trim()->upperCaseFirst();
} }
@@ -555,8 +555,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Inserts $substring into the string at the $index provided. * Inserts $substring into the string at the $index provided.
* *
* @param string $substring String to be inserted * @param string $substring String to be inserted
* @param int $index The index at which to insert the substring * @param int $index The index at which to insert the substring
* @return static Object with the resulting $str after the insertion * @return static Object with the resulting $str after the insertion
*/ */
public function insert($substring, $index) public function insert($substring, $index)
@@ -683,7 +683,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns the last $n characters of the string. * Returns the last $n characters of the string.
* *
* @param int $n Number of characters to retrieve from the end * @param int $n Number of characters to retrieve from the end
* @return static Object with its $str being the last $n chars * @return static Object with its $str being the last $n chars
*/ */
public function last($n) public function last($n)
@@ -727,7 +727,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns the longest common prefix between the string and $otherStr. * Returns the longest common prefix between the string and $otherStr.
* *
* @param string $otherStr Second string for comparison * @param string $otherStr Second string for comparison
* @return static Object with its $str being the longest common prefix * @return static Object with its $str being the longest common prefix
*/ */
public function longestCommonPrefix($otherStr) public function longestCommonPrefix($otherStr)
@@ -752,7 +752,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns the longest common suffix between the string and $otherStr. * Returns the longest common suffix between the string and $otherStr.
* *
* @param string $otherStr Second string for comparison * @param string $otherStr Second string for comparison
* @return static Object with its $str being the longest common suffix * @return static Object with its $str being the longest common suffix
*/ */
public function longestCommonSuffix($otherStr) public function longestCommonSuffix($otherStr)
@@ -778,7 +778,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Returns the longest common substring between the string and $otherStr. * Returns the longest common substring between the string and $otherStr.
* In the case of ties, it returns that which occurs first. * In the case of ties, it returns that which occurs first.
* *
* @param string $otherStr Second string for comparison * @param string $otherStr Second string for comparison
* @return static Object with its $str being the longest common substring * @return static Object with its $str being the longest common substring
*/ */
public function longestCommonSubstring($otherStr) public function longestCommonSubstring($otherStr)
@@ -916,16 +916,16 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
* if $padType isn't one of those 3 values. * if $padType isn't one of those 3 values.
* *
* @param int $length Desired string length after padding * @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space * @param string $padStr String used to pad, defaults to space
* @param string $padType One of 'left', 'right', 'both' * @param string $padType One of 'left', 'right', 'both'
* @return static Object with a padded $str * @return static Object with a padded $str
* @throws /InvalidArgumentException If $padType isn't one of 'right', * @throws /InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both' * 'left' or 'both'
*/ */
public function pad($length, $padStr = ' ', $padType = 'right') public function pad($length, $padStr = ' ', $padType = 'right')
{ {
if (!in_array($padType, array('left', 'right', 'both'))) { if (!in_array($padType, ['left', 'right', 'both'])) {
throw new InvalidArgumentException('Pad expects $padType ' . throw new InvalidArgumentException('Pad expects $padType ' .
"to be one of 'left', 'right' or 'both'"); "to be one of 'left', 'right' or 'both'");
} }
@@ -944,8 +944,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Returns a new string of a given length such that both sides of the * Returns a new string of a given length such that both sides of the
* string are padded. Alias for pad() with a $padType of 'both'. * string are padded. Alias for pad() with a $padType of 'both'.
* *
* @param int $length Desired string length after padding * @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space * @param string $padStr String used to pad, defaults to space
* @return static String with padding applied * @return static String with padding applied
*/ */
public function padBoth($length, $padStr = ' ') public function padBoth($length, $padStr = ' ')
@@ -960,8 +960,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Returns a new string of a given length such that the beginning of the * Returns a new string of a given length such that the beginning of the
* string is padded. Alias for pad() with a $padType of 'left'. * string is padded. Alias for pad() with a $padType of 'left'.
* *
* @param int $length Desired string length after padding * @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space * @param string $padStr String used to pad, defaults to space
* @return static String with left padding * @return static String with left padding
*/ */
public function padLeft($length, $padStr = ' ') public function padLeft($length, $padStr = ' ')
@@ -973,8 +973,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Returns a new string of a given length such that the end of the string * Returns a new string of a given length such that the end of the string
* is padded. Alias for pad() with a $padType of 'right'. * is padded. Alias for pad() with a $padType of 'right'.
* *
* @param int $length Desired string length after padding * @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space * @param string $padStr String used to pad, defaults to space
* @return static String with right padding * @return static String with right padding
*/ */
public function padRight($length, $padStr = ' ') public function padRight($length, $padStr = ' ')
@@ -985,7 +985,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns a new string starting with $string. * Returns a new string starting with $string.
* *
* @param string $string The string to append * @param string $string The string to append
* @return static Object with appended $string * @return static Object with appended $string
*/ */
public function prepend($string) public function prepend($string)
@@ -1000,9 +1000,9 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* to a lack of support in the bundled version of Oniguruma in PHP < 5.6, * to a lack of support in the bundled version of Oniguruma in PHP < 5.6,
* and current versions of HHVM (3.8 and below). * and current versions of HHVM (3.8 and below).
* *
* @param string $pattern The regular expression pattern * @param string $pattern The regular expression pattern
* @param string $replacement The string to replace with * @param string $replacement The string to replace with
* @param string $options Matching conditions to be used * @param string $options Matching conditions to be used
* @return static Object with the resulting $str after the replacements * @return static Object with the resulting $str after the replacements
*/ */
public function regexReplace($pattern, $replacement, $options = 'msr') public function regexReplace($pattern, $replacement, $options = 'msr')
@@ -1019,7 +1019,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns a new string with the prefix $substring removed, if present. * Returns a new string with the prefix $substring removed, if present.
* *
* @param string $substring The prefix to remove * @param string $substring The prefix to remove
* @return static Object having a $str without the prefix $substring * @return static Object having a $str without the prefix $substring
*/ */
public function removeLeft($substring) public function removeLeft($substring)
@@ -1037,7 +1037,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns a new string with the suffix $substring removed, if present. * Returns a new string with the suffix $substring removed, if present.
* *
* @param string $substring The suffix to remove * @param string $substring The suffix to remove
* @return static Object having a $str without the suffix $substring * @return static Object having a $str without the suffix $substring
*/ */
public function removeRight($substring) public function removeRight($substring)
@@ -1055,7 +1055,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns a repeated string given a multiplier. An alias for str_repeat. * Returns a repeated string given a multiplier. An alias for str_repeat.
* *
* @param int $multiplier The number of times to repeat the string * @param int $multiplier The number of times to repeat the string
* @return static Object with a repeated str * @return static Object with a repeated str
*/ */
public function repeat($multiplier) public function repeat($multiplier)
@@ -1068,8 +1068,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Replaces all occurrences of $search in $str by $replacement. * Replaces all occurrences of $search in $str by $replacement.
* *
* @param string $search The needle to search for * @param string $search The needle to search for
* @param string $replacement The string to replace with * @param string $replacement The string to replace with
* @return static Object with the resulting $str after the replacements * @return static Object with the resulting $str after the replacements
*/ */
public function replace($search, $replacement) public function replace($search, $replacement)
@@ -1101,8 +1101,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* string is further truncated so that the substring may be appended without * string is further truncated so that the substring may be appended without
* exceeding the desired length. * exceeding the desired length.
* *
* @param int $length Desired length of the truncated string * @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit * @param string $substring The substring to append if it can fit
* @return static Object with the resulting $str after truncating * @return static Object with the resulting $str after truncating
*/ */
public function safeTruncate($length, $substring = '') public function safeTruncate($length, $substring = '')
@@ -1157,15 +1157,18 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* characters with their closest ASCII equivalents, removing remaining * characters with their closest ASCII equivalents, removing remaining
* non-ASCII and non-alphanumeric characters, and replacing whitespace with * non-ASCII and non-alphanumeric characters, and replacing whitespace with
* $replacement. The replacement defaults to a single dash, and the string * $replacement. The replacement defaults to a single dash, and the string
* is also converted to lowercase. * is also converted to lowercase. The language of the source string can
* also be supplied for language-specific transliteration.
* *
* @param string $replacement The string used to replace whitespace * @param string $replacement The string used to replace whitespace
* @param string $language Language of the source string
* @return static Object whose $str has been converted to an URL slug * @return static Object whose $str has been converted to an URL slug
*/ */
public function slugify($replacement = '-') public function slugify($replacement = '-', $language = 'en')
{ {
$stringy = $this->toAscii(); $stringy = $this->toAscii($language);
$stringy->str = str_replace('@', $replacement, $stringy);
$quotedReplacement = preg_quote($replacement); $quotedReplacement = preg_quote($replacement);
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u"; $pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
$stringy->str = preg_replace($pattern, '', $stringy); $stringy->str = preg_replace($pattern, '', $stringy);
@@ -1180,7 +1183,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* by setting $caseSensitive to false. * by setting $caseSensitive to false.
* *
* @param string $substring The substring to look for * @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity * @param bool $caseSensitive Whether or not to enforce
* case-sensitivity
* @return bool Whether or not $str starts with $substring * @return bool Whether or not $str starts with $substring
*/ */
public function startsWith($substring, $caseSensitive = true) public function startsWith($substring, $caseSensitive = true)
@@ -1228,8 +1232,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* the remaining string. If $end is negative, it is computed from the end * the remaining string. If $end is negative, it is computed from the end
* of the string. * of the string.
* *
* @param int $start Initial index from which to begin extraction * @param int $start Initial index from which to begin extraction
* @param int $end Optional index at which to end extraction * @param int $end Optional index at which to end extraction
* @return static Object with its $str being the extracted substring * @return static Object with its $str being the extracted substring
*/ */
public function slice($start, $end = null) public function slice($start, $end = null)
@@ -1252,20 +1256,20 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* array of Stringy objects. An optional integer $limit will truncate the * array of Stringy objects. An optional integer $limit will truncate the
* results. * results.
* *
* @param string $pattern The regex with which to split the string * @param string $pattern The regex with which to split the string
* @param int $limit Optional maximum number of results to return * @param int $limit Optional maximum number of results to return
* @return static[] An array of Stringy objects * @return static[] An array of Stringy objects
*/ */
public function split($pattern, $limit = null) public function split($pattern, $limit = null)
{ {
if ($limit === 0) { if ($limit === 0) {
return array(); return [];
} }
// mb_split errors when supplied an empty pattern in < PHP 5.4.13 // mb_split errors when supplied an empty pattern in < PHP 5.4.13
// and HHVM < 3.8 // and HHVM < 3.8
if ($pattern === '') { if ($pattern === '') {
return array(static::create($this->str, $this->encoding)); return [static::create($this->str, $this->encoding)];
} }
$regexEncoding = $this->regexEncoding(); $regexEncoding = $this->regexEncoding();
@@ -1316,8 +1320,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* It differs from the mb_substr() function in that providing a $length of * It differs from the mb_substr() function in that providing a $length of
* null will return the rest of the string, rather than an empty string. * null will return the rest of the string, rather than an empty string.
* *
* @param int $start Position of the first character to use * @param int $start Position of the first character to use
* @param int $length Maximum number of characters used * @param int $length Maximum number of characters used
* @return static Object with its $str being the substring * @return static Object with its $str being the substring
*/ */
public function substr($start, $length = null) public function substr($start, $length = null)
@@ -1331,13 +1335,13 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Surrounds $str with the given substring. * Surrounds $str with the given substring.
* *
* @param string $substring The substring to add to both sides * @param string $substring The substring to add to both sides
* @return static Object whose $str had the substring both prepended and * @return static Object whose $str had the substring both prepended and
* appended * appended
*/ */
public function surround($substring) public function surround($substring)
{ {
$str = implode('', array($substring, $this->str, $substring)); $str = implode('', [$substring, $this->str, $substring]);
return static::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
@@ -1376,17 +1380,17 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
*/ */
public function tidy() public function tidy()
{ {
$str = preg_replace(array( $str = preg_replace([
'/\x{2026}/u', '/\x{2026}/u',
'/[\x{201C}\x{201D}]/u', '/[\x{201C}\x{201D}]/u',
'/[\x{2018}\x{2019}]/u', '/[\x{2018}\x{2019}]/u',
'/[\x{2013}\x{2014}]/u', '/[\x{2013}\x{2014}]/u',
), array( ], [
'...', '...',
'"', '"',
"'", "'",
'-', '-',
), $this->str); ], $this->str);
return static::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
@@ -1396,7 +1400,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* 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 array $ignore An array of words not to capitalize * @param array $ignore An array of words not to capitalize
* @return static Object with a titleized $str * @return static Object with a titleized $str
*/ */
public function titleize($ignore = null) public function titleize($ignore = null)
@@ -1424,16 +1428,25 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
/** /**
* Returns an ASCII version of the string. A set of non-ASCII characters are * Returns an ASCII version of the string. A set of non-ASCII characters are
* replaced with their closest ASCII counterparts, and the rest are removed * replaced with their closest ASCII counterparts, and the rest are removed
* unless instructed otherwise. * by default. The language or locale of the source string can be supplied
* for language-specific transliteration in any of the following formats:
* en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
* to "aeoeue" rather than "aou" as in other languages.
* *
* @param bool $removeUnsupported Whether or not to remove the * @param string $language Language of the source string
* @param bool $removeUnsupported Whether or not to remove the
* unsupported characters * unsupported characters
* @return static Object whose $str contains only ASCII characters * @return static Object whose $str contains only ASCII characters
*/ */
public function toAscii($removeUnsupported = true) public function toAscii($language = 'en', $removeUnsupported = true)
{ {
$str = $this->str; $str = $this->str;
$langSpecific = $this->langSpecificCharsArray($language);
if (!empty($langSpecific)) {
$str = str_replace($langSpecific[0], $langSpecific[1], $str);
}
foreach ($this->charsArray() as $key => $value) { foreach ($this->charsArray() as $key => $value) {
$str = str_replace($value, $key, $str); $str = str_replace($value, $key, $str);
} }
@@ -1459,7 +1472,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
public function toBoolean() public function toBoolean()
{ {
$key = $this->toLowerCase()->str; $key = $this->toLowerCase()->str;
$map = array( $map = [
'true' => true, 'true' => true,
'1' => true, '1' => true,
'on' => true, 'on' => true,
@@ -1468,7 +1481,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
'0' => false, '0' => false,
'off' => false, 'off' => false,
'no' => false 'no' => false
); ];
if (array_key_exists($key, $map)) { if (array_key_exists($key, $map)) {
return $map[$key]; return $map[$key];
@@ -1496,7 +1509,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Converts each tab in the string to some number of spaces, as defined by * Converts each tab in the string to some number of spaces, as defined by
* $tabLength. By default, each tab is converted to 4 consecutive spaces. * $tabLength. By default, each tab is converted to 4 consecutive spaces.
* *
* @param int $tabLength Number of spaces to replace each tab with * @param int $tabLength Number of spaces to replace each tab with
* @return static Object whose $str has had tabs switched to spaces * @return static Object whose $str has had tabs switched to spaces
*/ */
public function toSpaces($tabLength = 4) public function toSpaces($tabLength = 4)
@@ -1512,7 +1525,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* defined by $tabLength, to a tab. By default, each 4 consecutive spaces * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
* are converted to a tab. * are converted to a tab.
* *
* @param int $tabLength Number of spaces to replace with a tab * @param int $tabLength Number of spaces to replace with a tab
* @return static Object whose $str has had spaces switched to tabs * @return static Object whose $str has had spaces switched to tabs
*/ */
public function toTabs($tabLength = 4) public function toTabs($tabLength = 4)
@@ -1553,7 +1566,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* string. Supports the removal of unicode whitespace. Accepts an optional * string. Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults. * string of characters to strip instead of the defaults.
* *
* @param string $chars Optional string of characters to strip * @param string $chars Optional string of characters to strip
* @return static Object with a trimmed $str * @return static Object with a trimmed $str
*/ */
public function trim($chars = null) public function trim($chars = null)
@@ -1568,7 +1581,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Supports the removal of unicode whitespace. Accepts an optional * Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults. * string of characters to strip instead of the defaults.
* *
* @param string $chars Optional string of characters to strip * @param string $chars Optional string of characters to strip
* @return static Object with a trimmed $str * @return static Object with a trimmed $str
*/ */
public function trimLeft($chars = null) public function trimLeft($chars = null)
@@ -1583,7 +1596,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* Supports the removal of unicode whitespace. Accepts an optional * Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults. * string of characters to strip instead of the defaults.
* *
* @param string $chars Optional string of characters to strip * @param string $chars Optional string of characters to strip
* @return static Object with a trimmed $str * @return static Object with a trimmed $str
*/ */
public function trimRight($chars = null) public function trimRight($chars = null)
@@ -1598,8 +1611,8 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
* truncating occurs, the string is further truncated so that the substring * truncating occurs, the string is further truncated so that the substring
* may be appended without exceeding the desired length. * may be appended without exceeding the desired length.
* *
* @param int $length Desired length of the truncated string * @param int $length Desired length of the truncated string
* @param string $substring The substring to append if it can fit * @param string $substring The substring to append if it can fit
* @return static Object with the resulting $str after truncating * @return static Object with the resulting $str after truncating
*/ */
public function truncate($length, $substring = '') public function truncate($length, $substring = '')
@@ -1670,180 +1683,216 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
static $charsArray; static $charsArray;
if (isset($charsArray)) return $charsArray; if (isset($charsArray)) return $charsArray;
return $charsArray = array( return $charsArray = [
'0' => array('°', '₀', '۰', ''), '0' => ['°', '₀', '۰', ''],
'1' => array('¹', '₁', '۱', ''), '1' => ['¹', '₁', '۱', ''],
'2' => array('²', '₂', '۲', ''), '2' => ['²', '₂', '۲', ''],
'3' => array('³', '₃', '۳', ''), '3' => ['³', '₃', '۳', ''],
'4' => array('⁴', '₄', '۴', '٤', ''), '4' => ['⁴', '₄', '۴', '٤', ''],
'5' => array('⁵', '₅', '۵', '٥', ''), '5' => ['⁵', '₅', '۵', '٥', ''],
'6' => array('⁶', '₆', '۶', '٦', ''), '6' => ['⁶', '₆', '۶', '٦', ''],
'7' => array('⁷', '₇', '۷', ''), '7' => ['⁷', '₇', '۷', ''],
'8' => array('⁸', '₈', '۸', ''), '8' => ['⁸', '₈', '۸', ''],
'9' => array('⁹', '₉', '۹', ''), '9' => ['⁹', '₉', '۹', ''],
'a' => array('à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ',
'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å',
'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ',
'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά',
'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ',
'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', ''), 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', '', 'ä'],
'b' => array('б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ', ''), 'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', ''],
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ', ''), 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', ''],
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', ''), 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', ''],
'e' => array('é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ',
'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ',
'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э',
'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', ''), 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', ''],
'f' => array('ф', 'φ', 'ف', 'ƒ', 'ფ', ''), 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', ''],
'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ',
''), ''],
'h' => array('ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', ''), 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', ''],
'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į',
'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ',
'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ',
'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი',
'इ', 'ی', ''), 'इ', 'ی', ''],
'j' => array('ĵ', 'ј', 'Ј', 'ჯ', 'ج', ''), 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', ''],
'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ',
'ک', ''), 'ک', ''],
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ',
''), ''],
'm' => array('м', 'μ', 'م', 'မ', 'მ', ''), 'm' => ['м', 'μ', 'م', 'မ', 'მ', ''],
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န',
'ნ', ''), 'ნ', ''],
'o' => array('ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ',
'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő',
'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό',
'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', ''), 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', '',
'p' => array('п', 'π', 'ပ', 'პ', 'پ', ''), 'ö'],
'q' => array('', ''), 'p' => ['п', 'π', '', 'პ', 'پ', ''],
'r' => array('ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', '', ''), 'q' => ['', ''],
's' => array('ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', '', ''],
'ſ', '', ''), 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', '',
't' => array('ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', '', '', 'ŧ', 'ſ', '', ''],
'', '', ''), 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', '', '', 'ŧ',
'u' => array('ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', '', '', '', '', '', ''],
'', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', '', 'u' => ['ú', 'ù', '', 'ũ', '', 'ư', '', '', '', '',
'', '', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', '', '', '', '', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', '',
'ў'), 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', '',
'v' => array('в', 'ვ', 'ϐ', ''), 'ў', 'ü'],
'w' => array('ŵ', 'ω', 'ώ', '', '', ''), 'v' => ['в', '', 'ϐ', ''],
'x' => array('χ', 'ξ', ''), 'w' => ['ŵ', 'ω', 'ώ', '', '', ''],
'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', '', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'x' => ['χ', 'ξ', ''],
'ϋ', 'ύ', 'ΰ', 'ي', '', ''), 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', '', 'ÿ', 'ŷ', 'й', 'ы', 'υ',
'z' => array('ź', 'ž', 'ż', 'з', 'ζ', 'ز', '', '', ''), 'ϋ', 'ύ', 'ΰ', 'ي', '', ''],
'aa' => array('ع', '', 'آ'), 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', ''],
'ae' => array('ä', 'æ', 'ǽ'), 'aa' => ['ع', '', 'آ'],
'ai' => array(''), 'ae' => ['æ', 'ǽ'],
'at' => array('@'), 'ai' => [''],
'ch' => array('ч', 'ჩ', 'ჭ', 'چ'), 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
'dj' => array('ђ', 'đ'), 'dj' => ['ђ', 'đ'],
'dz' => array('џ', 'ძ'), 'dz' => ['џ', 'ძ'],
'ei' => array('ऍ'), 'ei' => ['ऍ'],
'gh' => array('غ', 'ღ'), 'gh' => ['غ', 'ღ'],
'ii' => array('ई'), 'ii' => ['ई'],
'ij' => array('ij'), 'ij' => ['ij'],
'kh' => array('х', 'خ', 'ხ'), 'kh' => ['х', 'خ', 'ხ'],
'lj' => array('љ'), 'lj' => ['љ'],
'nj' => array('њ'), 'nj' => ['њ'],
'oe' => array('ö', 'œ', 'ؤ'), 'oe' => ['œ', 'ؤ'],
'oi' => array('ऑ'), 'oi' => ['ऑ'],
'oii' => array('ऒ'), 'oii' => ['ऒ'],
'ps' => array('ψ'), 'ps' => ['ψ'],
'sh' => array('ш', 'შ', 'ش'), 'sh' => ['ш', 'შ', 'ش'],
'shch' => array('щ'), 'shch' => ['щ'],
'ss' => array('ß'), 'ss' => ['ß'],
'sx' => array('ŝ'), 'sx' => ['ŝ'],
'th' => array('þ', 'ϑ', 'ث', 'ذ', 'ظ'), 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
'ts' => array('ц', 'ც', 'წ'), 'ts' => ['ц', 'ც', 'წ'],
'ue' => array('ü'), 'uu' => [''],
'uu' => array(''), 'ya' => ['я'],
'ya' => array('я'), 'yu' => ['ю'],
'yu' => array('ю'), 'zh' => ['ж', 'ჟ', 'ژ'],
'zh' => array('ж', 'ჟ', 'ژ'), '(c)' => ['©'],
'(c)' => array('©'), 'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ',
'A' => array('Á', 'À', '', 'Ã', '', 'Ă', '', '', '', '', '', 'Â', '', '', '', '', '', 'Å', 'Ā', 'Ą',
'', 'Â', '', '', '', '', '', 'Å', 'Ā', 'Ą', 'Α', 'Ά', '', '', '', '', '', '', '', '',
'Α', 'Ά', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', 'Ᾰ', 'Ᾱ', '', 'Ά', '', 'А', 'Ǻ', 'Ǎ', '', 'Ä'],
'Ὰ', 'Ά', 'ᾼ', 'А', 'Ǻ', 'Ǎ', ''), 'B' => ['Б', 'Β', '', ''],
'B' => array('Б', 'Β', '', ''), 'C' => ['Ç','Ć', 'Č', 'Ĉ', 'Ċ', ''],
'C' => array('Ç','Ć', 'Č', 'Ĉ', 'Ċ', ''), 'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ',
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', ', ''],
''), 'E' => ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', '',
'E' => array('É', 'È', '', '', '', 'Ê', '', '', '', '', '', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', '',
'', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', '', '', '', '', '', '', 'Έ', '', 'Е', 'Ё', 'Э',
'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', '', 'Е', 'Ё', 'Э', ', 'Ə', ''],
'Є', 'Ə', ''), 'F' => ['Ф', 'Φ', ''],
'F' => array('Ф', 'Φ', ''), 'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', ''],
'G' => array('Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', ''), 'H' => ['Η', 'Ή', 'Ħ', ''],
'H' => array('Η', 'Ή', 'Ħ', ''), 'I' => ['Í', 'Ì', 'Ỉ', 'Ĩ', '', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į',
'I' => array('Í', 'Ì', '', 'Ĩ', '', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ', '', '', '', '', '', '',
'İ', 'Ι', 'Ί', 'Ϊ', '', '', '', '', '', '', 'Ἷ', '', '', '', 'Ί', 'И', 'І', 'Ї', 'Ǐ', 'ϒ',
'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', 'І', 'Ї', 'Ǐ', 'ϒ', ''],
''), 'J' => [''],
'J' => array(''), 'K' => ['К', 'Κ', ''],
'K' => array('К', 'Κ', ''), 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', ''],
'L' => array('Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', ''), 'M' => ['М', 'Μ', ''],
'M' => array('М', 'Μ', ''), 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', ''],
'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', ''), 'O' => ['Ó', 'Ò', '', 'Õ', '', 'Ô', '', '', '', '',
'O' => array('Ó', 'Ò', '', 'Õ', '', 'Ô', '', '', '', '', '', 'Ơ', '', '', '', '', '', 'Ø', 'Ō', 'Ő',
'', 'Ơ', '', '', '', '', '', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', '', '', '', '', '', '', '',
'Ŏ', 'Ο', 'Ό', '', '', '', '', '', '', '', 'Ό', 'О', 'Θ', 'Ө', 'Ǒ', 'Ǿ', '', 'Ö'],
'Ό', 'О', 'Θ', 'Ө', 'Ǒ', 'Ǿ', ''), 'P' => ['П', 'Π', ''],
'P' => array('П', 'Π', ''), 'Q' => [''],
'Q' => array(''), 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', ''],
'R' => array('Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', ''), 'S' => ['Ş', 'Ŝ', 'Ș', ', 'Ś', 'С', 'Σ', ''],
'S' => array('Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ', ''), 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', ''],
'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', ''), 'U' => ['Ú', 'Ù', '', 'Ũ', '', 'Ư', '', '', 'Ử', 'Ữ',
'U' => array('Ú', 'Ù', '', 'Ũ', '', 'Ư', '', '', '', '', '', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У', 'Ǔ', 'Ǖ',
'', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У', 'Ǔ', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', '', 'Ў', 'Ü'],
'Ǘ', 'Ǚ', 'Ǜ', '', 'Ў'), 'V' => ['В', ''],
'V' => array('В', ''), 'W' => ['Ω', 'Ώ', ', ''],
'W' => array('Ω', 'Ώ', 'Ŵ', ''), 'X' => ['Χ', 'Ξ', ''],
'X' => array('Χ', 'Ξ', ''), 'Y' => ['Ý', '', '', '', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ',
'Y' => array('Ý', 'Ỳ', 'Ỷ', 'Ỹ', '', 'Ÿ', '', '', '', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', ''],
'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', ''), 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', ''],
'Z' => array('Ź', 'Ž', 'Ż', 'З', 'Ζ', ''), 'AE' => ['Æ', 'Ǽ'],
'AE' => array('Ä', 'Æ', 'Ǽ'), 'Ch' => ['Ч'],
'CH' => array('Ч'), 'Dj' => ['Ђ'],
'DJ' => array('Ђ'), 'Dz' => ['Џ'],
'DZ' => array('Џ'), 'Gx' => ['Ĝ'],
'GX' => array('Ĝ'), 'Hx' => ['Ĥ'],
'HX' => array('Ĥ'), 'Ij' => ['IJ'],
'IJ' => array('IJ'), 'Jx' => ['Ĵ'],
'JX' => array('Ĵ'), 'Kh' => ['Х'],
'KH' => array('Х'), 'Lj' => ['Љ'],
'LJ' => array('Љ'), 'Nj' => ['Њ'],
'NJ' => array('Њ'), 'Oe' => ['Œ'],
'OE' => array('Ö', 'Œ'), 'Ps' => ['Ψ'],
'PS' => array('Ψ'), 'Sh' => ['Ш'],
'SH' => array('Ш'), 'Shch' => ['Щ'],
'SHCH' => array('Щ'), 'Ss' => [''],
'SS' => array(''), 'Th' => ['Þ'],
'TH' => array('Þ'), 'Ts' => ['Ц'],
'TS' => array('Ц'), 'Ya' => ['Я'],
'UE' => array('Ü'), 'Yu' => ['Ю'],
'YA' => array('Я'), 'Zh' => ['Ж'],
'YU' => array('Ю'), ' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81",
'ZH' => array('Ж'), "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84",
' ' => array("\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87",
"\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A",
"\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80",
"\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xEF\xBE\xA0"],
"\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", ];
"\xEF\xBE\xA0"), }
);
/**
* Returns language-specific replacements for the toAscii() method.
* For example, German will map 'ä' to 'ae', while other languages
* will simply return 'a'.
*
* @param string $language Language of the source string
* @return array An array of replacements.
*/
protected static function langSpecificCharsArray($language = 'en')
{
$split = preg_split('/[-_]/', $language);
$language = strtolower($split[0]);
static $charsArray = [];
if (isset($charsArray[$language])) {
return $charsArray[$language];
}
$languageSpecific = [
'de' => [
['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü' ],
['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],
],
'bg' => [
['х', 'Х', 'щ', 'Щ', 'ъ', 'Ъ', 'ь', 'Ь'],
['h', 'H', 'sht', 'SHT', 'a', 'А', 'y', 'Y']
]
];
if (isset($languageSpecific[$language])) {
$charsArray[$language] = $languageSpecific[$language];
} else {
$charsArray[$language] = [];
}
return $charsArray[$language];
} }
/** /**
* Adds the specified amount of left and right padding to the given string. * Adds the specified amount of left and right padding to the given string.
* The default character used is a space. * The default character used is a space.
* *
* @param int $left Length of left padding * @param int $left Length of left padding
* @param int $right Length of right padding * @param int $right Length of right padding
* @param string $padStr String used to pad * @param string $padStr String used to pad
* @return static String with padding applied * @return static String with padding applied
*/ */
protected function applyPadding($left = 0, $right = 0, $padStr = ' ') protected function applyPadding($left = 0, $right = 0, $padStr = ' ')
@@ -1924,7 +1973,7 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
protected function supportsEncoding() protected function supportsEncoding()
{ {
$supported = array('UTF-8' => true, 'ASCII' => true); $supported = ['UTF-8' => true, 'ASCII' => true];
if (isset($supported[$this->encoding])) { if (isset($supported[$this->encoding])) {
return true; return true;

View File

@@ -15,25 +15,38 @@ class StaticStringyTestCase extends PHPUnit_Framework_TestCase
public function testEmptyArgsInvocation() public function testEmptyArgsInvocation()
{ {
$result = S::toLowerCase(); $result = S::toLowerCase();
$this->assertEquals('', (string) $result); $this->assertEquals('', $result);
} }
public function testInvocation() public function testInvocation()
{ {
$result = S::toLowerCase('FOOBAR'); $result = S::toLowerCase('FOOBAR');
$this->assertEquals('foobar', (string) $result); $this->assertEquals('foobar', $result);
$this->assertInternalType('string', $result);
} }
public function testPartialArgsInvocation() public function testPartialArgsInvocation()
{ {
$result = S::slice('foobar', 0, 3); $result = S::slice('foobar', 0, 3);
$this->assertEquals('foo', (string) $result); $this->assertEquals('foo', $result);
$this->assertInternalType('string', $result);
} }
public function testFullArgsInvocation() public function testFullArgsInvocation()
{ {
$result = S::slice('fòôbàř', 0, 3, 'UTF-8'); $result = S::slice('fòôbàř', 0, 3, 'UTF-8');
$this->assertEquals('fòô', (string) $result); $this->assertEquals('fòô', $result);
$this->assertInternalType('string', $result);
}
public function testArrayReturnValue()
{
$result = S::lines("a\nb");
$this->assertEquals(['a', 'b'], $result);
$this->assertInternalType('array', $result);
foreach ($result as $val) {
$this->assertInternalType('string', $val);
}
} }
/** /**

File diff suppressed because it is too large Load Diff