From 27ef5a891478d529ff6634f461b8d657051e133d Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Thu, 1 Jan 2015 21:38:58 -0800 Subject: [PATCH] Add Stringy\create function for PHP 5.6 --- README.md | 16 ++++++++++++++++ composer.json | 5 ++++- phpunit.xml.dist | 5 ++++- src/Create.php | 17 +++++++++++++++++ tests/CreateTest.php | 16 ++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/Create.php create mode 100644 tests/CreateTest.php diff --git a/README.md b/README.md index 8534ee7..b0ba92a 100644 --- a/README.md +++ b/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 diff --git a/composer.json b/composer.json index b9ef099..499815c 100644 --- a/composer.json +++ b/composer.json @@ -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" ] } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e65b79e..987dbbf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,7 +5,10 @@ syntaxCheck="false"> - tests + tests/CommonTest.php + tests/StringyTest.php + tests/StaticStringyTest.php + tests/CreateTest.php diff --git a/src/Create.php b/src/Create.php new file mode 100644 index 0000000..737e329 --- /dev/null +++ b/src/Create.php @@ -0,0 +1,17 @@ +assertInstanceOf('Stringy\Stringy', $stringy); + $this->assertEquals('foo bar', (string) $stringy); + $this->assertEquals('UTF-8', $stringy->getEncoding()); + } +}