mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-30 08:20:12 +02:00
Compare commits
117 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
df24ab62d2 | ||
|
f9c2e0ae36 | ||
|
0c0b352009 | ||
|
f0a5c0ec80 | ||
|
8ef4bddd15 | ||
|
d9cee68330 | ||
|
2838f3412a | ||
|
50f0cb6356 | ||
|
d6dabeee67 | ||
|
44f492c025 | ||
|
2288363663 | ||
|
f22117862b | ||
|
eb13cc35d6 | ||
|
9b6bddcb20 | ||
|
67f16c9423 | ||
|
3708b6ba90 | ||
|
5c50a58bb8 | ||
|
8b59cd2233 | ||
|
63d81901ad | ||
|
a2624d4969 | ||
|
72102ae911 | ||
|
bf2a302aa1 | ||
|
5303203a72 | ||
|
e01ba7f848 | ||
|
f9f3b7f2f2 | ||
|
75b0cd2e0d | ||
|
b0976c72b7 | ||
|
edbda419cb | ||
|
e8afcffc7c | ||
|
986260239d | ||
|
841600867d | ||
|
845e8d693d | ||
|
b35270671c | ||
|
0aab4361bf | ||
|
df777da6ac | ||
|
ded4d41d44 | ||
|
569736327a | ||
|
6f2e2851ac | ||
|
553bf37acb | ||
|
916f79221e | ||
|
d71d4f0ad6 | ||
|
de93eab01f | ||
|
9b6404d5c3 | ||
|
27a4f11f2e | ||
|
46de78bda9 | ||
|
cb14fc2e6b | ||
|
090a15f2fd | ||
|
d80347e72a | ||
|
0491ed8db8 | ||
|
eb445b55ef | ||
|
91b153d30e | ||
|
9ebdf88b71 | ||
|
0607751e17 | ||
|
6507f0ac44 | ||
|
4e214a5195 | ||
|
fcc2969210 | ||
|
30d1742082 | ||
|
bd90918858 | ||
|
a2d1ec535a | ||
|
b953a85d2c | ||
|
e07a07a076 | ||
|
c4d785d29f | ||
|
5554477962 | ||
|
87c1a47355 | ||
|
6d558974ba | ||
|
5a9b987f9a | ||
|
e3d397fa74 | ||
|
ffc3fa5ff9 | ||
|
c3f25577ed | ||
|
5a4629662c | ||
|
823e50a340 | ||
|
9d77c49fca | ||
|
a074d2a2ac | ||
|
b7cfa84219 | ||
|
e3be6a56ea | ||
|
bdbd4d7541 | ||
|
8419369f3b | ||
|
a3dde38a2f | ||
|
59ce593086 | ||
|
54cf92c89c | ||
|
d320bf8704 | ||
|
2de4c4aee3 | ||
|
fc39b70953 | ||
|
9b61dcc771 | ||
|
b85a84d0b9 | ||
|
e5c3043d75 | ||
|
49a7405a2a | ||
|
63109ce85c | ||
|
b223a2c320 | ||
|
264eed18dd | ||
|
d990a41a1a | ||
|
318db789b1 | ||
|
f086c67ac7 | ||
|
ee59d586f2 | ||
|
3c7bc662a6 | ||
|
2072e667e9 | ||
|
b65da66ae8 | ||
|
604ddc0528 | ||
|
abd5505e98 | ||
|
ee4b89c2f8 | ||
|
ac04be8416 | ||
|
41336996a1 | ||
|
a8793ff711 | ||
|
183e529e15 | ||
|
4673754054 | ||
|
83671e721a | ||
|
c5f370c46d | ||
|
436d269622 | ||
|
c09de057a1 | ||
|
efb10020f6 | ||
|
ae74e66369 | ||
|
f6475e1288 | ||
|
ff36c79892 | ||
|
8f40b3495d | ||
|
e237f30d94 | ||
|
ad6d32080f | ||
|
7377aabf61 |
16
.editorconfig
Normal file
16
.editorconfig
Normal file
@@ -0,0 +1,16 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[*.yml]
|
||||
indent_style = space
|
||||
indent_size = 2
|
8
.gitattributes
vendored
Normal file
8
.gitattributes
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
* text=auto
|
||||
|
||||
.editorconfig export-ignore
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.travis.yml export-ignore
|
||||
phpunit.xml.dist export-ignore
|
||||
tests export-ignore
|
44
.travis.yml
44
.travis.yml
@@ -1,7 +1,43 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 5.5
|
||||
- 5.4
|
||||
- 5.3
|
||||
- hhvm
|
||||
|
||||
# This triggers builds to run on the new TravisCI infrastructure.
|
||||
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
- php: 5.4
|
||||
env: polyfill='true'
|
||||
- php: 5.4
|
||||
env: polyfill='false'
|
||||
- php: 5.5
|
||||
env: polyfill='true'
|
||||
- php: 5.5
|
||||
env: polyfill='false'
|
||||
- php: 5.6
|
||||
env: polyfill='true'
|
||||
- php: 5.6
|
||||
env: polyfill='false'
|
||||
- php: 7.0
|
||||
env: polyfill='true'
|
||||
- php: 7.0
|
||||
env: polyfill='false'
|
||||
- php: 7.1
|
||||
env: polyfill='true'
|
||||
- php: 7.1
|
||||
env: polyfill='false'
|
||||
|
||||
install: travis_retry composer install --no-interaction --prefer-source
|
||||
|
||||
before_script:
|
||||
- travis_retry composer self-update
|
||||
- travis_retry composer install --no-interaction --prefer-source
|
||||
- if [[ $polyfill = 'false' ]]; then travis_retry composer remove symfony/polyfill-mbstring; fi
|
||||
|
||||
script:
|
||||
- vendor/bin/phpunit
|
||||
|
||||
|
54
CHANGELOG.md
54
CHANGELOG.md
@@ -1,3 +1,57 @@
|
||||
### 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)
|
||||
|
||||
* Add startsWithAny
|
||||
* Add endsWithAny
|
||||
* Add stripWhitespace
|
||||
* Fix error handling for unsupported encodings
|
||||
* Change private methods to protected for extending class
|
||||
* Fix safeTruncate for strings without spaces
|
||||
* Additional char support in toAscii, e.g. full width chars and wide
|
||||
non-breaking space
|
||||
|
||||
### 2.3.2 (2016-05-02)
|
||||
|
||||
* Improve support without mbstring
|
||||
|
||||
### 2.3.1 (2016-03-21)
|
||||
|
||||
* Always use root namespace for mbstring functions
|
||||
|
||||
### 2.3.0 (2016-03-19)
|
||||
|
||||
* Add Persian characters in Stringy::charsArray()
|
||||
* Use symfony/polyfill-mbstring to avoid dependency on ext-mbstring
|
||||
|
||||
### 2.2.0 (2015-12-20)
|
||||
|
||||
* isJSON now returns false for empty strings
|
||||
* Update for German umlaut transformation
|
||||
* Use reflection to generate method list for StaticStringy
|
||||
* Added isBase64 method
|
||||
* Improved toAscii char coverage
|
||||
|
||||
### 2.1.0 (2015-09-02)
|
||||
|
||||
* Added simplified StaticStringy class
|
||||
* str in Stringy::create and constructor is now optional
|
||||
|
||||
### 2.0.0 (2015-07-29)
|
||||
|
||||
* Removed StaticStringy class
|
||||
|
333
README.md
333
README.md
@@ -1,99 +1,152 @@
|
||||

