1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-15 23:42:08 +02:00

8 Commits
2.0.0 ... 2.1.0

Author SHA1 Message Date
Daniel St. Jules
efb10020f6 2.1 2015-09-02 23:50:48 -07:00
Daniel St. Jules
ae74e66369 Update readme notes on StaticStringy 2015-09-02 23:47:07 -07:00
Daniel St. Jules
f6475e1288 Stringy::create str defaults to empty string 2015-09-02 23:46:48 -07:00
Daniel St. Jules
ff36c79892 Add test for StaticStringy invocation 2015-09-02 23:37:17 -07:00
Daniel St. Jules
8f40b3495d String is now optional in Stringy constructor 2015-09-02 23:35:32 -07:00
Daniel St. Jules
e237f30d94 Bring back StaticStringy as a __callStatic wrapper 2015-09-02 00:12:25 -07:00
Daniel St. Jules
ad6d32080f Update titleize description in readme 2015-07-29 02:09:00 -07:00
Daniel St. Jules
7377aabf61 Fix titleize example 2015-07-29 02:05:16 -07:00
7 changed files with 225 additions and 8 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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
View 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);
}
}

View File

@@ -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);
}

View 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);
}
}
}

View File

@@ -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
*/