Skip to content

Commit

Permalink
Adds termination to check for matching element in provided collection
Browse files Browse the repository at this point in the history
  • Loading branch information
pitchart committed Mar 4, 2020
1 parent d159b83 commit e430cc7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Reducer/Termination/Has.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


namespace Pitchart\Transformer\Reducer\Termination;


use Pitchart\Transformer\Reduced;
use Pitchart\Transformer\Reducer\Traits\HasCallback;
use Pitchart\Transformer\Termination;

class Has implements Termination
{
/**
* @var callable
*/
protected $callback;

public function __construct(callable $callback)
{
$this->callback = $callback;
}

public function init()
{
return false;
}

public function step($result, $current)
{
$callback = $this->callback;
if ($callback($current)) {
return new Reduced(true);
}

return $result;
}

public function complete($result)
{
if ($result instanceof Reduced) {
return $result->value();
}

return $result;
}

}
11 changes: 11 additions & 0 deletions src/Transducer/transducers.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,19 @@ function to_single()
return new Reducer\Termination\SingleResult();
}

/**
* @param callable $callback
*
* @return Reducer\Termination\Has
*/
function has(callable $callback)
{
return new Reducer\Termination\Has($callback);
}

/**
* @param mixed $glue
*
* @return Reducer\Termination\ToString
*/
function to_string($glue = '')
Expand Down
10 changes: 10 additions & 0 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ public function single()
return $this->terminate(t\to_single());
}

/**
* @param callable $callback
*
* @return mixed
*/
public function has(callable $callback)
{
return $this->terminate(t\has($callback));
}

/**
* @return mixed
*/
Expand Down
63 changes: 63 additions & 0 deletions tests/Reducer/Termination/HasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Pitchart\Transformer\Tests\Reducer\Termination;

use Pitchart\Transformer\Reducer;
use Pitchart\Transformer\Reducer\Termination\Has;
use PHPUnit\Framework\TestCase;
use Pitchart\Transformer\Termination;
use function Pitchart\Transformer\Tests\Fixtures\plus_one;
use Pitchart\Transformer\Transformer;

class HasTest extends TestCase
{
public function test_is_a_reducer()
{
$has = new Has(function ($item) { return $item != 0; });
self::assertInstanceOf(Reducer::class, $has);
}

public function test_is_a_termination()
{
$has = new Has(function ($item) { return $item != 0; });
self::assertInstanceOf(Termination::class, $has);
}

public function test_returns_true_if_collection_contains_matching_element()
{
$has = (new Transformer([1, 2, 3, 4]))
->has(function ($item) { return $item != 0; });

self::assertTrue($has);
}

public function test_returns_false_if_collection_does_not_contain_matching_element()
{
$has = (new Transformer([1, 2, 3, 4]))
->has(function ($item) { return $item == 0; });

self::assertFalse($has);
}

/**
* @param callable $callback
* @param bool $expected
*
* @dataProvider callableProvider
*/
public function test_applies_after_operation(callable $callback, bool $expected)
{
$has = (new Transformer([1, 2, 3, 4]))
->map(plus_one())
->has($callback);
self::assertEquals($expected, $has);
}

public function callableProvider()
{
yield from [
[function ($item) { return $item == 5; }, true],
[function ($item) { return $item == 1; }, false],
];
}
}

0 comments on commit e430cc7

Please sign in to comment.