|
||||
|
||||
A PHP string manipulation library with multibyte support. Compatible with PHP
|
||||
5.3+ and HHVM. Refer to the [1.x branch](https://github.com/danielstjules/Stringy/tree/1.x)
|
||||
for older documentation.
|
||||
5.4+, PHP 7+, and HHVM.
|
||||
|
||||
``` php
|
||||
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.
|
||||
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
[](https://packagist.org/packages/danielstjules/stringy)
|
||||
[](https://packagist.org/packages/danielstjules/stringy)
|
||||
|
||||
* [Why?](#why)
|
||||
* [Installation](#installation)
|
||||
* [OO and Chaining](#oo-and-chaining)
|
||||
* [Implemented Interfaces](#implemented-interfaces)
|
||||
* [PHP 5.6 Creation](#php-56-creation)
|
||||
* [StaticStringy](#staticstringy)
|
||||
* [Class methods](#class-methods)
|
||||
* [create](#createmixed-str--encoding-)
|
||||
* [Instance methods](#instance-methods)
|
||||
* [append](#appendstring-string)
|
||||
* [at](#atint-index)
|
||||
* [between](#betweenstring-start-string-end--int-offset)
|
||||
* [camelize](#camelize)
|
||||
* [chars](#chars)
|
||||
* [collapseWhitespace](#collapsewhitespace)
|
||||
* [contains](#containsstring-needle--boolean-casesensitive--true-)
|
||||
* [containsAll](#containsallarray-needles--boolean-casesensitive--true-)
|
||||
* [containsAny](#containsanyarray-needles--boolean-casesensitive--true-)
|
||||
* [countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-)
|
||||
* [dasherize](#dasherize)
|
||||
* [delimit](#delimitint-delimiter)
|
||||
* [endsWith](#endswithstring-substring--boolean-casesensitive--true-)
|
||||
* [ensureLeft](#ensureleftstring-substring)
|
||||
* [ensureRight](#ensurerightstring-substring)
|
||||
* [first](#firstint-n)
|
||||
* [getEncoding](#getencoding)
|
||||
* [hasLowerCase](#haslowercase)
|
||||
* [hasUpperCase](#hasuppercase)
|
||||
* [htmlDecode](#htmldecode)
|
||||
* [htmlEncode](#htmlencode)
|
||||
* [humanize](#humanize)
|
||||
* [indexOf](#indexofstring-needle--offset--0-)
|
||||
* [indexOfLast](#indexoflaststring-needle--offset--0-)
|
||||
* [insert](#insertint-index-string-substring)
|
||||
* [isAlpha](#isalpha)
|
||||
* [isAlphanumeric](#isalphanumeric)
|
||||
* [isBlank](#isblank)
|
||||
* [isHexadecimal](#ishexadecimal)
|
||||
* [isJson](#isjson)
|
||||
* [isLowerCase](#islowercase)
|
||||
* [isSerialized](#isserialized)
|
||||
* [isUpperCase](#isuppercase)
|
||||
* [last](#last)
|
||||
* [length](#length)
|
||||
* [lines](#lines)
|
||||
* [longestCommonPrefix](#longestcommonprefixstring-otherstr)
|
||||
* [longestCommonSuffix](#longestcommonsuffixstring-otherstr)
|
||||
* [longestCommonSubstring](#longestcommonsubstringstring-otherstr)
|
||||
* [lowerCaseFirst](#lowercasefirst)
|
||||
* [pad](#padint-length--string-padstr-----string-padtype--right-)
|
||||
* [padBoth](#padbothint-length--string-padstr----)
|
||||
* [padLeft](#padleftint-length--string-padstr----)
|
||||
* [padRight](#padrightint-length--string-padstr----)
|
||||
* [prepend](#prependstring-string)
|
||||
* [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)
|
||||
* [removeLeft](#removeleftstring-substring)
|
||||
* [removeRight](#removerightstring-substring)
|
||||
* [repeat](#repeatmultiplier)
|
||||
* [replace](#replacestring-search-string-replacement)
|
||||
* [reverse](#reverse)
|
||||
* [safeTruncate](#safetruncateint-length--string-substring---)
|
||||
* [shuffle](#shuffle)
|
||||
* [slugify](#slugify-string-replacement----)
|
||||
* [startsWith](#startswithstring-substring--boolean-casesensitive--true-)
|
||||
* [slice](#sliceint-start--int-end-)
|
||||
* [split](#splitstring-pattern--int-limit-)
|
||||
* [substr](#substrint-start--int-length-)
|
||||
* [surround](#surroundstring-substring)
|
||||
* [swapCase](#swapcase)
|
||||
* [tidy](#tidy)
|
||||
* [titleize](#titleize-array-ignore)
|
||||
* [toAscii](#toascii)
|
||||
* [toBoolean](#toboolean)
|
||||
* [toLowerCase](#tolowercase)
|
||||
* [toSpaces](#tospaces-tablength--4-)
|
||||
* [toTabs](#totabs-tablength--4-)
|
||||
* [toTitleCase](#totitlecase)
|
||||
* [toUpperCase](#touppercase)
|
||||
* [trim](#trim-string-chars)
|
||||
* [trimLeft](#trimleft-string-chars)
|
||||
* [trimRight](#trimright-string-chars)
|
||||
* [truncate](#truncateint-length--string-substring---)
|
||||
* [underscored](#underscored)
|
||||
* [upperCamelize](#uppercamelize)
|
||||
* [upperCaseFirst](#uppercasefirst)
|
||||
<table>
|
||||
<tr>
|
||||
<td><a href="#appendstring-string">append</a></td>
|
||||
<td><a href="#atint-index">at</a></td>
|
||||
<td><a href="#betweenstring-start-string-end--int-offset">between</a></td>
|
||||
<td><a href="#camelize">camelize</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#chars">chars</a></td>
|
||||
<td><a href="#collapsewhitespace">collapseWhitespace</a></td>
|
||||
<td><a href="#containsstring-needle--boolean-casesensitive--true-">contains</a></td>
|
||||
<td><a href="#containsallarray-needles--boolean-casesensitive--true-">containsAll</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#containsanyarray-needles--boolean-casesensitive--true-">containsAny</a></td>
|
||||
<td><a href="#countsubstrstring-substring--boolean-casesensitive--true-">countSubstr</a></td>
|
||||
<td><a href="#dasherize">dasherize</a></td>
|
||||
<td><a href="#delimitint-delimiter">delimit</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#endswithstring-substring--boolean-casesensitive--true-">endsWith</a></td>
|
||||
<td><a href="#endswithanystring-substrings--boolean-casesensitive--true-">endsWithAny</a></td>
|
||||
<td><a href="#ensureleftstring-substring">ensureLeft</a></td>
|
||||
<td><a href="#ensurerightstring-substring">ensureRight</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#firstint-n">first</a></td>
|
||||
<td><a href="#getencoding">getEncoding</a></td>
|
||||
<td><a href="#haslowercase">hasLowerCase</a></td>
|
||||
<td><a href="#hasuppercase">hasUpperCase</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#htmldecode">htmlDecode</a></td>
|
||||
<td><a href="#htmlencode">htmlEncode</a></td>
|
||||
<td><a href="#humanize">humanize</a></td>
|
||||
<td><a href="#indexofstring-needle--offset--0-">indexOf</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#indexoflaststring-needle--offset--0-">indexOfLast</a></td>
|
||||
<td><a href="#insertint-index-string-substring">insert</a></td>
|
||||
<td><a href="#isalpha">isAlpha</a></td>
|
||||
<td><a href="#isalphanumeric">isAlphanumeric</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#isbase64">isBase64</a></td>
|
||||
<td><a href="#isblank">isBlank</a></td>
|
||||
<td><a href="#ishexadecimal">isHexadecimal</a></td>
|
||||
<td><a href="#isjson">isJson</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#islowercase">isLowerCase</a></td>
|
||||
<td><a href="#isserialized">isSerialized</a></td>
|
||||
<td><a href="#isuppercase">isUpperCase</a></td>
|
||||
<td><a href="#lastint-n">last</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#length">length</a></td>
|
||||
<td><a href="#lines">lines</a></td>
|
||||
<td><a href="#longestcommonprefixstring-otherstr">longestCommonPrefix</a></td>
|
||||
<td><a href="#longestcommonsuffixstring-otherstr">longestCommonSuffix</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#longestcommonsubstringstring-otherstr">longestCommonSubstring</a></td>
|
||||
<td><a href="#lowercasefirst">lowerCaseFirst</a></td>
|
||||
<td><a href="#padint-length--string-padstr-----string-padtype--right-">pad</a></td>
|
||||
<td><a href="#padbothint-length--string-padstr----">padBoth</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#padleftint-length--string-padstr----">padLeft</a></td>
|
||||
<td><a href="#padrightint-length--string-padstr----">padRight</a></td>
|
||||
<td><a href="#prependstring-string">prepend</a></td>
|
||||
<td><a href="#regexreplacestring-pattern-string-replacement--string-options--msr">regexReplace</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#removeleftstring-substring">removeLeft</a></td>
|
||||
<td><a href="#removerightstring-substring">removeRight</a></td>
|
||||
<td><a href="#repeatint-multiplier">repeat</a></td>
|
||||
<td><a href="#replacestring-search-string-replacement">replace</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#reverse">reverse</a></td>
|
||||
<td><a href="#safetruncateint-length--string-substring---">safeTruncate</a></td>
|
||||
<td><a href="#shuffle">shuffle</a></td>
|
||||
<td><a href="#slugify-string-replacement-----string-language--en">slugify</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#sliceint-start--int-end-">slice</a></td>
|
||||
<td><a href="#splitstring-pattern--int-limit-">split</a></td>
|
||||
<td><a href="#startswithstring-substring--boolean-casesensitive--true-">startsWith</a></td>
|
||||
<td><a href="#startswithanystring-substrings--boolean-casesensitive--true-">startsWithAny</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#stripwhitespace">stripWhitespace</a></td>
|
||||
<td><a href="#substrint-start--int-length-">substr</a></td>
|
||||
<td><a href="#surroundstring-substring">surround</a></td>
|
||||
<td><a href="#swapcase">swapCase</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#tidy">tidy</a></td>
|
||||
<td><a href="#titleize-array-ignore">titleize</a></td>
|
||||
<td><a href="#toascii-string-language--en--bool-removeunsupported--true-">toAscii</a></td>
|
||||
<td><a href="#toboolean">toBoolean</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#tolowercase">toLowerCase</a></td>
|
||||
<td><a href="#tospaces-tablength--4-">toSpaces</a></td>
|
||||
<td><a href="#totabs-tablength--4-">toTabs</a></td>
|
||||
<td><a href="#totitlecase">toTitleCase</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#touppercase">toUpperCase</a></td>
|
||||
<td><a href="#trim-string-chars">trim</a></td>
|
||||
<td><a href="#trimleft-string-chars">trimLeft</a></td>
|
||||
<td><a href="#trimright-string-chars">trimRight</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="#truncateint-length--string-substring---">truncate</a></td>
|
||||
<td><a href="#underscored">underscored</a></td>
|
||||
<td><a href="#uppercamelize">upperCamelize</a></td>
|
||||
<td><a href="#uppercasefirst">upperCaseFirst</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
* [Extensions](#extensions)
|
||||
* [Tests](#tests)
|
||||
* [License](#license)
|
||||
@@ -127,7 +180,7 @@ in your composer.json file:
|
||||
|
||||
```json
|
||||
"require": {
|
||||
"danielstjules/stringy": "~2.0"
|
||||
"danielstjules/stringy": "~3.1.0"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -150,11 +203,13 @@ And in either case, I'd suggest using an alias.
|
||||
use Stringy\Stringy as S;
|
||||
```
|
||||
|
||||
Please note that Stringy relies on the `mbstring` PHP module for its underlying
|
||||
multibyte support. This is a non-default, but very common module. For example,
|
||||
with debian and ubuntu, it's included in libapache2-mod-php5, php5-cli, and
|
||||
php5-fpm. For OSX users, it's a default for any version of PHP installed with
|
||||
homebrew. If compiling PHP from scratch, it can be included with the
|
||||
Please note that Stringy relies on the `mbstring` module for its underlying
|
||||
multibyte support. If the module is not found, Stringy will use
|
||||
[symfony/polyfill-mbstring](https://github.com/symfony/polyfill-mbstring).
|
||||
ex-mbstring is a non-default, but very common module. For example, with debian
|
||||
and ubuntu, it's included in libapache2-mod-php5, php5-cli, and php5-fpm. For
|
||||
OSX users, it's a default for any version of PHP installed with homebrew.
|
||||
If compiling PHP from scratch, it can be included with the
|
||||
`--enable-mbstring` flag.
|
||||
|
||||
## OO and Chaining
|
||||
@@ -223,6 +278,21 @@ use function Stringy\create as s;
|
||||
s('fòô bàř')->collapseWhitespace()->swapCase();
|
||||
```
|
||||
|
||||
## StaticStringy
|
||||
|
||||
All methods listed under "Instance methods" are available as part of a static
|
||||
wrapper. For StaticStringy methods, the optional encoding is expected to be the
|
||||
last argument. The return value is not cast, and may thus be of type Stringy,
|
||||
integer, boolean, etc.
|
||||
|
||||
```php
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
// Translates to Stringy::create('fòôbàř')->slice(0, 3);
|
||||
// Returns a Stringy object with the string "fòô"
|
||||
S::slice('fòôbàř', 0, 3);
|
||||
```
|
||||
|
||||
## Class methods
|
||||
|
||||
##### create(mixed $str [, $encoding ])
|
||||
@@ -234,7 +304,7 @@ 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àř'
|
||||
$stringy = S::create('fòôbàř'); // 'fòôbàř'
|
||||
```
|
||||
|
||||
## Instance Methods
|
||||
@@ -367,7 +437,17 @@ default, the comparison is case-sensitive, but can be made insensitive by
|
||||
setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
s('fòôbàř')->endsWith('bàř', true); // true
|
||||
s('fòôbàř')->endsWith('bàř'); // true
|
||||
```
|
||||
|
||||
##### endsWithAny(string[] $substrings [, boolean $caseSensitive = true ])
|
||||
|
||||
Returns true if the string ends 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àř')->endsWithAny(['bàř', 'baz']); // true
|
||||
```
|
||||
|
||||
##### ensureLeft(string $substring)
|
||||
@@ -380,7 +460,7 @@ s('foobar')->ensureLeft('http://'); // 'http://foobar'
|
||||
|
||||
##### ensureRight(string $substring)
|
||||
|
||||
Ensures that the string begins with $substring. If it doesn't, it's appended.
|
||||
Ensures that the string ends with $substring. If it doesn't, it's appended.
|
||||
|
||||
```php
|
||||
s('foobar')->ensureRight('.com'); // 'foobar.com'
|
||||
@@ -399,7 +479,7 @@ s('fòôbàř')->first(3); // 'fòô'
|
||||
Returns the encoding used by the Stringy object.
|
||||
|
||||
```php
|
||||
s('fòôbàř', 'UTF-8')->getEncoding(); // 'UTF-8'
|
||||
s('fòôbàř')->getEncoding(); // 'UTF-8'
|
||||
```
|
||||
|
||||
##### hasLowerCase()
|
||||
@@ -493,6 +573,14 @@ otherwise.
|
||||
s('دانيال1')->isAlphanumeric(); // true
|
||||
```
|
||||
|
||||
##### isBase64()
|
||||
|
||||
Returns true if the string is base64 encoded, false otherwise.
|
||||
|
||||
```php
|
||||
s('Zm9vYmFy')->isBase64(); // true
|
||||
```
|
||||
|
||||
##### isBlank()
|
||||
|
||||
Returns true if the string contains only whitespace chars, false otherwise.
|
||||
@@ -511,7 +599,9 @@ s('A102F')->isHexadecimal(); // true
|
||||
|
||||
##### isJson()
|
||||
|
||||
Returns true if the string is JSON, false otherwise.
|
||||
Returns true if the string is JSON, false otherwise. Unlike json_decode
|
||||
in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
|
||||
in that an empty string is not considered valid JSON.
|
||||
|
||||
```php
|
||||
s('{"foo":"bar"}')->isJson(); // true
|
||||
@@ -655,7 +745,8 @@ to a lack of support in the bundled version of Oniguruma in PHP < 5.6,
|
||||
and current versions of HHVM (3.8 and below).
|
||||
|
||||
```php
|
||||
s('fòô ')->regexReplace('f[òô]+\s', 'bàř', 'msr'); // 'bàř'
|
||||
s('fòô ')->regexReplace('f[òô]+\s', 'bàř'); // 'bàř'
|
||||
s('fò')->regexReplace('(ò)', '\\1ô'); // 'fòô'
|
||||
```
|
||||
|
||||
##### removeLeft(string $substring)
|
||||
@@ -719,28 +810,19 @@ random order.
|
||||
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
|
||||
characters with their closest ASCII equivalents, removing remaining
|
||||
non-ASCII and non-alphanumeric characters, and replacing whitespace with
|
||||
$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
|
||||
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
|
||||
```
|
||||
|
||||
##### slice(int $start [, int $end ])
|
||||
|
||||
Returns the substring beginning at $start, and up to, but not including
|
||||
@@ -762,6 +844,36 @@ results.
|
||||
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()
|
||||
|
||||
Strip all whitespace characters. This includes tabs and newline
|
||||
characters, as well as multibyte whitespace such as the thin space
|
||||
and ideographic space.
|
||||
|
||||
```php
|
||||
s(' Ο συγγραφέας ')->stripWhitespace(); // 'Οσυγγραφέας'
|
||||
```
|
||||
|
||||
##### substr(int $start [, int $length ])
|
||||
|
||||
Returns the substring beginning at $start with the specified $length.
|
||||
@@ -800,23 +912,28 @@ s('“I see…”')->tidy(); // '"I see..."'
|
||||
##### titleize([, array $ignore])
|
||||
|
||||
Returns a trimmed string with the first letter of each word capitalized.
|
||||
Ignores the case of other letters, preserving any acronyms. Also accepts
|
||||
an array, $ignore, allowing you to list words not to be capitalized.
|
||||
Also accepts an array, $ignore, allowing you to list words not to be
|
||||
capitalized.
|
||||
|
||||
```php
|
||||
$ignore = ['at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the'];
|
||||
s('If you optimize everything, you will always be unhappy.')->titleize($ignore);
|
||||
// 'I Like to Watch television'
|
||||
s('i like to watch television')->titleize($ignore);
|
||||
// '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
|
||||
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
|
||||
s('fòôbàř')->toAscii(); // 'foobar'
|
||||
s('äöü')->toAscii(); // 'aou'
|
||||
s('äöü')->toAscii('de'); // 'aeoeue'
|
||||
```
|
||||
|
||||
##### toBoolean()
|
||||
|
@@ -15,8 +15,8 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"ext-mbstring": "*"
|
||||
"php": ">=5.4.0",
|
||||
"symfony/polyfill-mbstring": "~1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.0"
|
||||
|
@@ -1,12 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit processIsolation="false"
|
||||
<phpunit bootstrap="vendor/autoload.php"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<file>tests/CommonTest.php</file>
|
||||
<file>tests/StringyTest.php</file>
|
||||
<file>tests/StaticStringyTest.php</file>
|
||||
<file phpVersion="5.6.0">tests/CreateTest.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
161
src/StaticStringy.php
Normal file
161
src/StaticStringy.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
use BadMethodCallException;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Class StaticStringy
|
||||
*
|
||||
* @method static string append(string $str, string $stringAppend, string $encoding = null)
|
||||
* @method static string at(string $str, int $index, string $encoding = null)
|
||||
* @method static string between(string $str, string $start, string $end, int $offset = 0, string $encoding = null)
|
||||
* @method static string camelize(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 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 containsAny(string $str, string[] $needle, bool $caseSensitive = true, 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 string dasherize(string $str, 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 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 ensureRight(string $str, string $substring, string $encoding = null)
|
||||
* @method static string first(string $str, int $n, string $encoding = null)
|
||||
* @method static bool hasLowerCase(string $str, string $encoding = null)
|
||||
* @method static bool hasUpperCase(string $str, string $encoding = null)
|
||||
* @method static string htmlDecode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
|
||||
* @method static string htmlEncode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
|
||||
* @method static string humanize(string $str, string $encoding = null)
|
||||
* @method static int indexOf(string $str, string $needle, int $offset = 0, string $encoding = null)
|
||||
* @method static int indexOfLast(string $str, string $needle, int $offset = 0, string $encoding = null)
|
||||
* @method static string insert(string $str, string $substring, int $index = 0, string $encoding = null)
|
||||
* @method static bool isAlpha(string $str, string $encoding = null)
|
||||
* @method static bool isAlphanumeric(string $str, string $encoding = null)
|
||||
* @method static bool isBase64(string $str, string $encoding = null)
|
||||
* @method static bool isBlank(string $str, string $encoding = null)
|
||||
* @method static bool isHexadecimal(string $str, string $encoding = null)
|
||||
* @method static bool isJson(string $str, string $encoding = null)
|
||||
* @method static bool isLowerCase(string $str, string $encoding = null)
|
||||
* @method static bool isSerialized(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 int length(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 longestCommonSuffix(string $str, string $otherStr, string $encoding = null)
|
||||
* @method static string longestCommonSubstring(string $str, string $otherStr, string $encoding = null)
|
||||
* @method static string lowerCaseFirst(string $str, string $encoding = null)
|
||||
* @method static string pad(string $str, int $length, string $padStr = ' ', string $padType = 'right', string $encoding = null)
|
||||
* @method static string padBoth(string $str, int $length, string $padStr = ' ', string $encoding = null)
|
||||
* @method static string padLeft(string $str, int $length, string $padStr = ' ', string $encoding = null)
|
||||
* @method static string padRight(string $str, int $length, string $padStr = ' ', string $encoding = null)
|
||||
* @method static string prepend(string $str, string $string, string $encoding = null)
|
||||
* @method static string regexReplace(string $str, string $pattern, string $replacement, string $options = 'msr', string $encoding = null)
|
||||
* @method static string removeLeft(string $str, string $substring, string $encoding = null)
|
||||
* @method static string removeRight(string $str, string $substring, string $encoding = null)
|
||||
* @method static string repeat(string $str, int $multiplier, string $encoding = null)
|
||||
* @method static string replace(string $str, string $search, string $replacement, string $encoding = null)
|
||||
* @method static string reverse(string $str, 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 slugify(string $str, string $replacement = '-', 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 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 surround(string $str, string $substring, string $encoding = null)
|
||||
* @method static string swapCase(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 toAscii(string $str, string $language = 'en', bool $removeUnsupported = true, string $encoding = null)
|
||||
* @method static bool toBoolean(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 toTabs(string $str, int $tabLength = 4, string $encoding = null)
|
||||
* @method static string toTitleCase(string $str, string $encoding = null)
|
||||
* @method static string toUpperCase(string $str, string $encoding = null)
|
||||
* @method static string trim(string $str, string $chars = null, string $encoding = null)
|
||||
* @method static string trimLeft(string $str, string $chars = null, string $encoding = null)
|
||||
* @method static string trimRight(string $str, string $chars = null, string $encoding = null)
|
||||
* @method static string truncate(string $str, int $length, string $substring = '', string $encoding = null)
|
||||
* @method static string underscored(string $str, string $encoding = null)
|
||||
* @method static string upperCamelize(string $str, string $encoding = null)
|
||||
* @method static string upperCaseFirst(string $str, string $encoding = null)
|
||||
*/
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* A mapping of method names to the numbers of arguments it accepts. Each
|
||||
* should be two more than the equivalent Stringy method. Necessary as
|
||||
* static methods place the optional $encoding as the last parameter.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $methodArgs = null;
|
||||
|
||||
/**
|
||||
* Creates an instance of Stringy and invokes the given method with the
|
||||
* rest of the passed arguments. The optional encoding is expected to be
|
||||
* the last argument. For example, the following:
|
||||
* StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
|
||||
* Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
|
||||
* The result is not cast, so the return value may be of type Stringy,
|
||||
* integer, boolean, etc.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed[] $arguments
|
||||
*
|
||||
* @return Stringy
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
if (!static::$methodArgs) {
|
||||
$stringyClass = new ReflectionClass('Stringy\Stringy');
|
||||
$methods = $stringyClass->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$params = $method->getNumberOfParameters() + 2;
|
||||
static::$methodArgs[$method->name] = $params;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset(static::$methodArgs[$name])) {
|
||||
throw new BadMethodCallException($name . ' is not a valid method');
|
||||
}
|
||||
|
||||
$numArgs = count($arguments);
|
||||
$str = ($numArgs) ? $arguments[0] : '';
|
||||
|
||||
if ($numArgs === static::$methodArgs[$name]) {
|
||||
$args = array_slice($arguments, 1, -1);
|
||||
$encoding = $arguments[$numArgs - 1];
|
||||
} else {
|
||||
$args = array_slice($arguments, 1);
|
||||
$encoding = null;
|
||||
}
|
||||
|
||||
$stringy = Stringy::create($str, $encoding);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
983
src/Stringy.php
983
src/Stringy.php
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/Create.php';
|
||||
|
||||
use function Stringy\create as s;
|
||||
|
||||
class CreateTestCase extends PHPUnit_Framework_TestCase
|
||||
|
73
tests/StaticStringyTest.php
Normal file
73
tests/StaticStringyTest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function testBadMethodCall()
|
||||
{
|
||||
$result = S::invalidMethod('foo');
|
||||
}
|
||||
|
||||
public function testEmptyArgsInvocation()
|
||||
{
|
||||
$result = S::toLowerCase();
|
||||
$this->assertEquals('', $result);
|
||||
}
|
||||
|
||||
public function testInvocation()
|
||||
{
|
||||
$result = S::toLowerCase('FOOBAR');
|
||||
$this->assertEquals('foobar', $result);
|
||||
$this->assertInternalType('string', $result);
|
||||
}
|
||||
|
||||
public function testPartialArgsInvocation()
|
||||
{
|
||||
$result = S::slice('foobar', 0, 3);
|
||||
$this->assertEquals('foo', $result);
|
||||
$this->assertInternalType('string', $result);
|
||||
}
|
||||
|
||||
public function testFullArgsInvocation()
|
||||
{
|
||||
$result = S::slice('fòôbàř', 0, 3, 'UTF-8');
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use reflection to ensure that all argument numbers are correct. Each
|
||||
* static method should accept 2 more arguments than their Stringy
|
||||
* equivalent.
|
||||
*/
|
||||
public function testArgumentNumbers()
|
||||
{
|
||||
$staticStringyClass = new ReflectionClass('Stringy\StaticStringy');
|
||||
$stringyClass = new ReflectionClass('Stringy\Stringy');
|
||||
|
||||
// getStaticPropertyValue can't access protected properties
|
||||
$properties = $staticStringyClass->getStaticProperties();
|
||||
|
||||
foreach ($properties['methodArgs'] as $method => $expected) {
|
||||
$num = $stringyClass->getMethod($method)
|
||||
->getNumberOfParameters() + 2;
|
||||
|
||||
$this->assertEquals($expected, $num,
|
||||
'Invalid num args for ' . $method);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user