Skip to content

Commit

Permalink
Add \Pipeline\Standard as an alias for Simple, add ExampleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 20, 2018
1 parent 714f4cb commit de8226d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
59 changes: 50 additions & 9 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
// }
22 changes: 22 additions & 0 deletions src/Pipeline/Standard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*
* Copyright 2017 Alexey Kopytko <alexey@kopytko.com>
*
* 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
{
}
40 changes: 40 additions & 0 deletions tests/Pipeline/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* Copyright 2017 Alexey Kopytko <alexey@kopytko.com>
*
* 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);
}
}

0 comments on commit de8226d

Please sign in to comment.