-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds termination to check for matching element in provided collection
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |