mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-31 16:51:57 +02:00
Compare commits
184 Commits
1.0.0-rc.1
...
1.9.0
Author | SHA1 | Date | |
---|---|---|---|
|
3cf18e9e42 | ||
|
04b12c9500 | ||
|
b2386b6c33 | ||
|
1172a75d1e | ||
|
4ebf9aee3d | ||
|
61b986a3e6 | ||
|
2bd4be60bb | ||
|
0aa476d44c | ||
|
f63646dcba | ||
|
97e607dc5d | ||
|
a9a4ce461f | ||
|
9a0f0e16cb | ||
|
e9cd229808 | ||
|
4974407729 | ||
|
8a36b8d3f0 | ||
|
66c2a98e83 | ||
|
36c3fd05d2 | ||
|
f3bd5b6c62 | ||
|
dd34bc6f85 | ||
|
f490aa2894 | ||
|
40974608d4 | ||
|
8676bd9d9b | ||
|
f4327a2583 | ||
|
571dc93d1e | ||
|
62f6fd7b3a | ||
|
8b834482df | ||
|
08a72a72dd | ||
|
6a7b0391b2 | ||
|
d6fa00bb1c | ||
|
d1452263bd | ||
|
9da55d5d4f | ||
|
b515598227 | ||
|
28b65615d7 | ||
|
7b88b4f3da | ||
|
70e5b9c38b | ||
|
e0d50f0b8f | ||
|
c5365f256f | ||
|
401af084c7 | ||
|
773477cdd8 | ||
|
26444e4429 | ||
|
f3d32c79b0 | ||
|
98c1013623 | ||
|
91d8da4741 | ||
|
49cdd5b1e6 | ||
|
2fe42b46a1 | ||
|
9baa545f23 | ||
|
640fc5b26b | ||
|
d661047fb3 | ||
|
26fe399396 | ||
|
31c4ba145f | ||
|
5a1be9bf20 | ||
|
28c3db01c5 | ||
|
27ef5a8914 | ||
|
350d1e6f8e | ||
|
ce58c93fc2 | ||
|
d60422d197 | ||
|
3cc12c2ee7 | ||
|
8e16ad472d | ||
|
3f926c654a | ||
|
f8fc711ae1 | ||
|
628c467a67 | ||
|
24aa843639 | ||
|
bb6ea99c52 | ||
|
d1df244678 | ||
|
9a929d1860 | ||
|
583789b9e3 | ||
|
f00e3d5d80 | ||
|
c31e41f57a | ||
|
722f30d132 | ||
|
26a0fed52e | ||
|
1c0026ca00 | ||
|
da5ee7057d | ||
|
410ceac663 | ||
|
857650f278 | ||
|
bc11fce93e | ||
|
fc885d777d | ||
|
9d2863b983 | ||
|
037184e3d9 | ||
|
8ac1918386 | ||
|
7cb6709fd4 | ||
|
a663802c2c | ||
|
7e91d1ead2 | ||
|
abf3e38c1c | ||
|
5a5c39d02b | ||
|
c577684e0a | ||
|
681463de87 | ||
|
90de5cca00 | ||
|
a0b7615210 | ||
|
63ad4d238f | ||
|
ece219b609 | ||
|
e81dbaa6d9 | ||
|
2b2c03bd1f | ||
|
7600549abc | ||
|
8fe30d18b7 | ||
|
ae7527ad4a | ||
|
f74f535b78 | ||
|
5c5087db13 | ||
|
8d961e25d0 | ||
|
e46797a9f8 | ||
|
f0805f522d | ||
|
6103eca714 | ||
|
952ffd4c00 | ||
|
7832e22e09 | ||
|
bcd87c69b8 | ||
|
1d9001bcf0 | ||
|
ee88cd93e8 | ||
|
48f53e98fa | ||
|
6e7415a9f5 | ||
|
c62a5e8d4e | ||
|
cd41723b12 | ||
|
404909ffaa | ||
|
210c928b79 | ||
|
1965da2e39 | ||
|
25fa7340ed | ||
|
e5f66c6eca | ||
|
5d7e9a8d36 | ||
|
a7c5b8721c | ||
|
9399f7a694 | ||
|
b990df3c61 | ||
|
220d4f95c0 | ||
|
b3e922c517 | ||
|
edaf567a13 | ||
|
1d186ca79f | ||
|
ffe6c2575a | ||
|
3f5b6a2413 | ||
|
ebc58a51ef | ||
|
1c13f2ad7e | ||
|
7313dcc07e | ||
|
f14e8c16a4 | ||
|
9f930262af | ||
|
c230885a13 | ||
|
1f25d0c793 | ||
|
0e70d72352 | ||
|
d54628e6ae | ||
|
7335ee5c88 | ||
|
5e01d895db | ||
|
3458fd79fd | ||
|
6e5e5e055f | ||
|
88d5973701 | ||
|
75404a0338 | ||
|
bc96cdad0c | ||
|
6014ff52df | ||
|
6ba29637b2 | ||
|
2ada00ff62 | ||
|
2a197244a5 | ||
|
8b1a91fe0c | ||
|
906f217dc1 | ||
|
d4baab7583 | ||
|
0c8f0e9083 | ||
|
2155447ce8 | ||
|
77efce3a38 | ||
|
f6085ed2cb | ||
|
fa36291254 | ||
|
88145e83e8 | ||
|
9bd5a9c0c8 | ||
|
41ea0277b2 | ||
|
d862f9c24c | ||
|
61cd5f5f4d | ||
|
ea20aebc91 | ||
|
49e1c5ad2a | ||
|
90913d39bc | ||
|
02da00c433 | ||
|
07f23d6165 | ||
|
d27683b570 | ||
|
bc175f0e12 | ||
|
a6964e4561 | ||
|
5fff1a4124 | ||
|
545a5aec5f | ||
|
63298f36a8 | ||
|
a667c4aa6a | ||
|
cd5e897ac3 | ||
|
390671e131 | ||
|
0b76c56333 | ||
|
5f35226926 | ||
|
11f961cae6 | ||
|
660b1d6de8 | ||
|
3d81e2ef70 | ||
|
454e8e2f87 | ||
|
89c292c041 | ||
|
a1d9787309 | ||
|
f1abc38c17 | ||
|
7a64dad935 | ||
|
9d4ca96528 | ||
|
f0063f8bac |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
vendor/
|
||||
composer.lock
|
||||
.DS_Store
|
||||
|
@@ -1,5 +1,7 @@
|
||||
language: php
|
||||
php:
|
||||
- 5.6
|
||||
- 5.5
|
||||
- 5.4
|
||||
- 5.3
|
||||
- hhvm
|
||||
|
107
CHANGELOG.md
107
CHANGELOG.md
@@ -1,3 +1,110 @@
|
||||
### 1.9.0 (2015-02-09)
|
||||
|
||||
* Added hasUpperCase and hasLowerCase
|
||||
* Added $removeUnsupported parameter to toAscii()
|
||||
* Improved toAscii support with additional Unicode spaces, Vietnamese chars,
|
||||
and numerous other characters
|
||||
* Separated the charsArray from toAscii as a protected method that may be
|
||||
extended by inheriting classes
|
||||
* Chars array is cached for better performance
|
||||
|
||||
### 1.8.1 (2015-01-08)
|
||||
|
||||
* Optimized chars()
|
||||
* Added "ä Ä Ö Ü"" in toAscii()
|
||||
* Added support for Unicode spaces in toAscii()
|
||||
* Replaced instances of self::create() with static::create()
|
||||
* Added missing test cases for safeTruncate() and longestCommonSuffix()
|
||||
* Updated Stringy\create() to avoid collision when it already exists
|
||||
|
||||
### 1.8.0 (2015-01-03)
|
||||
|
||||
* Listed ext-mbstring in composer.json
|
||||
* Added Stringy\create function for PHP 5.6
|
||||
|
||||
### 1.7.0 (2014-10-14)
|
||||
|
||||
* Added containsAll and containsAny
|
||||
* Light cleanup
|
||||
|
||||
### 1.6.0 (2014-09-14)
|
||||
|
||||
* Added toTitleCase
|
||||
|
||||
### 1.5.2 (2014-07-09)
|
||||
|
||||
* Announced support for HHVM
|
||||
|
||||
### 1.5.1 (2014-04-19)
|
||||
|
||||
* Fixed toAscii() failing to remove remaining non-ascii characters
|
||||
* Updated slugify() to treat dash and underscore as delimiters by default
|
||||
* Updated slugify() to remove leading and trailing delimiter, if present
|
||||
|
||||
### 1.5.0 (2014-03-19)
|
||||
|
||||
* Made both str and encoding protected, giving property access to subclasses
|
||||
* Added getEncoding()
|
||||
* Fixed isJSON() giving false negatives
|
||||
* Cleaned up and simplified: replace(), collapseWhitespace(), underscored(),
|
||||
dasherize(), pad(), padLeft(), padRight() and padBoth()
|
||||
* Fixed handling consecutive invalid chars in slugify()
|
||||
* Removed conflicting hard sign transliteration in toAscii()
|
||||
|
||||
### 1.4.0 (2014-02-12)
|
||||
|
||||
* Implemented the IteratorAggregate interface, added chars()
|
||||
* Renamed count() to countSubstr()
|
||||
* Updated count() to implement Countable interface
|
||||
* Implemented the ArrayAccess interface with positive and negative indices
|
||||
* Switched from PSR-0 to PSR-4 autoloading
|
||||
|
||||
### 1.3.0 (2013-12-16)
|
||||
|
||||
* Additional Bulgarian support for toAscii
|
||||
* str property made private
|
||||
* Constructor casts first argument to string
|
||||
* Constructor throws an InvalidArgumentException when given an array
|
||||
* Constructor throws an InvalidArgumentException when given an object without
|
||||
a __toString method
|
||||
|
||||
### 1.2.2 (2013-12-04)
|
||||
|
||||
* Updated create function to use late static binding
|
||||
* Added optional $replacement param to slugify
|
||||
|
||||
### 1.2.1 (2013-10-11)
|
||||
|
||||
* Cleaned up tests
|
||||
* Added homepage to composer.json
|
||||
|
||||
### 1.2.0 (2013-09-15)
|
||||
|
||||
* Fixed pad's use of InvalidArgumentException
|
||||
* Fixed replace(). It now correctly treats regex special chars as normal chars
|
||||
* Added additional Cyrillic letters to toAscii
|
||||
* Added $caseSensitive to contains() and count()
|
||||
* Added toLowerCase()
|
||||
* Added toUpperCase()
|
||||
* Added regexReplace()
|
||||
|
||||
### 1.1.0 (2013-08-31)
|
||||
|
||||
* Fix for collapseWhitespace()
|
||||
* Added isHexadecimal()
|
||||
* Added constructor to Stringy\Stringy
|
||||
* Added isSerialized()
|
||||
* Added isJson()
|
||||
|
||||
### 1.0.0 (2013-08-1)
|
||||
|
||||
* 1.0.0 release
|
||||
* Added test coverage for Stringy::create and method chaining
|
||||
* Added tests for returned type
|
||||
* Fixed StaticStringy::replace(). It was returning a Stringy object instead of string
|
||||
* Renamed standardize() to the more appropriate toAscii()
|
||||
* Cleaned up comments and README
|
||||
|
||||
### 1.0.0-rc.1 (2013-07-28)
|
||||
|
||||
* Release candidate
|
||||
|
541
README.md
541
README.md
@@ -1,31 +1,42 @@
|
||||
# Stringy
|
||||

