mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-15 23:42:08 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
efb10020f6 | ||
|
ae74e66369 | ||
|
f6475e1288 | ||
|
ff36c79892 | ||
|
8f40b3495d | ||
|
e237f30d94 | ||
|
ad6d32080f | ||
|
7377aabf61 |
@@ -1,3 +1,8 @@
|
||||
### 2.1.0 (2015-09-02)
|
||||
|
||||
* Added simplified StaticStringy class
|
||||
* str in Stringy::create and constructor is now optional
|
||||
|
||||
### 2.0.0 (2015-07-29)
|
||||
|
||||
* Removed StaticStringy class
|
||||
|
26
README.md
26
README.md
@@ -15,6 +15,7 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
|
||||
* [OO and Chaining](#oo-and-chaining)
|
||||
* [Implemented Interfaces](#implemented-interfaces)
|
||||
* [PHP 5.6 Creation](#php-56-creation)
|
||||
* [StaticStringy](#staticstringy)
|
||||
* [Class methods](#class-methods)
|
||||
* [create](#createmixed-str--encoding-)
|
||||
* [Instance methods](#instance-methods)
|
||||
@@ -127,7 +128,7 @@ in your composer.json file:
|
||||
|
||||
```json
|
||||
"require": {
|
||||
"danielstjules/stringy": "~2.0"
|
||||
"danielstjules/stringy": "~2.1"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -223,6 +224,21 @@ use function Stringy\create as s;
|
||||
s('fòô bàř')->collapseWhitespace()->swapCase();
|
||||
```
|
||||
|
||||
## StaticStringy
|
||||
|
||||
All methods listed under "Instance methods" are available as part of a static
|
||||
wrapper. For StaticStringy methods, the optional encoding is expected to be the
|
||||
last argument. The return value is not cast, and may thus be of type Stringy,
|
||||
integer, boolean, etc.
|
||||
|
||||
```php
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
// Translates to Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
|
||||
// Returns a Stringy object with the string "fòô"
|
||||
S::slice('fòôbàř', 0, 3, 'UTF-8');
|
||||
```
|
||||
|
||||
## Class methods
|
||||
|
||||
##### create(mixed $str [, $encoding ])
|
||||
@@ -800,13 +816,13 @@ s('“I see…”')->tidy(); // '"I see..."'
|
||||
##### titleize([, array $ignore])
|
||||
|
||||
Returns a trimmed string with the first letter of each word capitalized.
|
||||
Ignores the case of other letters, preserving any acronyms. Also accepts
|
||||
an array, $ignore, allowing you to list words not to be capitalized.
|
||||
Also accepts an array, $ignore, allowing you to list words not to be
|
||||
capitalized.
|
||||
|
||||
```php
|
||||
$ignore = ['at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the'];
|
||||
s('If you optimize everything, you will always be unhappy.')->titleize($ignore);
|
||||
// 'I Like to Watch television'
|
||||
s('i like to watch television')->titleize($ignore);
|
||||
// 'I Like to Watch Television'
|
||||
```
|
||||
|
||||
##### toAscii()
|
||||
|
@@ -5,8 +5,8 @@
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<file>tests/CommonTest.php</file>
|
||||
<file>tests/StringyTest.php</file>
|
||||
<file>tests/StaticStringyTest.php</file>
|
||||
<file phpVersion="5.6.0">tests/CreateTest.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
127
src/StaticStringy.php
Normal file
127
src/StaticStringy.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* A mapping of method names to the numbers of arguments it accepts. Each
|
||||
* should be two more than the equivalent Stringy method. Necessary as
|
||||
* static methods place the optional $encoding as the last parameter.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $methodArgs = array(
|
||||
'append' => 3,
|
||||
'at' => 3,
|
||||
'between' => 5,
|
||||
'camelize' => 2,
|
||||
'chars' => 2,
|
||||
'collapseWhitespace' => 2,
|
||||
'contains' => 4,
|
||||
'containsAll' => 4,
|
||||
'containsAny' => 4,
|
||||
'count' => 2,
|
||||
'countSubstr' => 4,
|
||||
'dasherize' => 2,
|
||||
'delimit' => 3,
|
||||
'endsWith' => 4,
|
||||
'ensureLeft' => 3,
|
||||
'ensureRight' => 3,
|
||||
'first' => 3,
|
||||
'getEncoding' => 2,
|
||||
'hasLowerCase' => 2,
|
||||
'hasUpperCase' => 2,
|
||||
'htmlDecode' => 3,
|
||||
'htmlEncode' => 3,
|
||||
'humanize' => 2,
|
||||
'indexOf' => 4,
|
||||
'indexOfLast' => 4,
|
||||
'insert' => 4,
|
||||
'isAlpha' => 2,
|
||||
'isAlphanumeric' => 2,
|
||||
'isBlank' => 2,
|
||||
'isHexadecimal' => 2,
|
||||
'isJson' => 2,
|
||||
'isLowerCase' => 2,
|
||||
'isSerialized' => 2,
|
||||
'isUpperCase' => 2,
|
||||
'last' => 3,
|
||||
'length' => 2,
|
||||
'lines' => 2,
|
||||
'longestCommonPrefix' => 3,
|
||||
'longestCommonSuffix' => 3,
|
||||
'longestCommonSubstring' => 3,
|
||||
'lowerCaseFirst' => 2,
|
||||
'pad' => 5,
|
||||
'padBoth' => 4,
|
||||
'padLeft' => 4,
|
||||
'padRight' => 4,
|
||||
'prepend' => 3,
|
||||
'regexReplace' => 5,
|
||||
'removeLeft' => 3,
|
||||
'removeRight' => 3,
|
||||
'repeat' => 3,
|
||||
'replace' => 4,
|
||||
'reverse' => 2,
|
||||
'safeTruncate' => 4,
|
||||
'shuffle' => 2,
|
||||
'slugify' => 3,
|
||||
'startsWith' => 4,
|
||||
'slice' => 4,
|
||||
'split' => 4,
|
||||
'substr' => 4,
|
||||
'surround' => 3,
|
||||
'swapCase' => 2,
|
||||
'tidy' => 2,
|
||||
'titleize' => 3,
|
||||
'toAscii' => 3,
|
||||
'toBoolean' => 2,
|
||||
'toLowerCase' => 2,
|
||||
'toSpaces' => 3,
|
||||
'toTabs' => 3,
|
||||
'toTitleCase' => 2,
|
||||
'toUpperCase' => 2,
|
||||
'trim' => 3,
|
||||
'trimLeft' => 3,
|
||||
'trimRight' => 3,
|
||||
'truncate' => 4,
|
||||
'underscored' => 2,
|
||||
'upperCamelize' => 2,
|
||||
'upperCaseFirst' => 2
|
||||
);
|
||||
|
||||
/**
|
||||
* Creates an instance of Stringy and invokes the given method with the
|
||||
* rest of the passed arguments. The optional encoding is expected to be
|
||||
* the last argument. For example, the following:
|
||||
* StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
|
||||
* Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
|
||||
* The result is not cast, so the return value may be of type Stringy,
|
||||
* integer, boolean, etc.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed[] $arguments
|
||||
*/
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
if (!isset(static::$methodArgs[$name])) {
|
||||
throw new \BadMethodCallException($name . ' is not a valid method');
|
||||
}
|
||||
|
||||
$numArgs = count($arguments);
|
||||
$str = ($numArgs) ? $arguments[0] : '';
|
||||
|
||||
if ($numArgs === static::$methodArgs[$name]) {
|
||||
$args = array_slice($arguments, 1, -1);
|
||||
$encoding = $arguments[$numArgs - 1];
|
||||
} else {
|
||||
$args = array_slice($arguments, 1);
|
||||
$encoding = null;
|
||||
}
|
||||
|
||||
$stringy = Stringy::create($str, $encoding);
|
||||
|
||||
return call_user_func_array(array($stringy, $name), $args);
|
||||
}
|
||||
}
|
@@ -31,7 +31,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
* @throws \InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
*/
|
||||
public function __construct($str, $encoding = null)
|
||||
public function __construct($str = '', $encoding = null)
|
||||
{
|
||||
if (is_array($str)) {
|
||||
throw new \InvalidArgumentException(
|
||||
@@ -60,7 +60,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
* @throws \InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
*/
|
||||
public static function create($str, $encoding = null)
|
||||
public static function create($str = '', $encoding = null)
|
||||
{
|
||||
return new static($str, $encoding);
|
||||
}
|
||||
|
62
tests/StaticStringyTest.php
Normal file
62
tests/StaticStringyTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/StaticStringy.php';
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function testBadMethodCall()
|
||||
{
|
||||
$result = S::invalidMethod('foo');
|
||||
}
|
||||
|
||||
public function testEmptyArgsInvocation()
|
||||
{
|
||||
$result = S::toLowerCase();
|
||||
$this->assertEquals('', (string) $result);
|
||||
}
|
||||
|
||||
public function testInvocation()
|
||||
{
|
||||
$result = S::toLowerCase('FOOBAR');
|
||||
$this->assertEquals('foobar', (string) $result);
|
||||
}
|
||||
|
||||
public function testPartialArgsInvocation()
|
||||
{
|
||||
$result = S::slice('foobar', 0, 3);
|
||||
$this->assertEquals('foo', (string) $result);
|
||||
}
|
||||
|
||||
public function testFullArgsInvocation()
|
||||
{
|
||||
$result = S::slice('fòôbàř', 0, 3, 'UTF-8');
|
||||
$this->assertEquals('fòô', (string) $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use reflection to ensure that all argument numbers are correct. Each
|
||||
* static method should accept 2 more arguments than their Stringy
|
||||
* equivalent.
|
||||
*/
|
||||
public function testArgumentNumbers()
|
||||
{
|
||||
$staticStringyClass = new ReflectionClass('Stringy\StaticStringy');
|
||||
$stringyClass = new ReflectionClass('Stringy\Stringy');
|
||||
|
||||
// getStaticPropertyValue can't access protected properties
|
||||
$properties = $staticStringyClass->getStaticProperties();
|
||||
|
||||
foreach ($properties['methodArgs'] as $method => $expected) {
|
||||
$num = $stringyClass->getMethod($method)
|
||||
->getNumberOfParameters() + 2;
|
||||
|
||||
$this->assertEquals($expected, $num,
|
||||
'Invalid num args for ' . $method);
|
||||
}
|
||||
}
|
||||
}
|
@@ -24,6 +24,13 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||
}
|
||||
|
||||
public function testEmptyConstruct()
|
||||
{
|
||||
$stringy = new S();
|
||||
$this->assertStringy($stringy);
|
||||
$this->assertEquals('', (string) $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
|
Reference in New Issue
Block a user