Skip to content

Commit

Permalink
Added shortcuts for input arrays. Made __invoke() final to avoid ambi…
Browse files Browse the repository at this point in the history
…guity.
  • Loading branch information
sanmai committed Mar 28, 2018
1 parent f67ee13 commit 3d774ae
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 47 deletions.
19 changes: 13 additions & 6 deletions src/Pipeline/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ private static function apply($previous, callable $func): \Generator
public function filter(callable $func)
{
if (!$this->pipeline) {
// No-op: either an empty array or null
// No-op: either an empty array or null.
return $this;
}

// We got an array, that's what we need. Moving along.
if (is_array($this->pipeline)) {
$this->pipeline = array_filter($this->pipeline, $func);

return $this;
}

// Got iterator. No other choice but this.
$this->pipeline = new \CallbackFilterIterator($this->pipeline, $func);

return $this;
Expand Down Expand Up @@ -151,12 +159,11 @@ public function reduce(callable $func, $initial = null)
}

/**
* Convinience method to allow pipeline to be used as an argument to map().
*
* Must not take any arguments whatsoever: therefore final.
* Convinience method to allow pipeline pass for a callable, used in map().
* Shall be used for only the above reason: therefore final.
* Must not take any arguments whatsoever. Returns nothing.
*/
final public function __invoke(callable $func)
final public function __invoke()
{
return $this->map($func);
}
}
69 changes: 69 additions & 0 deletions tests/Pipeline/ArraysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* Copyright 2017, 2018 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.
*/

declare(strict_types=1);

namespace Pipeline;

use PHPUnit\Framework\TestCase;

/**
* @covers \Pipeline\Standard
* @covers \Pipeline\Principal
*/
class ArraysTest extends TestCase
{
public function testInitialCallbackNotGenerator()
{
$pipeline = new Standard();
$pipeline->map(function () {
return PHP_INT_MAX;
});

$this->assertEquals([PHP_INT_MAX], iterator_to_array($pipeline));
}

public function testArrayToArray()
{
$pipeline = new Standard();
$pipeline->map(function () {
return 42;
});

$this->assertEquals([42], $pipeline->toArray());
}

public function testArrayFilter()
{
$pipeline = new Standard();
$pipeline->map(function () {
return false;
})->filter()->filter();

$this->assertEquals([], $pipeline->toArray());
}

public function testArrayReduce()
{
$pipeline = new Standard();
$pipeline->map(function () {
return 3;
});

$this->assertSame(3, $pipeline->reduce());
}
}
36 changes: 16 additions & 20 deletions tests/Pipeline/EdgeCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@
*/
class EdgeCasesTest extends TestCase
{
public function testInitialCallbackNotGenerator()
{
$pipeline = new Standard();
$pipeline->map(function () {
return PHP_INT_MAX;
});

$this->assertEquals([PHP_INT_MAX], iterator_to_array($pipeline));
}

public function testStandardStringFunctions()
{
$pipeline = new Standard(new \ArrayIterator([1, 2, 'foo', 'bar']));
Expand Down Expand Up @@ -104,9 +94,9 @@ public function testIteratorIterator()
$this->assertEquals(42, $this->firstValueFromIterator($iterator));
}

public function testIteratorToArrayWithSameKeys()
private function pipelineWithNonUniqueKeys(): Standard
{
$pipeline = new \Pipeline\Standard();
$pipeline = new Standard();
$pipeline->map(function () {
yield 1;
yield 2;
Expand All @@ -117,7 +107,17 @@ public function testIteratorToArrayWithSameKeys()
yield $i + 2;
});

$this->assertEquals([3, 4], iterator_to_array($pipeline));
return $pipeline;
}

public function testIteratorToArrayWithSameKeys()
{
$this->assertEquals([3, 4], iterator_to_array($this->pipelineWithNonUniqueKeys()));
}

public function testIteratorToArrayWithAllValues()
{
$this->assertEquals([2, 3, 3, 4], $this->pipelineWithNonUniqueKeys()->toArray());
}

public function testPointlessReplace()
Expand Down Expand Up @@ -153,19 +153,15 @@ public function testPipelineInPipelineUsesSelf()
$this->assertSame([3, 5, 7, 11], $pipeline->toArray());
}

public function testArrayReduce()
public function testPipelineInvokeReturnsVoid()
{
$pipeline = new Standard();
$pipeline->map(function () {
return 3;
});

$this->assertSame(3, $pipeline->reduce());
$this->assertNull($pipeline());
}

public function testInvokeMaps()
{
$pipeline = new \Pipeline\Standard(new \ArrayIterator(range(1, 5)));
$pipeline = new Standard(new \ArrayIterator(range(1, 5)));
$pipeline->map($this);

$this->assertEquals(range(1, 5), iterator_to_array($pipeline));
Expand Down
6 changes: 6 additions & 0 deletions tests/Pipeline/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ public function testInvalidInitialGeneratorWithArguments()
$this->fail('Shall never be called');
});
}

public function __invoke()
{
// Like the standard pipeline does
return $this;
}
}
24 changes: 3 additions & 21 deletions tests/Pipeline/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ public function testPipelineInPipeline()
});

$this->assertEquals(3 + 5 + 7 + 11, $pipeline2->reduce());
}

public function testPipelineReadsFromPipeline()
{
$foo = new Standard();
$foo->map(function () {
yield 1;
Expand Down Expand Up @@ -255,25 +258,4 @@ public function testMethodChaining()

$this->assertEquals([52, 104], iterator_to_array($pipeline));
}

public function testInvokeMaps()
{
$pipeline = new Standard(new \ArrayIterator(range(1, 3)));

$pipeline(function ($i) {
yield pow($i, 2);
yield pow($i, 3);
})(function ($i) {
return $i - 1;
})(function ($i) {
yield $i * 2;
yield $i * 4;
})(function ($i) {
if ($i > 50) {
yield $i;
}
});

$this->assertEquals([52, 104], $pipeline->toArray());
}
}

0 comments on commit 3d774ae

Please sign in to comment.