|
||||
|
||||
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
||||
A PHP string manipulation library with multibyte support. Offers both OO method
|
||||
chaining and a procedural-style static wrapper. Tested and compatible with
|
||||
PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
[](https://packagist.org/packages/danielstjules/stringy)
|
||||
[](https://packagist.org/packages/danielstjules/stringy)
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
|
||||
* [Requiring/Loading](#requiringloading)
|
||||
* [OO and Procedural](#oo-and-procedural)
|
||||
* [Implemented Interfaces](#implemented-interfaces)
|
||||
* [PHP 5.6 Creation](#php-56-creation)
|
||||
* [Methods](#methods)
|
||||
* [at](#at)
|
||||
* [camelize](#camelize)
|
||||
* [chars](#chars)
|
||||
* [collapseWhitespace](#collapsewhitespace)
|
||||
* [contains](#contains)
|
||||
* [count](#count)
|
||||
* [containsAll](#containsall)
|
||||
* [containsAny](#containsany)
|
||||
* [countSubstr](#countsubstr)
|
||||
* [create](#create)
|
||||
* [dasherize](#dasherize)
|
||||
* [endsWith](#endswith)
|
||||
* [ensureLeft](#ensureleft)
|
||||
* [ensureRight](#ensureright)
|
||||
* [first](#first)
|
||||
* [getEncoding](#getencoding)
|
||||
* [hasLowerCase](#haslowercase)
|
||||
* [hasUpperCase](#hasuppercase)
|
||||
* [humanize](#humanize)
|
||||
* [insert](#insert)
|
||||
* [isAlpha](#isalpha)
|
||||
* [isAlphanumeric](#isalphanumeric)
|
||||
* [isBlank](#isblank)
|
||||
* [isHexadecimal](#ishexadecimal)
|
||||
* [isJson](#isjson)
|
||||
* [isLowerCase](#islowercase)
|
||||
* [isSerialized](#isserialized)
|
||||
* [isUpperCase](#isuppercase)
|
||||
* [last](#last)
|
||||
* [length](#length)
|
||||
@@ -37,6 +48,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
||||
* [padBoth](#padboth)
|
||||
* [padLeft](#padleft)
|
||||
* [padRight](#padright)
|
||||
* [regexReplace](#regexreplace)
|
||||
* [removeLeft](#removeleft)
|
||||
* [removeRight](#removeright)
|
||||
* [replace](#replace)
|
||||
@@ -44,32 +56,42 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
||||
* [safeTruncate](#safetruncate)
|
||||
* [shuffle](#shuffle)
|
||||
* [slugify](#slugify)
|
||||
* [standardize](#standardize)
|
||||
* [startsWith](#startswith)
|
||||
* [substr](#substr)
|
||||
* [surround](#surround)
|
||||
* [swapCase](#swapcase)
|
||||
* [tidy](#tidy)
|
||||
* [titleize](#titleize)
|
||||
* [toAscii](#toascii)
|
||||
* [toLowerCase](#tolowercase)
|
||||
* [toSpaces](#tospaces)
|
||||
* [toTabs](#totabs)
|
||||
* [toTitleCase](#totitlecase)
|
||||
* [toUpperCase](#touppercase)
|
||||
* [trim](#trim)
|
||||
* [truncate](#truncate)
|
||||
* [underscored](#underscored)
|
||||
* [upperCamelize](#uppercamelize)
|
||||
* [upperCaseFirst](#uppercasefirst)
|
||||
* [Links](#links)
|
||||
* [Tests](#tests)
|
||||
* [License](#license)
|
||||
|
||||
## Requiring/Loading
|
||||
|
||||
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
|
||||
If you're using Composer to manage dependencies, you can include the following
|
||||
in your composer.json file:
|
||||
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"danielstjules/stringy": "dev-master"
|
||||
"danielstjules/stringy": "~1.9"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then, after running `composer update` or `php composer.phar update`, you can load the class using Composer's autoloading:
|
||||
Then, after running `composer update` or `php composer.phar update`, you can
|
||||
load the class using Composer's autoloading:
|
||||
|
||||
```php
|
||||
require 'vendor/autoload.php';
|
||||
@@ -78,9 +100,9 @@ require 'vendor/autoload.php';
|
||||
Otherwise, you can simply require the file directly:
|
||||
|
||||
```php
|
||||
require_once 'path/to/Stringy/src/Stringy/Stringy.php';
|
||||
require_once 'path/to/Stringy/src/Stringy.php';
|
||||
// or
|
||||
require_once 'path/to/Stringy/src/Stringy/StaticStringy.php';
|
||||
require_once 'path/to/Stringy/src/StaticStringy.php';
|
||||
```
|
||||
|
||||
And in either case, I'd suggest using an alias.
|
||||
@@ -99,35 +121,90 @@ of the former is the following:
|
||||
|
||||
```php
|
||||
use Stringy\Stringy as S;
|
||||
echo S::create("Fòô Bàř", 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
`Stringy\Stringy` contains a __toString() method, which returns the current
|
||||
string when the object is used in a string context. Its $str property is also
|
||||
public, and can be accessed directly if required, ie: `S::create('foo')->str // 'foo'`
|
||||
`Stringy\Stringy` has a __toString() method, which returns the current string
|
||||
when the object is used in a string context, ie:
|
||||
`(string) S::create('foo') // 'foo'`
|
||||
|
||||
Using the static wrapper, an alternative is the following:
|
||||
|
||||
```php
|
||||
use Stringy\StaticStringy as S;
|
||||
$string = S::collapseWhitespace("Fòô Bàř", 'UTF-8');
|
||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
|
||||
$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
|
||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
## Implemented Interfaces
|
||||
|
||||
`Stringy\Stringy` implements the `IteratorAggregate` interface, meaning that
|
||||
`foreach` can be used with an instance of the class:
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Fòô Bàř', 'UTF-8');
|
||||
foreach ($stringy as $char) {
|
||||
echo $char;
|
||||
}
|
||||
// 'Fòô Bàř'
|
||||
```
|
||||
|
||||
It implements the `Countable` interface, enabling the use of `count()` to
|
||||
retrieve the number of characters in the string:
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Fòô', 'UTF-8');
|
||||
count($stringy); // 3
|
||||
```
|
||||
|
||||
Furthermore, the `ArrayAccess` interface has been implemented. As a result,
|
||||
`isset()` can be used to check if a character at a specific index exists. And
|
||||
since `Stringy\Stringy` is immutable, any call to `offsetSet` or `offsetUnset`
|
||||
will throw an exception. `offsetGet` has been implemented, however, and accepts
|
||||
both positive and negative indexes. Invalid indexes result in an
|
||||
`OutOfBoundsException`.
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Bàř', 'UTF-8');
|
||||
echo $stringy[2]; // 'ř'
|
||||
echo $stringy[-2]; // 'à'
|
||||
isset($stringy[-4]); // false
|
||||
|
||||
$stringy[3]; // OutOfBoundsException
|
||||
$stringy[2] = 'a'; // Exception
|
||||
```
|
||||
|
||||
## PHP 5.6 Creation
|
||||
|
||||
As of PHP 5.6, [`use function`](https://wiki.php.net/rfc/use_function) is
|
||||
available for importing functions. Stringy exposes a namespaced function,
|
||||
`Stringy\create`, which emits the same behaviour as `Stringy\Stringy::create()`.
|
||||
If running PHP 5.6, or another runtime that supports the `use function` syntax,
|
||||
you can take advantage of an even simpler API as seen below:
|
||||
|
||||
``` php
|
||||
use function Stringy\create as s;
|
||||
|
||||
// Instead of: S::create('Fòô Bàř', 'UTF-8')
|
||||
s('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase();
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
||||
In the list below, any static method other than S::create refers to a
|
||||
method in Stringy\StaticStringy. For all others, they're found in Stringy\Stringy. Furthermore, all methods that return an object of type Stringy return a new object, and do not modify the original.
|
||||
In the list below, any static method other than S::create refers to a method in
|
||||
`Stringy\StaticStringy`. For all others, they're found in `Stringy\Stringy`.
|
||||
Furthermore, all methods that return a Stringy object or string do not modify
|
||||
the original. Stringy objects are immutable.
|
||||
|
||||
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*
|
||||
*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*
|
||||
|
||||
#### at
|
||||
|
||||
$stringy->at(int $index)
|
||||
|
||||
S::substr(int $index [, string $encoding ])
|
||||
S::at(int $index [, string $encoding ])
|
||||
|
||||
Gets the character of $str at $index, with indexes starting at 0.
|
||||
Returns the character at $index, with indexes starting at 0.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->at(6);
|
||||
@@ -140,23 +217,37 @@ $stringy->camelize();
|
||||
|
||||
S::camelize(string $str [, string $encoding ])
|
||||
|
||||
Returns a camelCase version of a supplied string, with multibyte support.
|
||||
Trims surrounding spaces, capitalizes letters following digits, spaces,
|
||||
dashes and underscores, and removes spaces, dashes, underscores.
|
||||
Returns a camelCase version of the string. Trims surrounding spaces,
|
||||
capitalizes letters following digits, spaces, dashes and underscores,
|
||||
and removes spaces, dashes, as well as underscores.
|
||||
|
||||
```php
|
||||
S::create('Camel-Case')->camelize();
|
||||
S::camelize('Camel-Case'); // 'camelCase'
|
||||
```
|
||||
|
||||
#### chars
|
||||
|
||||
$stringy->chars();
|
||||
|
||||
S::chars(string $str [, string $encoding ])
|
||||
|
||||
Returns an array consisting of the characters in the string.
|
||||
|
||||
```php
|
||||
S::create('Fòô Bàř', 'UTF-8')->chars();
|
||||
S::chars('Fòô Bàř', 'UTF-8'); // array(F', 'ò', 'ô', ' ', 'B', 'à', 'ř')
|
||||
```
|
||||
|
||||
#### collapseWhitespace
|
||||
|
||||
$stringy->collapseWhitespace()
|
||||
|
||||
S::collapseWhitespace(string $str)
|
||||
S::collapseWhitespace(string $str [, string $encoding ])
|
||||
|
||||
Trims the string and replaces consecutive whitespace characters with a
|
||||
single space. This inclues tabs and newline characters.
|
||||
single space. This includes tabs and newline characters, as well as
|
||||
multibyte whitespace such as the thin space and ideographic space.
|
||||
|
||||
```php
|
||||
S::create(' Ο συγγραφέας ')->collapseWhitespace();
|
||||
@@ -165,41 +256,76 @@ S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγρα
|
||||
|
||||
#### contains
|
||||
|
||||
$stringy->contains(string $needle)
|
||||
$stringy->contains(string $needle [, boolean $caseSensitive = true ])
|
||||
|
||||
S::contains(string $haystack, string $needle [, string $encoding ])
|
||||
S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns true if $haystack contains $needle, false otherwise.
|
||||
Returns true if the string contains $needle, false otherwise. By default,
|
||||
the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
|
||||
S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### count
|
||||
#### containsAll
|
||||
|
||||
$stringy->count(string $substring)
|
||||
$stringy->containsAll(array $needles [, boolean $caseSensitive = true ])
|
||||
|
||||
S::count(string $str, string $substring [, string $encoding ])
|
||||
S::containsAll(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns the number of occurences of $substring in $str. An alias for
|
||||
mb_substr_count()
|
||||
Returns true if the string contains all $needles, false otherwise. By
|
||||
default the comparison is case-sensitive, but can be made insensitive by
|
||||
setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α');
|
||||
S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
|
||||
S::create('Str contains foo and bar')->containsAll(array('foo', 'bar'));
|
||||
S::containsAll('Str contains foo and bar', array('foo', 'bar')); // true
|
||||
```
|
||||
|
||||
#### containsAny
|
||||
|
||||
$stringy->containsAny(array $needles [, boolean $caseSensitive = true ])
|
||||
|
||||
S::containsAny(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns true if the string contains any $needles, false otherwise. By
|
||||
default the comparison is case-sensitive, but can be made insensitive by
|
||||
setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('Str contains foo')->containsAny(array('foo', 'bar'));
|
||||
S::containsAny('Str contains foo', array('foo', 'bar')); // true
|
||||
```
|
||||
|
||||
#### countSubstr
|
||||
|
||||
$stringy->countSubstr(string $substring [, boolean $caseSensitive = true ])
|
||||
|
||||
S::countSubstr(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns the number of occurrences of $substring in the given string.
|
||||
By default, the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->countSubstr('α');
|
||||
S::countSubstr('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
|
||||
```
|
||||
|
||||
#### create
|
||||
|
||||
$stringy = S::create(string $str, [, $encoding ])
|
||||
S::create(mixed $str [, $encoding ])
|
||||
|
||||
Creates a Stringy object and assigns both str and encoding properties
|
||||
the supplied values. If $encoding is not specified, it defaults to
|
||||
mb_internal_encoding(). It then returns the instantiated object.
|
||||
the supplied values. $str is cast to a string prior to assignment, and if
|
||||
$encoding is not specified, it defaults to mb_internal_encoding(). It
|
||||
then returns the initialized object. Throws an InvalidArgumentException
|
||||
if the first argument is an array or object without a __toString method.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
|
||||
$stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
|
||||
```
|
||||
|
||||
#### dasherize
|
||||
@@ -208,10 +334,9 @@ $stringy->dasherize();
|
||||
|
||||
S::dasherize(string $str [, string $encoding ])
|
||||
|
||||
Returns a lowercase and trimmed string seperated by dashes, with
|
||||
multibyte support. Dashes are inserted before uppercase characters
|
||||
(with the exception of the first character of the string), and in place
|
||||
of spaces as well as underscores.
|
||||
Returns a lowercase and trimmed string separated by dashes. Dashes are
|
||||
inserted before uppercase characters (with the exception of the first
|
||||
character of the string), and in place of spaces as well as underscores.
|
||||
|
||||
```php
|
||||
S::create('TestDCase')->dasherize();
|
||||
@@ -224,9 +349,9 @@ $stringy->endsWith(string $substring [, boolean $caseSensitive = true ])
|
||||
|
||||
S::endsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns true if the string $str ends with $substring, false otherwise.
|
||||
By default, the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
Returns true if the string ends with $substring, false otherwise. By
|
||||
default, the comparison is case-sensitive, but can be made insensitive by
|
||||
setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('FÒÔ bàřs', 'UTF-8')->endsWith('àřs', true);
|
||||
@@ -239,7 +364,7 @@ $stringy->ensureLeft(string $substring)
|
||||
|
||||
S::ensureLeft(string $substring [, string $encoding ])
|
||||
|
||||
Ensures that $str begins with $substring.
|
||||
Ensures that the string begins with $substring. If it doesn't, it's prepended.
|
||||
|
||||
```php
|
||||
S::create('foobar')->ensureLeft('http://');
|
||||
@@ -252,7 +377,7 @@ $stringy->ensureRight(string $substring)
|
||||
|
||||
S::ensureRight(string $substring [, string $encoding ])
|
||||
|
||||
Ensures that $str ends with $substring.
|
||||
Ensures that the string begins with $substring. If it doesn't, it's appended.
|
||||
|
||||
```php
|
||||
S::create('foobar')->ensureRight('.com');
|
||||
@@ -265,21 +390,57 @@ $stringy->first(int $n)
|
||||
|
||||
S::first(int $n [, string $encoding ])
|
||||
|
||||
Gets the first $n characters of $str.
|
||||
Returns the first $n characters of the string.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->first(3);
|
||||
S::first('fòô bàř', 3, 'UTF-8'); // 'fòô'
|
||||
```
|
||||
|
||||
#### getEncoding
|
||||
|
||||
$stringy->getEncoding()
|
||||
|
||||
Returns the encoding used by the Stringy object.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->getEncoding(); // 'UTF-8'
|
||||
```
|
||||
|
||||
#### hasLowerCase
|
||||
|
||||
$stringy->hasLowerCase()
|
||||
|
||||
S::hasLowerCase(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string contains a lower case char, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->hasLowerCase();
|
||||
S::hasLowerCase('fòô bàř', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### hasUpperCase
|
||||
|
||||
$stringy->hasUpperCase()
|
||||
|
||||
S::hasUpperCase(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string contains an upper case char, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->hasUpperCase();
|
||||
S::hasUpperCase('fòô bàř', 'UTF-8'); // false
|
||||
```
|
||||
|
||||
#### humanize
|
||||
|
||||
$stringy->humanize()
|
||||
|
||||
S::humanize(string $str [, string $encoding ])
|
||||
|
||||
Capitalizes the first word of a string, replaces underscores with spaces,
|
||||
and strips '_id'.
|
||||
Capitalizes the first word of the string, replaces underscores with
|
||||
spaces, and strips '_id'.
|
||||
|
||||
```php
|
||||
S::create('author_id')->humanize();
|
||||
@@ -292,7 +453,7 @@ $stringy->insert(int $index, string $substring)
|
||||
|
||||
S::insert(string $str, int $index, string $substring [, string $encoding ])
|
||||
|
||||
Inserts $substring into $str at the $index provided.
|
||||
Inserts $substring into the string at the $index provided.
|
||||
|
||||
```php
|
||||
S::create('fòô bà', 'UTF-8')->insert('ř', 6);
|
||||
@@ -305,7 +466,7 @@ $stringy->isAlpha()
|
||||
|
||||
S::isAlpha(string $str [, string $encoding ])
|
||||
|
||||
Returns true if $str contains only alphabetic chars, false otherwise.
|
||||
Returns true if the string contains only alphabetic chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('丹尼爾', 'UTF-8')->isAlpha();
|
||||
@@ -318,7 +479,7 @@ $stringy->isAlphanumeric()
|
||||
|
||||
S::isAlphanumeric(string $str [, string $encoding ])
|
||||
|
||||
Returns true if $str contains only alphabetic and numeric chars, false
|
||||
Returns true if the string contains only alphabetic and numeric chars, false
|
||||
otherwise.
|
||||
|
||||
```php
|
||||
@@ -332,37 +493,76 @@ $stringy->isBlank()
|
||||
|
||||
S::isBlank(string $str [, string $encoding ])
|
||||
|
||||
Returns true if $str contains only whitespace chars, false otherwise.
|
||||
Returns true if the string contains only whitespace chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create("\n\t \v\f")->isBlank();
|
||||
S::isBlank("\n\t \v\f"); // true
|
||||
```
|
||||
|
||||
#### isHexadecimal
|
||||
|
||||
$stringy->isHexadecimal()
|
||||
|
||||
S::isHexadecimal(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('A102F')->isHexadecimal();
|
||||
S::isHexadecimal('A102F'); // true
|
||||
```
|
||||
|
||||
#### isJson
|
||||
|
||||
$stringy->isJson()
|
||||
|
||||
S::isJson(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string is JSON, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('{"foo":"bar"}')->isJson();
|
||||
S::isJson('{"foo":"bar"}'); // true
|
||||
```
|
||||
|
||||
#### isLowerCase
|
||||
|
||||
$stringy->isLowerCase()
|
||||
|
||||
S::isLowerCase(string $str [, string $encoding ])
|
||||
|
||||
Returns true if $str contains only lower case chars, false otherwise.
|
||||
Returns true if the string contains only lower case chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->isLowerCase();
|
||||
S::isLowerCase('fòô bàř', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### isSerialized
|
||||
|
||||
$stringy->isSerialized()
|
||||
|
||||
S::isSerialized(string $str [, string $encoding ])
|
||||
|
||||
Returns true if the string is serialized, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8')->isSerialized();
|
||||
S::isSerialized('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### isUpperCase
|
||||
|
||||
$stringy->isUpperCase()
|
||||
|
||||
S::isUpperCase(string $str [, string $encoding ])
|
||||
|
||||
Returns true if $str contains only upper case chars, false otherwise.
|
||||
Returns true if the string contains only upper case chars, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('FÒÔBÀŘ',, 'UTF-8')->isUpperCase();
|
||||
S::isUpperCase('FÒÔBÀŘ',, 'UTF-8'); // true
|
||||
S::create('FÒÔBÀŘ', 'UTF-8')->isUpperCase();
|
||||
S::isUpperCase('FÒÔBÀŘ', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### last
|
||||
@@ -371,7 +571,7 @@ $stringy->last(int $n)
|
||||
|
||||
S::last(int $n [, string $encoding ])
|
||||
|
||||
Gets the last $n characters of $str.
|
||||
Returns the last $n characters of the string.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->last(3);
|
||||
@@ -384,7 +584,7 @@ $stringy->length()
|
||||
|
||||
S::length(string $str [, string $encoding ])
|
||||
|
||||
Returns the length of $str. An alias for PHP's mb_strlen() function.
|
||||
Returns the length of the string. An alias for PHP's mb_strlen() function.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->length();
|
||||
@@ -397,7 +597,7 @@ $stringy->longestCommonPrefix(string $otherStr)
|
||||
|
||||
S::longestCommonPrefix(string $str, string $otherStr [, $encoding ])
|
||||
|
||||
Finds the longest common prefix between $str and $otherStr.
|
||||
Returns the longest common prefix between the string and $otherStr.
|
||||
|
||||
```php
|
||||
S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar');
|
||||
@@ -410,7 +610,7 @@ $stringy->longestCommonSuffix(string $otherStr)
|
||||
|
||||
S::longestCommonSuffix(string $str, string $otherStr [, $encoding ])
|
||||
|
||||
Finds the longest common suffix between $str and $otherStr.
|
||||
Returns the longest common suffix between the string and $otherStr.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř');
|
||||
@@ -423,8 +623,8 @@ $stringy->longestCommonSubstring(string $otherStr)
|
||||
|
||||
S::longestCommonSubstring(string $str, string $otherStr [, $encoding ])
|
||||
|
||||
Finds the longest common substring between $str and $otherStr. In the
|
||||
case of ties, returns that which occurs first.
|
||||
Returns the longest common substring between the string and $otherStr. In the
|
||||
case of ties, it returns that which occurs first.
|
||||
|
||||
```php
|
||||
S::create('foo bar')->longestCommonSubstring('boo far');
|
||||
@@ -437,8 +637,7 @@ $stringy->lowerCaseFirst();
|
||||
|
||||
S::lowerCaseFirst(string $str [, string $encoding ])
|
||||
|
||||
Converts the first character of the supplied string to lower case, with
|
||||
support for multibyte strings.
|
||||
Converts the first character of the supplied string to lower case.
|
||||
|
||||
```php
|
||||
S::create('Σ test', 'UTF-8')->lowerCaseFirst();
|
||||
@@ -451,14 +650,14 @@ $stringy->pad(int $length [, string $padStr = ' ' [, string $padType = 'right' ]
|
||||
|
||||
S::pad(string $str , int $length [, string $padStr = ' ' [, string $padType = 'right' [, string $encoding ]]])
|
||||
|
||||
Pads a string to a given length with another string. If length is less
|
||||
than or equal to the length of $str, then no padding takes places. The
|
||||
default string used for padding is a space, and the default type (one of
|
||||
'left', 'right', 'both') is 'right'. Throws an exception if $padType
|
||||
isn't one of those 3 values.
|
||||
Pads the string to a given length with $padStr. If length is less than
|
||||
or equal to the length of the string, no padding takes places. The default
|
||||
string used for padding is a space, and the default type (one of 'left',
|
||||
'right', 'both') is 'right'. Throws an InvalidArgumentException if
|
||||
$padType isn't one of those 3 values.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
|
||||
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left');
|
||||
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
|
||||
```
|
||||
|
||||
@@ -469,7 +668,7 @@ $stringy->padBoth(int $length [, string $padStr = ' ' ])
|
||||
S::padBoth(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
|
||||
|
||||
Returns a new string of a given length such that both sides of the string
|
||||
string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
string are padded. Alias for pad() with a $padType of 'both'.
|
||||
|
||||
```php
|
||||
S::create('foo bar')->padBoth(9, ' ');
|
||||
@@ -483,7 +682,7 @@ $stringy->padLeft(int $length [, string $padStr = ' ' ])
|
||||
S::padLeft(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
|
||||
|
||||
Returns a new string of a given length such that the beginning of the
|
||||
string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
|
||||
string is padded. Alias for pad() with a $padType of 'left'.
|
||||
|
||||
```php
|
||||
S::create($str, $encoding)->padLeft($length, $padStr);
|
||||
@@ -497,20 +696,36 @@ $stringy->padRight(int $length [, string $padStr = ' ' ])
|
||||
S::padRight(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
|
||||
|
||||
Returns a new string of a given length such that the end of the string is
|
||||
padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
|
||||
padded. Alias for pad() with a $padType of 'right'.
|
||||
|
||||
```php
|
||||
S::create('foo bar')->padRight(10, '_*');
|
||||
S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
|
||||
```
|
||||
|
||||
#### regexReplace
|
||||
|
||||
$stringy->regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
|
||||
|
||||
S::regexReplace(string $str, string $pattern, string $replacement [, string $options = 'msr' [, string $encoding ]])
|
||||
|
||||
Replaces all occurrences of $pattern in $str by $replacement. An alias
|
||||
for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
|
||||
in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
|
||||
in the bundled version of Oniguruma in PHP 5.3.
|
||||
|
||||
```php
|
||||
S::create('fòô ', 'UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
|
||||
S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
|
||||
```
|
||||
|
||||
#### removeLeft
|
||||
|
||||
$stringy->removeLeft(string $substring)
|
||||
|
||||
S::removeLeft(string $str, string $substring [, string $encoding ])
|
||||
|
||||
Removes the prefix $substring if present.
|
||||
Returns a new string with the prefix $substring removed, if present.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
|
||||
@@ -523,7 +738,7 @@ $stringy->removeRight(string $substring)
|
||||
|
||||
S::removeRight(string $str, string $substring [, string $encoding ])
|
||||
|
||||
Removes the suffix $substring if present.
|
||||
Returns a new string with the suffix $substring removed, if present.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
|
||||
@@ -532,11 +747,11 @@ S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
|
||||
|
||||
#### replace
|
||||
|
||||
$stringy->replace(string $search, string $replace)
|
||||
$stringy->replace(string $search, string $replacement)
|
||||
|
||||
S::replace(string $str, string $search, string $replace [, string $encoding ])
|
||||
S::replace(string $str, string $search, string $replacement [, string $encoding ])
|
||||
|
||||
Replaces all occurrences of $search with $replace in $str.
|
||||
Replaces all occurrences of $search in $str by $replacement.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
|
||||
@@ -547,9 +762,9 @@ S::replace('fòô bàř fòô bàř', 'fòô ', '', 'UTF-8'); // 'bàř bàř'
|
||||
|
||||
$stringy->reverse()
|
||||
|
||||
S::reverse(string $str, [, string $encoding ])
|
||||
S::reverse(string $str [, string $encoding ])
|
||||
|
||||
Reverses a string. A multibyte version of strrev.
|
||||
Returns a reversed string. A multibyte version of strrev().
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->reverse();
|
||||
@@ -558,13 +773,13 @@ S::reverse('fòô bàř', 'UTF-8'); // 'řàb ôòf'
|
||||
|
||||
#### safeTruncate
|
||||
|
||||
$stringy->safeTruncate(int $length, [, string $substring = '' ])
|
||||
$stringy->safeTruncate(int $length [, string $substring = '' ])
|
||||
|
||||
S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||
S::safeTruncate(string $str, int $length [, string $substring = '' [, string $encoding ]])
|
||||
|
||||
Truncates the string to a given length, while ensuring that it does not
|
||||
chop words. If $substring is provided, and truncating occurs, the string
|
||||
is further truncated so that the substring may be appended without
|
||||
split words. If $substring is provided, and truncating occurs, the
|
||||
string is further truncated so that the substring may be appended without
|
||||
exceeding the desired length.
|
||||
|
||||
```php
|
||||
@@ -578,8 +793,8 @@ $stringy->shuffle()
|
||||
|
||||
S::shuffle(string $str [, string $encoding ])
|
||||
|
||||
A multibyte str_shuffle function. It randomizes the order of characters
|
||||
in a string.
|
||||
A multibyte str_shuffle() function. It returns a string with its characters in
|
||||
random order.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->shuffle();
|
||||
@@ -588,40 +803,28 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
|
||||
|
||||
#### slugify
|
||||
|
||||
$stringy->slugify()
|
||||
$stringy->slugify([ string $replacement = '-' ])
|
||||
|
||||
S::slugify(string $str)
|
||||
S::slugify(string $str [, string $replacement = '-' ])
|
||||
|
||||
Converts the supplied text into an URL slug. This includes replacing
|
||||
non-ASCII characters with their closest ASCII equivalents, removing
|
||||
non-alphanumeric and non-ASCII characters, and replacing whitespace with
|
||||
dashes. The string is also converted to lowercase.
|
||||
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.
|
||||
|
||||
```php
|
||||
S::create('Using strings like fòô bàř')->slugify();
|
||||
S::slugify('Using strings like fòô bàř'); // 'using-strings-like-foo-bar'
|
||||
```
|
||||
|
||||
#### standardize
|
||||
|
||||
$stringy->standardize()
|
||||
|
||||
S::standardize(string $str)
|
||||
|
||||
Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř')->standardize();
|
||||
S::standardize('fòô bàř'); // 'foo bar'
|
||||
```
|
||||
|
||||
#### startsWith
|
||||
|
||||
$stringy->startsWith(string $substring [, boolean $caseSensitive = true ])
|
||||
|
||||
S::startsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns true if the string $str begins with $substring, false otherwise.
|
||||
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.
|
||||
|
||||
@@ -636,7 +839,7 @@ $stringy->substr(int $start [, int $length ])
|
||||
|
||||
S::substr(string $str, int $start [, int $length [, string $encoding ]])
|
||||
|
||||
Gets the substring of $str beginning at $start with the specified $length.
|
||||
Returns the substring beginning at $start with the specified $length.
|
||||
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.
|
||||
|
||||
@@ -664,7 +867,7 @@ $stringy->swapCase();
|
||||
|
||||
S::swapCase(string $str [, string $encoding ])
|
||||
|
||||
Returns a case swapped version of a string.
|
||||
Returns a case swapped version of the string.
|
||||
|
||||
```php
|
||||
S::create('Ντανιλ', 'UTF-8')->swapCase();
|
||||
@@ -677,8 +880,8 @@ $stringy->tidy()
|
||||
|
||||
S::tidy(string $str)
|
||||
|
||||
Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||
(and commonly used in Word documents) with their ASCII equivalents.
|
||||
Returns a string with smart quotes, ellipsis characters, and dashes from
|
||||
Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
|
||||
|
||||
```php
|
||||
S::create('“I see…”')->tidy();
|
||||
@@ -691,10 +894,9 @@ $stringy->titleize([ string $encoding ])
|
||||
|
||||
S::titleize(string $str [, array $ignore [, string $encoding ]])
|
||||
|
||||
Capitalizes the first letter of each word in a string, after trimming.
|
||||
Ignores the case of other letters, allowing for the use of acronyms.
|
||||
Also accepts an array, $ignore, allowing you to list words not to be
|
||||
capitalized.
|
||||
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.
|
||||
|
||||
```php
|
||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||
@@ -703,13 +905,42 @@ S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
|
||||
// 'I Like to Watch DVDs at Home'
|
||||
```
|
||||
|
||||
#### toAscii
|
||||
|
||||
$stringy->toAscii()
|
||||
|
||||
S::toAscii(string $str [, boolean $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.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř')->toAscii();
|
||||
S::toAscii('fòô bàř'); // 'foo bar'
|
||||
```
|
||||
|
||||
#### toLowerCase
|
||||
|
||||
$stringy->toLowerCase()
|
||||
|
||||
S::toLowerCase(string $str [, string $encoding ])
|
||||
|
||||
Converts all characters in the string to lowercase. An alias for PHP's
|
||||
mb_strtolower().
|
||||
|
||||
```php
|
||||
S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase();
|
||||
S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř'
|
||||
```
|
||||
|
||||
#### toSpaces
|
||||
|
||||
$stringy->toSpaces([ tabLength = 4 ])
|
||||
|
||||
S::toSpaces(string $str, [, int $tabLength = 4 ])
|
||||
S::toSpaces(string $str [, int $tabLength = 4 ])
|
||||
|
||||
Converts each tab in a 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.
|
||||
|
||||
```php
|
||||
@@ -721,9 +952,9 @@ S::toSpaces(' String speech = "Hi"'); // ' String speech = "Hi"'
|
||||
|
||||
$stringy->toTabs([ tabLength = 4 ])
|
||||
|
||||
S::toTabs(string $str, [, int $tabLength = 4 ])
|
||||
S::toTabs(string $str [, int $tabLength = 4 ])
|
||||
|
||||
Converts each occurence of some consecutive number of spaces, as defined
|
||||
Converts each occurrence of some consecutive number of spaces, as defined
|
||||
by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
||||
converted to a tab.
|
||||
|
||||
@@ -732,13 +963,40 @@ S::create(' fòô bàř')->toTabs();
|
||||
S::toTabs(' fòô bàř'); // ' fòô bàř'
|
||||
```
|
||||
|
||||
#### toTitleCase
|
||||
|
||||
$stringy->toTitleCase()
|
||||
|
||||
S::toTitleCase(string $str [, string $encoding ])
|
||||
|
||||
Converts the first character of each word in the string to uppercase.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->toTitleCase();
|
||||
S::toTitleCase('fòô bàř', 'UTF-8'); // 'Fòô Bàř'
|
||||
```
|
||||
|
||||
#### toUpperCase
|
||||
|
||||
$stringy->toUpperCase()
|
||||
|
||||
S::toUpperCase(string $str [, string $encoding ])
|
||||
|
||||
Converts all characters in the string to uppercase. An alias for PHP's
|
||||
mb_strtoupper().
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->toUpperCase();
|
||||
S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ'
|
||||
```
|
||||
|
||||
#### trim
|
||||
|
||||
$stringy->trim()
|
||||
|
||||
S::trim(string $str)
|
||||
|
||||
Trims $str. An alias for PHP's trim() function.
|
||||
Returns the trimmed string. An alias for PHP's trim() function.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->trim();
|
||||
@@ -747,17 +1005,17 @@ S::trim(' fòô bàř '); // 'fòô bàř'
|
||||
|
||||
#### truncate
|
||||
|
||||
$stringy->truncate(int $length, [, string $substring = '' ])
|
||||
$stringy->truncate(int $length [, string $substring = '' ])
|
||||
|
||||
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||
S::truncate(string $str, int $length [, string $substring = '' [, string $encoding ]])
|
||||
|
||||
Truncates $str to a given length. If $substring is provided, and
|
||||
Truncates the string to a given length. If $substring is provided, and
|
||||
truncating occurs, the string is further truncated so that the substring
|
||||
may be appended without exceeding the desired length.
|
||||
|
||||
```php
|
||||
S::create('What are your plans today?')->safeTruncate(19, '...');
|
||||
S::safeTruncate('What are your plans today?', 19, '...'); // 'What are your pl...'
|
||||
S::create('What are your plans today?')->truncate(19, '...');
|
||||
S::truncate('What are your plans today?', 19, '...'); // 'What are your pl...'
|
||||
```
|
||||
|
||||
#### underscored
|
||||
@@ -766,10 +1024,9 @@ $stringy->underscored();
|
||||
|
||||
S::underscored(string $str [, string $encoding ])
|
||||
|
||||
Returns a lowercase and trimmed string seperated by underscores, with
|
||||
multibyte support. Underscores are inserted before uppercase characters
|
||||
(with the exception of the first character of the string), and in place
|
||||
of spaces as well as dashes.
|
||||
Returns a lowercase and trimmed string separated by underscores.
|
||||
Underscores are inserted before uppercase characters (with the exception
|
||||
of the first character of the string), and in place of spaces as well as dashes.
|
||||
|
||||
```php
|
||||
S::create('TestUCase')->underscored();
|
||||
@@ -782,9 +1039,9 @@ $stringy->upperCamelize();
|
||||
|
||||
S::upperCamelize(string $str [, string $encoding ])
|
||||
|
||||
Returns an UpperCamelCase version of a supplied string, with multibyte
|
||||
support. Trims surrounding spaces, capitalizes letters following digits,
|
||||
spaces, dashes and underscores, and removes spaces, dashes, underscores.
|
||||
Returns an UpperCamelCase version of the supplied string. It trims
|
||||
surrounding spaces, capitalizes letters following digits, spaces, dashes
|
||||
and underscores, and removes spaces, dashes, underscores.
|
||||
|
||||
```php
|
||||
S::create('Upper Camel-Case')->upperCamelize();
|
||||
@@ -797,14 +1054,20 @@ $stringy->upperCaseFirst();
|
||||
|
||||
S::upperCaseFirst(string $str [, string $encoding ])
|
||||
|
||||
Converts the first character of the supplied string to upper case, with
|
||||
support for multibyte strings.
|
||||
Converts the first character of the supplied string to upper case.
|
||||
|
||||
```php
|
||||
S::create('σ test', 'UTF-8')->upperCaseFirst();
|
||||
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
||||
```
|
||||
|
||||
## Links
|
||||
|
||||
The following is a list of libraries that extend Stringy:
|
||||
|
||||
* [SliceableStringy](https://github.com/danielstjules/SliceableStringy):
|
||||
Python-like string slices in PHP
|
||||
|
||||
## Tests
|
||||
|
||||
From the project directory, tests can be ran using `phpunit`
|
||||
|
@@ -5,6 +5,7 @@
|
||||
"multibyte", "string", "manipulation", "utility", "methods", "utf-8",
|
||||
"helpers", "utils", "utf"
|
||||
],
|
||||
"homepage": "https://github.com/danielstjules/Stringy",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@@ -14,9 +15,21 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
"php": ">=5.3.0",
|
||||
"ext-mbstring": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.0"
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/danielstjules/Stringy/issues",
|
||||
"source": "https://github.com/danielstjules/Stringy"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "Stringy": "src/" }
|
||||
"psr-4": { "Stringy\\": "src/" },
|
||||
"files": ["src/Create.php"]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"classmap": [ "tests" ]
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,10 @@
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<directory>tests/Stringy</directory>
|
||||
<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>
|
||||
</phpunit>
|
||||
|
19
src/Create.php
Normal file
19
src/Create.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
if (!function_exists('Stringy\create')) {
|
||||
/**
|
||||
* Creates a Stringy object and returns it on success.
|
||||
*
|
||||
* @param mixed $str Value to modify, after being cast to string
|
||||
* @param string $encoding The character encoding
|
||||
* @return Stringy A Stringy object
|
||||
* @throws \InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
*/
|
||||
function create($str, $encoding = null)
|
||||
{
|
||||
return new Stringy($str, $encoding);
|
||||
}
|
||||
}
|
869
src/StaticStringy.php
Normal file
869
src/StaticStringy.php
Normal file
@@ -0,0 +1,869 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* Returns an array consisting of the characters in the string.
|
||||
*
|
||||
* @param string $str String for which to return the chars
|
||||
* @param string $encoding The character encoding
|
||||
* @return array An array of string chars
|
||||
*/
|
||||
public static function chars($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->chars();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->upperCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->lowerCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of the string. Trims surrounding spaces,
|
||||
* capitalizes letters following digits, spaces, dashes and underscores,
|
||||
* and removes spaces, dashes, as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->camelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an UpperCamelCase version of the supplied string. It trims
|
||||
* surrounding spaces, capitalizes letters following digits, spaces, dashes
|
||||
* and underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to UpperCamelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->upperCamelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string separated by dashes. Dashes are
|
||||
* inserted before uppercase characters (with the exception of the first
|
||||
* character of the string), and in place of spaces as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->dasherize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string separated by underscores.
|
||||
* Underscores are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces as well as
|
||||
* dashes.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->underscored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of the string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->swapCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->titleize($ignore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first word of the string, replaces underscores with
|
||||
* spaces, and strips '_id'.
|
||||
*
|
||||
* @param string $str String to humanize
|
||||
* @param string $encoding The character encoding
|
||||
* @return string A humanized string
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->humanize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with smart quotes, ellipsis characters, and dashes from
|
||||
* Windows-1252 (commonly used in Word documents) replaced by their ASCII
|
||||
* equivalents.
|
||||
*
|
||||
* @param string $str String to remove special chars
|
||||
* @return string String with those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
{
|
||||
return (string) Stringy::create($str)->tidy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This includes tabs and newline characters, as well as
|
||||
* multibyte whitespace such as the thin space and ideographic space.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
*/
|
||||
public static function collapseWhitespace($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->collapseWhitespace();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str A string with non-ASCII characters
|
||||
* @param bool $removeUnsupported Whether or not to remove the
|
||||
* unsupported characters
|
||||
* @return string A string containing only ASCII characters
|
||||
*/
|
||||
public static function toAscii($str, $removeUnsupported = true)
|
||||
{
|
||||
return (string) Stringy::create($str)->toAscii($removeUnsupported);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads the string to a given length with $padStr. If length is less than
|
||||
* or equal to the length of the string, no padding takes places. The
|
||||
* default string used for padding is a space, and the default type (one of
|
||||
* 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
|
||||
* if $padType isn't one of those 3 values.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @throws \InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->pad($length, $padStr, $padType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padLeft($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padRight($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padBoth($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to check the start of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->startsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string ends with $substring, false otherwise. By
|
||||
* default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the end of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->endsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert tabs to spaces
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
{
|
||||
return (string) Stringy::create($str)->toSpaces($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each occurrence of some consecutive number of spaces, as
|
||||
* defined by $tabLength, to a tab. By default, each 4 consecutive spaces
|
||||
* are converted to a tab.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
{
|
||||
return (string) Stringy::create($str)->toTabs($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all characters in the string to lowercase. An alias for PHP's
|
||||
* mb_strtolower().
|
||||
*
|
||||
* @param string $str String to convert case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The lowercase string
|
||||
*/
|
||||
public static function toLowerCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of each word in the string to uppercase.
|
||||
*
|
||||
* @param string $str String to convert case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The title-cased string
|
||||
*/
|
||||
public static function toTitleCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->toTitleCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all characters in the string to uppercase. An alias for PHP's
|
||||
* mb_strtoupper().
|
||||
*
|
||||
* @param string $str String to convert case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The uppercase string
|
||||
*/
|
||||
public static function toUpperCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the string into an URL slug. This includes replacing non-ASCII
|
||||
* characters with their closest ASCII equivalents, removing 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.
|
||||
*
|
||||
* @param string $str Text to transform into an URL slug
|
||||
* @param string $replacement The string used to replace whitespace
|
||||
* @return string The corresponding URL slug
|
||||
*/
|
||||
public static function slugify($str, $replacement = '-')
|
||||
{
|
||||
return (string) Stringy::create($str)->slugify($replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains $needle, false otherwise. By default,
|
||||
* the comparison is case-sensitive, but can be made insensitive by setting
|
||||
* $caseSensitive to false.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains $needle
|
||||
*/
|
||||
public static function contains($haystack, $needle, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->contains($needle, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains any $needles, false otherwise. By
|
||||
* default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param array $needles Substrings to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains any $needles
|
||||
*/
|
||||
public static function containsAny($haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->containsAny($needles, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains all $needles, false otherwise. By
|
||||
* default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param array $needles Substrings to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains all $needles
|
||||
*/
|
||||
public static function containsAll($haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->containsAll($needles, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
{
|
||||
return (string) Stringy::create($str)->surround($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into the string at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->insert($substring, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length. If $substring is provided, and
|
||||
* truncating occurs, the string is further truncated so that the substring
|
||||
* may be appended without exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function truncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->truncate($length, $substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* split words. If $substring is provided, and truncating occurs, the
|
||||
* string is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->safeTruncate($length, $substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed string. A multibyte version of strrev().
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* A multibyte str_shuffle() function. It returns a string with its
|
||||
* characters in random order.
|
||||
*
|
||||
* @param string $str String to shuffle
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The shuffled string
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trimmed string. An alias for PHP's trim() function.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common prefix between the string and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common prefix
|
||||
*/
|
||||
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonPrefix($otherStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common suffix between the string and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common suffix
|
||||
*/
|
||||
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonSuffix($otherStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common substring between the string and $otherStr.
|
||||
* In the case of ties, it returns that which occurs first.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common substring
|
||||
*/
|
||||
public static function longestCommonSubstring($str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonSubstring($otherStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the string. An alias for PHP's mb_strlen() function.
|
||||
*
|
||||
* @param string $str The string to get the length of
|
||||
* @param string $encoding The character encoding
|
||||
* @return int The number of characters in $str given the encoding
|
||||
*/
|
||||
public static function length($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the substring beginning at $start with the specified $length.
|
||||
* 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.
|
||||
*
|
||||
* @param string $str The string to get the length of
|
||||
* @param int $start Position of the first character to use
|
||||
* @param int $length Maximum number of characters used
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The substring of $str
|
||||
*/
|
||||
public static function substr($str, $start, $length = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->substr($start, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character at $index, with indexes starting at 0.
|
||||
*
|
||||
* @param string $str The string from which to get the char
|
||||
* @param int $index Position of the character
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The character at $index
|
||||
*/
|
||||
public static function at($str, $index, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->at($index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first $n characters of the string.
|
||||
*
|
||||
* @param string $str The string from which to get the substring
|
||||
* @param int $n Number of chars to retrieve from the start
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The first $n characters
|
||||
*/
|
||||
public static function first($str, $n, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->first($n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last $n characters of the string.
|
||||
*
|
||||
* @param string $str The string from which to get the substring
|
||||
* @param int $n Number of chars to retrieve from the end
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The last $n characters
|
||||
*/
|
||||
public static function last($str, $n, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->last($n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the string begins with $substring. If it doesn't, it's
|
||||
* prepended.
|
||||
*
|
||||
* @param string $str The string to modify
|
||||
* @param string $substring The substring to add if not present
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string prefixed by the $substring
|
||||
*/
|
||||
public static function ensureLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->ensureLeft($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the string begins with $substring. If it doesn't, it's
|
||||
* appended.
|
||||
*
|
||||
* @param string $str The string to modify
|
||||
* @param string $substring The substring to add if not present
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string suffixed by the $substring
|
||||
*/
|
||||
public static function ensureRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->ensureRight($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the prefix $substring removed, if present.
|
||||
*
|
||||
* @param string $str String from which to remove the prefix
|
||||
* @param string $substring The prefix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the prefix $substring
|
||||
*/
|
||||
public static function removeLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->removeLeft($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the suffix $substring removed, if present.
|
||||
*
|
||||
* @param string $str String from which to remove the suffix
|
||||
* @param string $substring The suffix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the suffix $substring
|
||||
*/
|
||||
public static function removeRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->removeRight($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains a lower case char, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains a lower case character.
|
||||
*/
|
||||
public static function hasLowerCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->hasLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains an upper case char, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains an upper case character.
|
||||
*/
|
||||
public static function hasUpperCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->hasUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only alphabetic chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphabetic chars
|
||||
*/
|
||||
public static function isAlpha($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlpha();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only alphabetic and numeric chars,
|
||||
* false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphanumeric chars
|
||||
*/
|
||||
public static function isAlphanumeric($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlphanumeric();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only whitespace characters
|
||||
*/
|
||||
public static function isBlank($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isBlank();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is JSON, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str is JSON
|
||||
*/
|
||||
public static function isJson($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only lower case chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only lower case characters
|
||||
*/
|
||||
public static function isLowerCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is serialized, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str is serialized
|
||||
*/
|
||||
public static function isSerialized($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isSerialized();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only upper case chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only upper case characters
|
||||
*/
|
||||
public static function isUpperCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only hexadecimal characters
|
||||
*/
|
||||
public static function isHexadecimal($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isHexadecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of occurrences of $substring in the given string.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str The string to search through
|
||||
* @param string $substring The substring to search for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return int The number of $substring occurrences
|
||||
*/
|
||||
public static function countSubstr($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->countSubstr($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of $search in $str by $replacement.
|
||||
*
|
||||
* @param string $str The haystack to search through
|
||||
* @param string $search The needle to search for
|
||||
* @param string $replacement The string to replace with
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the replacements
|
||||
*/
|
||||
public static function replace($str, $search, $replacement, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->replace($search, $replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of $pattern in $str by $replacement. An alias
|
||||
* for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
|
||||
* in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
|
||||
* in the bundled version of Oniguruma in PHP 5.3.
|
||||
*
|
||||
* @param string $str The haystack to search through
|
||||
* @param string $pattern The regular expression pattern
|
||||
* @param string $replacement The string to replace with
|
||||
* @param string $options Matching conditions to be used
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the replacements
|
||||
*/
|
||||
public static function regexReplace($str, $pattern, $replacement,
|
||||
$options = 'msr', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->regexReplace($pattern, $replacement, $options, $encoding);
|
||||
}
|
||||
}
|
1464
src/Stringy.php
Normal file
1464
src/Stringy.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,673 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCaseFirst()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->lowerCaseFirst()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->camelize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an UpperCamelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to UpperCamelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCamelize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by dashes. Dashes are
|
||||
* inserted before uppercase characters (with the exception of the first
|
||||
* character of the string), and in place of spaces as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->dasherize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by underscores.
|
||||
* Underscores are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces as well as
|
||||
* dashes.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->underscored()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of a string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->swapCase()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of each word in a string, after trimming.
|
||||
* Ignores the case of other letters, allowing for the use of acronyms.
|
||||
* Also accepts an array, $ignore, allowing you to list words not to be
|
||||
* capitalized.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->titleize($ignore)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first word of a string, replaces underscores with spaces,
|
||||
* and strips '_id'.
|
||||
*
|
||||
* @param string $str String to humanize
|
||||
* @param string $encoding The character encoding
|
||||
* @return string A humanized string
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->humanize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||
* (and commonly used in Word documents) with their ASCII equivalents.
|
||||
*
|
||||
* @param string $str String to remove special chars
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->tidy()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This inclues tabs and newline characters.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
*/
|
||||
public static function collapseWhitespace($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->collapseWhitespace()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||
*
|
||||
* @param string $str A string with non-ASCII characters
|
||||
* @return string The string after the replacements
|
||||
*/
|
||||
public static function standardize($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->standardize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads a string to a given length with another string. If length is less
|
||||
* than or equal to the length of $str, then no padding takes places. The
|
||||
* default string used for padding is a space, and the default type (one of
|
||||
* 'left', 'right', 'both') is 'right'. Throws an exception if $padType
|
||||
* isn't one of those 3 values.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)
|
||||
->pad($length, $padStr, $padType)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the beginning of the
|
||||
* string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padLeft($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the end of the string is
|
||||
* padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padRight($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that both sides of the string
|
||||
* string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padBoth($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str begins with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the start of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->startsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str ends with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the end of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->endsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each tab in a string to some number of spaces, as defined by
|
||||
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
|
||||
*
|
||||
* @param string $str String to convert tabs to spaces
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toSpaces($tabLength)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each occurence of some consecutive number of spaces, as defined
|
||||
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
||||
* converted to a tab.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toTabs($tabLength)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the supplied text into an URL slug. This includes replacing
|
||||
* non-ASCII characters with their closest ASCII equivalents, removing
|
||||
* non-alphanumeric and non-ASCII characters, and replacing whitespace with
|
||||
* dashes. The string is also converted to lowercase.
|
||||
*
|
||||
* @param string $str Text to transform into an URL slug
|
||||
* @return string The corresponding URL slug
|
||||
*/
|
||||
public static function slugify($str)
|
||||
{
|
||||
return Stringy::create($str)->slugify()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $haystack contains $needle, false otherwise.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains $needle
|
||||
*/
|
||||
public static function contains($haystack, $needle, $encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)->contains($needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
{
|
||||
return Stringy::create($str)->surround($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into $str at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->insert($substring, $index)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates $str to a given length. If $substring is provided, and
|
||||
* truncating occurs, the string is further truncated so that the substring
|
||||
* may be appended without exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function truncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->truncate($length, $substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* chop words. If $substring is provided, and truncating occurs, the string
|
||||
* is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->safeTruncate($length, $substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses a string. A multibyte version of strrev.
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->reverse()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* A multibyte str_shuffle function. It randomizes the order of characters
|
||||
* in a string.
|
||||
*
|
||||
* @param string $str String to shuffle
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The shuffled string
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->shuffle()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims $str. An alias for PHP's trim() function.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the longest common prefix between $str and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common prefix
|
||||
*/
|
||||
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonPrefix($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the longest common suffix between $str and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common suffix
|
||||
*/
|
||||
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonSuffix($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the longest common substring between $str and $otherStr. In the
|
||||
* case of ties, returns that which occurs first.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common substring
|
||||
*/
|
||||
public static function longestCommonSubstring($str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonSubstring($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of $str. An alias for PHP's mb_strlen() function.
|
||||
*
|
||||
* @param string $str The string to get the length of
|
||||
* @param string $encoding The character encoding
|
||||
* @return int The number of characters in $str given the encoding
|
||||
*/
|
||||
public static function length($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the substring of $str beginning at $start with the specified $length.
|
||||
* 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.
|
||||
*
|
||||
* @param string $str The string to get the length of
|
||||
* @param int $start Position of the first character to use from str
|
||||
* @param int $length Maximum number of characters used
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The substring of $str
|
||||
*/
|
||||
public static function substr($str, $start, $length = null, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->substr($start, $length)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the character of $str at $index, with indexes starting at 0.
|
||||
*
|
||||
* @param string $str The string from which to get the char
|
||||
* @param int $index Position of the character
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The character at $index
|
||||
*/
|
||||
public static function at($str, $index, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->at($index)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first $n characters of $str.
|
||||
*
|
||||
* @param string $str The string from which to get the substring
|
||||
* @param int $n Number of chars to retrieve from the start
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The first $n characters
|
||||
*/
|
||||
public static function first($str, $n, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->first($n)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last $n characters of $str.
|
||||
*
|
||||
* @param string $str The string from which to get the substring
|
||||
* @param int $n Number of chars to retrieve from the end
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The last $n characters
|
||||
*/
|
||||
public static function last($str, $n, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->last($n)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that $str begins with $substring.
|
||||
*
|
||||
* @param string $str The string to modify
|
||||
* @param string $substring The substring to add if not present
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string prefixed by the $substring
|
||||
*/
|
||||
public static function ensureLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->ensureLeft($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that $str ends with $substring.
|
||||
*
|
||||
* @param string $str The string to modify
|
||||
* @param string $substring The substring to add if not present
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string suffixed by the $substring
|
||||
*/
|
||||
public static function ensureRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->ensureRight($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the prefix $substring if present.
|
||||
*
|
||||
* @param string $str String from which to remove the prefix
|
||||
* @param string $substring The prefix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the prefix $substring
|
||||
*/
|
||||
public static function removeLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->removeLeft($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the suffix $substring if present.
|
||||
*
|
||||
* @param string $str String from which to remove the suffix
|
||||
* @param string $substring The suffix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the suffix $substring
|
||||
*/
|
||||
public static function removeRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->removeRight($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $str contains only alphabetic chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphabetic chars
|
||||
*/
|
||||
public static function isAlpha($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlpha();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $str contains only alphabetic and numeric chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphanumeric chars
|
||||
*/
|
||||
public static function isAlphanumeric($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlphanumeric();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $str contains only whitespace chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only whitespace characters
|
||||
*/
|
||||
public static function isBlank($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isBlank();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $str contains only lower case chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only lower case characters
|
||||
*/
|
||||
public static function isLowerCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $str contains only upper case chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only upper case characters
|
||||
*/
|
||||
public static function isUpperCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of occurences of $substring in $str. An alias for
|
||||
* mb_substr_count()
|
||||
*
|
||||
* @param string $str The haystack to search through
|
||||
* @param string $substring The substring to search for
|
||||
* @param string $encoding The character encoding
|
||||
* @return int The number of $substring occurences
|
||||
*/
|
||||
public static function count($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->count($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of $search with $replace in $str.
|
||||
*
|
||||
* @param string $str The haystack to search through
|
||||
* @param string $search The needle to search for
|
||||
* @param string $replace The string to replace with
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the replacements
|
||||
*/
|
||||
public static function replace($str, $search, $replace, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->replace($search, $replace);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
16
tests/CreateTest.php
Normal file
16
tests/CreateTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/Create.php';
|
||||
|
||||
use function Stringy\create as s;
|
||||
|
||||
class CreateTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreate()
|
||||
{
|
||||
$stringy = s('foo bar', 'UTF-8');
|
||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||
$this->assertEquals('foo bar', (string) $stringy);
|
||||
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||
}
|
||||
}
|
650
tests/StaticStringyTest.php
Normal file
650
tests/StaticStringyTest.php
Normal file
@@ -0,0 +1,650 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/StaticStringy.php';
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
public function testChars($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::chars($str, $encoding);
|
||||
$this->assertInternalType('array', $result);
|
||||
foreach ($result as $char) {
|
||||
$this->assertInternalType('string', $char);
|
||||
}
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCaseFirstProvider()
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCaseFirst($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lowerCaseFirstProvider()
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::lowerCaseFirst($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider camelizeProvider()
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::camelize($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCamelizeProvider()
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCamelize($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dasherizeProvider()
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::dasherize($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider underscoredProvider()
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::underscored($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider swapCaseProvider()
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::swapCase($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider titleizeProvider()
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::titleize($str, $ignore, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider humanizeProvider()
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::humanize($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider tidyProvider()
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$result = S::tidy($str);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider collapseWhitespaceProvider()
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::collapseWhitespace($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toAsciiProvider()
|
||||
*/
|
||||
public function testToAscii($expected, $str, $removeUnsupported = true)
|
||||
{
|
||||
$result = S::toAscii($str, $removeUnsupported);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padProvider()
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$result = S::pad($str, $length, $padStr, $padType, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testPadException()
|
||||
{
|
||||
$result = S::pad('string', 5, 'foo', 'bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padLeftProvider()
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padLeft($str, $length, $padStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padRightProvider()
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padRight($str, $length, $padStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padBothProvider()
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padBoth($str, $length, $padStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider startsWithProvider()
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider endsWithProvider()
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toSpacesProvider()
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toSpaces($str, $tabLength);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTabsProvider()
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toTabs($str, $tabLength);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toLowerCaseProvider()
|
||||
*/
|
||||
public function testToLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::toLowerCase($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTitleCaseProvider()
|
||||
*/
|
||||
public function testToTitleCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::toTitleCase($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
public function testToUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::toUpperCase($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider slugifyProvider()
|
||||
*/
|
||||
public function testSlugify($expected, $str, $replacement = '-')
|
||||
{
|
||||
$result = S::slugify($str, $replacement);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsProvider()
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::contains($haystack, $needle, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAnyProvider()
|
||||
*/
|
||||
public function testcontainsAny($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::containsAny($haystack, $needles, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAllProvider()
|
||||
*/
|
||||
public function testContainsAll($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::containsAll($haystack, $needles, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$result = S::surround($str, $substring);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider insertProvider()
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::insert($str, $substring, $index, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider truncateProvider()
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::truncate($str, $length, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider safeTruncateProvider()
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::safeTruncate($str, $length, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider reverseProvider()
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::reverse($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider shuffleProvider()
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
$result = S::shuffle($str, $encoding);
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals(mb_strlen($str, $encoding),
|
||||
mb_strlen($result, $encoding));
|
||||
|
||||
// We'll make sure that the chars are present after shuffle
|
||||
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
|
||||
$char = mb_substr($str, $i, 1, $encoding);
|
||||
$countBefore = mb_substr_count($str, $char, $encoding);
|
||||
$countAfter = mb_substr_count($result, $char, $encoding);
|
||||
$this->assertEquals($countBefore, $countAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
$result = S::trim($str);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonPrefixProvider()
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonPrefix($str, $otherStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonSuffixProvider()
|
||||
*/
|
||||
public function testLongestCommonSuffix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonSuffix($str, $otherStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonSubstringProvider()
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonSubstring($str, $otherStr, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lengthProvider()
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::length($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertInternalType('int', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider substrProvider()
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::substr($str, $start, $length, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider atProvider()
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
$result = S::at($str, $index, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider firstProvider()
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$result = S::first($str, $n, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lastProvider()
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$result = S::last($str, $n, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ensureLeftProvider()
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::ensureLeft($str, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ensureRightProvider()
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::ensureRight($str, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider removeLeftProvider()
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::removeLeft($str, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider removeRightProvider()
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::removeRight($str, $substring, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isAlphaProvider()
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isAlpha($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isAlphanumericProvider()
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isAlphanumeric($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isBlankProvider()
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isBlank($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isJsonProvider()
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isJson($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isLowerCaseProvider()
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isLowerCase($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider hasLowerCaseProvider()
|
||||
*/
|
||||
public function testHasLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::hasLowerCase($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isSerializedProvider()
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isSerialized($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isUpperCaseProvider()
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isUpperCase($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider hasUpperCaseProvider()
|
||||
*/
|
||||
public function testHasUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::hasUpperCase($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isHexadecimalProvider()
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isHexadecimal($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider countSubstrProvider()
|
||||
*/
|
||||
public function testCountSubstr($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::countSubstr($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('int', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider replaceProvider()
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replacement,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::replace($str, $search, $replacement, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider regexReplaceProvider()
|
||||
*/
|
||||
public function testRegexReplace($expected, $str, $pattern, $replacement,
|
||||
$options = 'msr', $encoding = null)
|
||||
{
|
||||
$result = S::regexReplace($str, $pattern, $replacement, $options, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
@@ -1,457 +0,0 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/StaticStringy.php");
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCaseFirst($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::lowerCaseFirst($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::camelize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCamelize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::dasherize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::underscored($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::swapCase($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::titleize($str, $ignore, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::humanize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$result = S::tidy($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
{
|
||||
$result = S::collapseWhitespace($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStandardize
|
||||
*/
|
||||
public function testStandardize($expected, $str)
|
||||
{
|
||||
$result = S::standardize($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$result = S::pad($str, $length, $padStr, $padType, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padLeft($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padRight($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padBoth($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toSpaces($str, $tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toTabs($str, $tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
*/
|
||||
public function testSlugify($expected, $str)
|
||||
{
|
||||
$result = S::slugify($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
{
|
||||
$result = S::contains($haystack, $needle, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$result = S::surround($str, $substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::insert($str, $substring, $index, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTruncate
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::truncate($str, $length, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::safeTruncate($str, $length, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::reverse($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$result = S::shuffle($str, $encoding);
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTrim
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
$result = S::trim($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonPrefix
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonPrefix($str, $otherStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSuffix
|
||||
*/
|
||||
public function testLongestCommonSuffix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonSuffix($str, $otherStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSubstring
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::longestCommonSubstring($str, $otherStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLength
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::length($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSubstr
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::substr($str, $start, $length, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForAt
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
$result = S::at($str, $index, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForFirst
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$result = S::first($str, $n, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLast
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$result = S::last($str, $n, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureLeft
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::ensureLeft($str, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureRight
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::ensureRight($str, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveLeft
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::removeLeft($str, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveRight
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::removeRight($str, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlpha
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isAlpha($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlphanumeric
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isAlphanumeric($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsBlank
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isBlank($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isLowerCase($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isUpperCase($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
public function testCount($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$result = S::count($str, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReplace
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replace,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::replace($str, $search, $replace, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
@@ -1,527 +0,0 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/Stringy.php");
|
||||
|
||||
use Stringy\Stringy as S;
|
||||
|
||||
class StringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->upperCaseFirst();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->lowerCaseFirst();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->camelize();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->upperCamelize();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->dasherize();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->underscored();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->swapCase();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->titleize($ignore);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->humanize();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->tidy();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->collapseWhitespace();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStandardize
|
||||
*/
|
||||
public function testStandardize($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->standardize();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->pad($length, $padStr, $padType);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padLeft($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padRight($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padBoth($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->startsWith($substring, $caseSensitive);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->endsWith($substring, $caseSensitive);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->toSpaces($tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->toTabs($tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
*/
|
||||
public function testSlugify($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->slugify();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->contains($needle);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->surround($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->insert($substring, $index);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTruncate
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->truncate($length, $substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->reverse();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->shuffle();
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTrim
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->trim();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonPrefix
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->longestCommonPrefix($otherStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSubstring
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->longestCommonSubstring($otherStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLength
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->length();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSubstr
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->substr($start, $length);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForAt
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->at($index);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForFirst
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->first($n);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLast
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->last($n);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureLeft
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->ensureLeft($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureRight
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->ensureRight($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveLeft
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->removeLeft($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveRight
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->removeRight($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlpha
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isAlpha();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlphanumeric
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isAlphanumeric();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsBlank
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isBlank();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isLowerCase();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isUpperCase();
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
public function testCount($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->count($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReplace
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replace,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->replace($search, $replace);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
}
|
914
tests/StringyTest.php
Normal file
914
tests/StringyTest.php
Normal file
@@ -0,0 +1,914 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/Stringy.php';
|
||||
|
||||
use Stringy\Stringy as S;
|
||||
|
||||
class StringyTestCase extends CommonTest
|
||||
{
|
||||
public function testConstruct()
|
||||
{
|
||||
$stringy = new S('foo bar', 'UTF-8');
|
||||
$this->assertStringy($stringy);
|
||||
$this->assertEquals('foo bar', (string) $stringy);
|
||||
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testConstructWithArray()
|
||||
{
|
||||
(string) new S(array());
|
||||
$this->fail('Expecting exception when the constructor is passed an array');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testMissingToString()
|
||||
{
|
||||
(string) new S(new stdClass());
|
||||
$this->fail('Expecting exception when the constructor is passed an ' .
|
||||
'object without a __toString method');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toStringProvider()
|
||||
*/
|
||||
public function testToString($expected, $str)
|
||||
{
|
||||
$this->assertEquals($expected, (string) new S($str));
|
||||
}
|
||||
|
||||
public function toStringProvider()
|
||||
{
|
||||
return array(
|
||||
array('', null),
|
||||
array('', false),
|
||||
array('1', true),
|
||||
array('-9', -9),
|
||||
array('1.18', 1.18),
|
||||
array(' string ', ' string ')
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$stringy = S::create('foo bar', 'UTF-8');
|
||||
$this->assertStringy($stringy);
|
||||
$this->assertEquals('foo bar', (string) $stringy);
|
||||
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||
}
|
||||
|
||||
public function testChaining()
|
||||
{
|
||||
$stringy = S::create("Fòô Bàř", 'UTF-8');
|
||||
$this->assertStringy($stringy);
|
||||
$result = $stringy->collapseWhitespace()->swapCase()->upperCaseFirst();
|
||||
$this->assertEquals('FÒÔ bÀŘ', $result);
|
||||
}
|
||||
|
||||
public function testCount()
|
||||
{
|
||||
$stringy = S::create('Fòô', 'UTF-8');
|
||||
$this->assertEquals(3, $stringy->count());
|
||||
$this->assertEquals(3, count($stringy));
|
||||
}
|
||||
|
||||
public function testGetIterator()
|
||||
{
|
||||
$stringy = S::create('Fòô Bàř', 'UTF-8');
|
||||
|
||||
$valResult = array();
|
||||
foreach ($stringy as $char) {
|
||||
$valResult[] = $char;
|
||||
}
|
||||
|
||||
$keyValResult = array();
|
||||
foreach ($stringy as $pos => $char) {
|
||||
$keyValResult[$pos] = $char;
|
||||
}
|
||||
|
||||
$this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $valResult);
|
||||
$this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $keyValResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider offsetExistsProvider()
|
||||
*/
|
||||
public function testOffsetExists($expected, $offset)
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$this->assertEquals($expected, $stringy->offsetExists($offset));
|
||||
$this->assertEquals($expected, isset($stringy[$offset]));
|
||||
}
|
||||
|
||||
public function offsetExistsProvider()
|
||||
{
|
||||
return array(
|
||||
array(true, 0),
|
||||
array(true, 2),
|
||||
array(false, 3),
|
||||
array(true, -1),
|
||||
array(true, -3),
|
||||
array(false, -4)
|
||||
);
|
||||
}
|
||||
|
||||
public function testOffsetGet()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
|
||||
$this->assertEquals('f', $stringy->offsetGet(0));
|
||||
$this->assertEquals('ô', $stringy->offsetGet(2));
|
||||
|
||||
$this->assertEquals('ô', $stringy[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \OutOfBoundsException
|
||||
*/
|
||||
public function testOffsetGetOutOfBounds()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$test = $stringy[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testOffsetSet()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$stringy[1] = 'invalid';
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testOffsetUnset()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
unset($stringy[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
public function testChars($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->chars();
|
||||
$this->assertInternalType('array', $result);
|
||||
foreach ($result as $char) {
|
||||
$this->assertInternalType('string', $char);
|
||||
}
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCaseFirstProvider()
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->upperCaseFirst();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lowerCaseFirstProvider()
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->lowerCaseFirst();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider camelizeProvider()
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->camelize();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCamelizeProvider()
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->upperCamelize();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dasherizeProvider()
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->dasherize();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider underscoredProvider()
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->underscored();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider swapCaseProvider()
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->swapCase();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider titleizeProvider()
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->titleize($ignore);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider humanizeProvider()
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->humanize();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider tidyProvider()
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->tidy();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider collapseWhitespaceProvider()
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->collapseWhitespace();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toAsciiProvider()
|
||||
*/
|
||||
public function testToAscii($expected, $str, $removeUnsupported = true)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->toAscii($removeUnsupported);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padProvider()
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->pad($length, $padStr, $padType);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testPadException()
|
||||
{
|
||||
$stringy = S::create('foo');
|
||||
$result = $stringy->pad(5, 'foo', 'bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padLeftProvider()
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padLeft($length, $padStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padRightProvider()
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padRight($length, $padStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider padBothProvider()
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->padBoth($length, $padStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider startsWithProvider()
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->startsWith($substring, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider endsWithProvider()
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->endsWith($substring, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toSpacesProvider()
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->toSpaces($tabLength);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTabsProvider()
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->toTabs($tabLength);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toLowerCaseProvider()
|
||||
*/
|
||||
public function testToLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->toLowerCase();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTitleCaseProvider()
|
||||
*/
|
||||
public function testToTitleCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->toTitleCase();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
public function testToUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->toUpperCase();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider slugifyProvider()
|
||||
*/
|
||||
public function testSlugify($expected, $str, $replacement = '-')
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->slugify($replacement);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsProvider()
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->contains($needle, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAnyProvider()
|
||||
*/
|
||||
public function testcontainsAny($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->containsAny($needles, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider containsAllProvider()
|
||||
*/
|
||||
public function testContainsAll($expected, $haystack, $needles,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->containsAll($needles, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->surround($substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider insertProvider()
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->insert($substring, $index);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider truncateProvider()
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->truncate($length, $substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider safeTruncateProvider()
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->safeTruncate($length, $substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider reverseProvider()
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->reverse();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider shuffleProvider()
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$result = $stringy->shuffle();
|
||||
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
$this->assertEquals(mb_strlen($str, $encoding),
|
||||
mb_strlen($result, $encoding));
|
||||
|
||||
// We'll make sure that the chars are present after shuffle
|
||||
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
|
||||
$char = mb_substr($str, $i, 1, $encoding);
|
||||
$countBefore = mb_substr_count($str, $char, $encoding);
|
||||
$countAfter = mb_substr_count($result, $char, $encoding);
|
||||
$this->assertEquals($countBefore, $countAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->trim();
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonPrefixProvider()
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->longestCommonPrefix($otherStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonSuffixProvider()
|
||||
*/
|
||||
public function testLongestCommonSuffix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->longestCommonSuffix($otherStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider longestCommonSubstringProvider()
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->longestCommonSubstring($otherStr);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lengthProvider()
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->length();
|
||||
$this->assertInternalType('int', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider substrProvider()
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->substr($start, $length);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider atProvider()
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->at($index);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider firstProvider()
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->first($n);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lastProvider()
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->last($n);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ensureLeftProvider()
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->ensureLeft($substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ensureRightProvider()
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->ensureRight($substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider removeLeftProvider()
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->removeLeft($substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider removeRightProvider()
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->removeRight($substring);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isAlphaProvider()
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isAlpha();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isAlphanumericProvider()
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isAlphanumeric();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isBlankProvider()
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isBlank();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isJsonProvider()
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isJson();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isLowerCaseProvider()
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isLowerCase();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider hasLowerCaseProvider()
|
||||
*/
|
||||
public function testHasLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->hasLowerCase();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isSerializedProvider()
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isSerialized();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isUpperCaseProvider()
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isUpperCase();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider hasUpperCaseProvider()
|
||||
*/
|
||||
public function testHasUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->hasUpperCase();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isHexadecimalProvider()
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isHexadecimal();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider countSubstrProvider()
|
||||
*/
|
||||
public function testCountSubstr($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->countSubstr($substring, $caseSensitive);
|
||||
$this->assertInternalType('int', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider replaceProvider()
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replacement,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->replace($search, $replacement);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider regexReplaceProvider()
|
||||
*/
|
||||
public function testregexReplace($expected, $str, $pattern, $replacement,
|
||||
$options = 'msr', $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->regexReplace($pattern, $replacement, $options);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user