diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..31b8b51 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: php + +php: + - 7.4 + +before_script: + - composer self-update + - composer install --prefer-source --no-interaction --dev + +script: vendor/bin/phpunit \ No newline at end of file diff --git a/Helpers/helpers.php b/Helpers/helpers.php index cbea9e0..87bdc29 100644 --- a/Helpers/helpers.php +++ b/Helpers/helpers.php @@ -12,6 +12,6 @@ */ function piper($value, $callback = null) { return (new Piper()) - ->on($value, $callback); + ->pipe($value, $callback); } } \ No newline at end of file diff --git a/README.md b/README.md index 98c52d4..5e5042a 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ use Transprime\Piper\Piper; // Normal $piper = new Piper(); -$piper->on(['AA'])->to('strtolower')->up(); +$piper->pipe(['AA'])->to('strtolower')->up(); // Better $piper->on(['AA'])->to('strtolower')(); @@ -65,16 +65,19 @@ $piper->on(['AA'])->to('strtolower')(); `piper()` function is a helper function. It is normally called like so: ```php +// Good +Piper::on('AA')->to('strtolower')->up(); + +//Better +Piper::on('AA')->to('strtolower')(); // Nifty piper(['AA'])->to('strtolower')(); ``` -Piper `on()` method accepts a callable instance on the second parameter that would be immediately on the first parameter: +Piper `piper()` function accepts a callable instance on the second parameter that would be immediately on the first parameter: -Say we have this class: - -Second callable parameter to `on()` method, which 'NAME' shall first be called on: +Second callable parameter to `piper()` function, which 'NAME' shall first be called on: ```php // test global method @@ -86,6 +89,7 @@ piper('NAME', 'strtolower') // NAME becomes name Also accepts a class with an accessible method name: Say we have this class: + ```php class StrManipulator { @@ -128,15 +132,6 @@ Please see `\Piper\Tests\PiperTest` for more examples. ## Coming Soon -- Calling piper statically -```php -// Good -Piper::on(['AA'])->to('strtolower')->up(); - -//Better -Piper::on(['AA'])->to('strtolower')(); -``` - - Backward Pipe with `fro()` looking like: ```php diff --git a/src/Piper.php b/src/Piper.php index 26c5511..5dd5cb3 100644 --- a/src/Piper.php +++ b/src/Piper.php @@ -15,7 +15,12 @@ public function __invoke(callable $action = null) return $this->up($action); } - public function on($value, callable $callback = null) + public static function on($value, callable $callback = null) + { + return (new static())->pipe($value, $callback); + } + + public function pipe($value, callable $callback = null) { if (!empty($this->piped)) { throw new PiperException('on() must be called only once'); diff --git a/tests/PiperTest.php b/tests/PiperTest.php index 167a27d..db1436e 100644 --- a/tests/PiperTest.php +++ b/tests/PiperTest.php @@ -16,7 +16,7 @@ public function testPipeToUp() { $piper = new Piper(); - $result = $piper->on('name') + $result = $piper->pipe('name') ->to(fn($name) => strtoupper($name)) ->up(); @@ -63,7 +63,7 @@ public function testPipeMethodCannotBeCalledMoreThanOnce() $this->expectExceptionMessage('on() must be called only once'); piper(fn() => 'NAME') - ->on('1') + ->pipe('1') ->to(fn($name) => strtolower($name)) ->up(); } @@ -149,6 +149,18 @@ public function testMakePiperInvokableWithoutCallingUpMethod() $this->assertSame('Name', $result); } + + public function testPiperOnStaticCreation() + { + $this->assertEquals( + 'n,a,m,e', + Piper::on('NAME') + ->to('str_split') + ->to('implode', ',') + ->to('strtolower') + ->up() + ); + } } class StrManipulator