diff --git a/.travis.yml b/.travis.yml index 31b8b51..c252509 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: php php: + - 7.2 + - 7.3 - 7.4 before_script: diff --git a/Dockerfile b/Dockerfile index 69a3a2f..746e68c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Set master image -FROM php:7.4-fpm-alpine +FROM php:7.2-fpm-alpine # Copy composer.lock and composer.json COPY composer.json /var/www/html/ diff --git a/README.md b/README.md index 2248faf..5bdbda2 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,11 @@ PHP Pipe function execution with values from initial call like F# - `composer require transprime-research/piper` +## Requirement +Minimum Requirement +- PHP 7.2 + +- Composer + ## Quick Usage Let us take an array and do the following: @@ -60,6 +65,7 @@ $arr = array_intersect($arr, [0 => 'ADE']); //$arr is ['ADE'] ``` +> PS: You can still use the old `function() { return v; }`, `fn()` is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php ## Other Usages diff --git a/composer.json b/composer.json index 27e315b..c400b8d 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,10 @@ } ], "require": { - "php": ">=7.4" + "php": ">=7.2" }, "require-dev": { - "phpunit/phpunit": "~8.0" + "phpunit/phpunit": ">=6.0" }, "autoload": { "psr-4": { diff --git a/src/Piper.php b/src/Piper.php index da89747..c1535d5 100644 --- a/src/Piper.php +++ b/src/Piper.php @@ -6,9 +6,9 @@ class Piper { - private array $piped = []; + private $piped = []; - private array $extraParameters = []; + private $extraParameters = []; public function __invoke(callable $action = null) { diff --git a/tests/PiperTest.php b/tests/PiperTest.php index 7447596..8b76aa9 100644 --- a/tests/PiperTest.php +++ b/tests/PiperTest.php @@ -17,7 +17,9 @@ public function testPipeToUp() $piper = new Piper(); $result = $piper->pipe('name') - ->to(fn($name) => strtoupper($name)) + ->to(function ($name) { + return strtoupper($name); + }) ->up(); $this->assertSame('NAME', $result); @@ -26,33 +28,53 @@ public function testPipeToUp() public function testClosureCanBeRunOnPipedResult() { piper('name') - ->to(fn($name) => strtoupper($name)) - ->up(fn($result) => $this->assertSame('NAME', $result)); + ->to(function ($name) { + return strtoupper($name); + }) + ->up(function ($result) { + return $this->assertSame('NAME', $result); + }); } public function testManyPipeTo() { piper('NAME') - ->to(fn($name) => strtolower($name)) - ->to(fn($name) => ucfirst($name)) - ->up(fn($result) => $this->assertSame('Name', $result)); + ->to(function ($name) { + return strtolower($name); + }) + ->to(function ($name) { + return ucfirst($name); + }) + ->up(function ($result) { + return $this->assertSame('Name', $result); + }); } public function testPipeAPipedResult() { $result = piper('NAME') - ->to(fn($name) => strtolower($name)) - ->to(fn($name) => ucfirst($name)) - ->up(fn($result) => \piper($result)->to(fn() => [$result])->up()); + ->to(function ($name) { + return strtolower($name); + }) + ->to(function ($name) { + return ucfirst($name); + }) + ->up(function ($result) { + return \piper($result)->to(function () use ($result) { + return [$result]; + })->up(); + }); $this->assertSame(['Name'], $result); } public function testPiperAcceptsClosure() { - $result = piper(fn() => 'NAME') - ->to(fn($name) => strtolower($name)) - ->up(); + $result = piper(function () { + return 'NAME'; + })->to(function ($name) { + return strtolower($name); + })->up(); $this->assertSame('name', $result); } @@ -62,9 +84,13 @@ public function testPipeMethodCannotBeCalledMoreThanOnce() $this->expectException(PiperException::class); $this->expectExceptionMessage('on() must be called only once'); - piper(fn() => 'NAME') + piper(function () { + return 'NAME'; + }) ->pipe('1') - ->to(fn($name) => strtolower($name)) + ->to(function ($name) { + return strtolower($name); + }) ->up(); } @@ -73,49 +99,74 @@ public function testPipeAndToMethodMustBeCalledBeforeUp() $this->expectException(PiperException::class); $this->expectExceptionMessage('on() must be called and to() at least once'); - piper(fn() => 'NAME') - ->up(); + piper(function () { + return 'NAME'; + })->up(); } public function testPipeMethodAcceptsCallable() { // test global method $result = piper('NAME', 'strtolower') - ->to(fn($name) => ucfirst($name)) + ->to(function ($name) { + return ucfirst($name); + }) ->up(); $this->assertSame('Name', $result); // test class method $result2 = piper('NAME', StrManipulator::class . '::strToLower') - ->to(fn($name) => ucfirst($name)) + ->to(function ($name) { + return ucfirst($name); + }) ->up(); $this->assertSame('Name', $result2); // test array class and method $result2 = piper('NAME', [StrManipulator::class, 'strToLower']) - ->to(fn($name) => ucfirst($name)) - ->up(); + ->to(function ($name) { + return ucfirst($name); + })->up(); $this->assertSame('Name', $result2); // test class object $result2 = piper('NAME', new StrManipulator()) - ->to(fn($name) => ucfirst($name)) - ->up(); + ->to(function ($name) { + return ucfirst($name); + })->up(); $this->assertSame('Name', $result2); } public function testFunctionCanBeRetrievedAtTheNextPipe() { - piper(fn() => fn() => fn() => fn() => 'ade') - ->to(fn($fn) => $fn()) //Executes the first function - ->to(fn($fn) => $fn()) //Executes the second function - ->to(fn($fn) => $fn()) //Executes the third function - ->to(fn($name) => ucfirst($name)) //Executes the fourth function - ->up(fn($result) => $this->assertSame('Ade', $result)); //manipulates the result + piper(function () { + return function () { + return function () { + return function () { + return 'ade'; + }; + }; + }; + }) + ->to(function ($fn) { + return $fn(); + }) //Executes the first function + ->to(function ($fn) { + return $fn(); + }) //Executes the second function + ->to(function ($fn) { + return $fn(); + }) //Executes the third function + ->to(function ($name) { + return ucfirst($name); + }) //Executes the fourth function + ->up(function ($result) { + $this->assertSame('Ade', $result); + }); //manipulates the result } public function testAcceptingFunctionAndParametersInToMethod() @@ -125,7 +176,11 @@ public function testAcceptingFunctionAndParametersInToMethod() $this->assertSame( 10, piper(['ade', 1]) - ->to(fn($data) => array_filter($data, static fn($val): bool => is_int($val))) + ->to(function ($data) { + return array_filter($data, static function ($val): bool { + return is_int($val); + }); + }) ->to('array_merge', [2, 3, 4]) ->to('array_values') ->to('array_sum') @@ -137,7 +192,9 @@ public function testAcceptingFunctionAndParametersInToMethod() piper(['name' => 'ade', 'hobby' => 'coding']) ->to('array_flip') ->to('array_keys') - ->to('array_map', fn($val) => strtoupper($val)) + ->to('array_map', function ($val) { + return strtoupper($val); + }) ->to('array_intersect', [0 => 'ADE'])() ); } @@ -145,7 +202,9 @@ public function testAcceptingFunctionAndParametersInToMethod() public function testMakePiperInvokableWithoutCallingUpMethod() { $result = piper('name') - ->to(fn($name) => ucfirst($name))(); + ->to(function ($name) { + return ucfirst($name); + })(); $this->assertSame('Name', $result); }