From de8226dfd09a7ed9f78ea0851741815bf9164efc Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Sun, 21 Oct 2018 00:26:51 +0900 Subject: [PATCH] Add \Pipeline\Standard as an alias for Simple, add ExampleTest --- example.php | 59 ++++++++++++++++++++++++++++------ src/Pipeline/Standard.php | 22 +++++++++++++ tests/Pipeline/ExampleTest.php | 40 +++++++++++++++++++++++ 3 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 src/Pipeline/Standard.php create mode 100644 tests/Pipeline/ExampleTest.php diff --git a/example.php b/example.php index aca79ba..e85e301 100644 --- a/example.php +++ b/example.php @@ -17,28 +17,38 @@ include 'vendor/autoload.php'; -$pipeline = new \Pipeline\Simple(); +$pipeline = new \Pipeline\Standard(); +// initial generator $pipeline->map(function () { foreach (range(1, 3) as $i) { yield $i; } }); -$pipeline->map(function ($i) { - yield pow($i, 2); - yield pow($i, 3); +// next processing step +$pipeline->map(function ($value) { + yield $value ** 2; + yield $value ** 3; }); +// simple one-to-one mapper $pipeline->map(function ($i) { yield $i - 1; }); +// one-to-many generator $pipeline->map(function ($i) { - yield $i * 2; - yield $i * 4; + yield [$i, 2]; + yield [$i, 4]; +}); + +// mapper with arguments unpacked from an input array +$pipeline->unpack(function ($i, $j) { + yield $i * $j; }); +// one way to filter $pipeline->map(function ($i) { if ($i > 50) { yield $i; @@ -49,9 +59,40 @@ return $i > 100; }); -$value = $pipeline->reduce(function ($a, $b) { - return $a + $b; +// reduce to a single value; can be an array or any value +$value = $pipeline->reduce(function ($carry, $item) { + // for the sake of convenience the default reducer from the simple pipeline does summation, just like we do here + return $carry + $item; }, 0); -// int(104) var_dump($value); +// int(104) + +// Now an example for toArray() +$pipeline = new \Pipeline\Standard(); + +// Yields [0 => 1, 1 => 2] +$pipeline->map(function () { + yield 1; + yield 2; +}); + +// For each value yields [0 => $i + 1, 1 => $i + 2] +$pipeline->map(function ($i) { + yield $i + 1; + yield $i + 2; +}); + +$arrayResult = $pipeline->toArray(); +var_dump($arrayResult); +// Since keys are discarded we get: +// array(4) { +// [0] => +// int(2) +// [1] => +// int(3) +// [2] => +// int(3) +// [3] => +// int(4) +// } diff --git a/src/Pipeline/Standard.php b/src/Pipeline/Standard.php new file mode 100644 index 0000000..df4a21c --- /dev/null +++ b/src/Pipeline/Standard.php @@ -0,0 +1,22 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Pipeline; + +final class Standard extends Simple +{ +} diff --git a/tests/Pipeline/ExampleTest.php b/tests/Pipeline/ExampleTest.php new file mode 100644 index 0000000..b982b93 --- /dev/null +++ b/tests/Pipeline/ExampleTest.php @@ -0,0 +1,40 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Tests\Pipeline; + +use PHPUnit\Framework\TestCase; + +/** + * @coversNothing + */ +class ExampleTest extends TestCase +{ + public function testExample() + { + // These variables will be set inside example.php + $value = null; + $arrayResult = null; + + ob_start(); + include 'example.php'; + ob_end_clean(); + + $this->assertSame(104, $value); + $this->assertSame([2, 3, 3, 4], $arrayResult); + } +}