mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-13 08:44:01 +02:00
Add Stringy\create function for PHP 5.6
This commit is contained in:
16
README.md
16
README.md
@@ -9,6 +9,7 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||
* [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)
|
||||
@@ -171,6 +172,21 @@ $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
|
||||
|
@@ -25,9 +25,12 @@
|
||||
"source": "https://github.com/danielstjules/Stringy"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Stringy\\": "src/" }
|
||||
"psr-4": { "Stringy\\": "src/" },
|
||||
"files": ["src/Create.php"]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": { "Stringy\\": "src/" },
|
||||
"files": ["src/Create.php"],
|
||||
"classmap": [ "tests" ]
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,10 @@
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<directory>tests</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>
|
||||
|
17
src/Create.php
Normal file
17
src/Create.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
/**
|
||||
* Invokes Stringy::create and returns the generated Stringy object 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);
|
||||
}
|
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user