Skip to content

Commit

Permalink
Merge pull request #50 from nreynis/feature/add-includes
Browse files Browse the repository at this point in the history
Add includes (in_array)
  • Loading branch information
florianeckerstorfer authored May 18, 2020
2 parents 5152d0f + eeb9b70 commit 005a62e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ $chain->reduce(function ($current, $value) {
- `->countValues()`
- `->every(callable)`
- `->first()`
- `->includes(mixed[, array])`
- `->join([$glue])`
- `->last()`
- `->reduce()`
Expand Down
2 changes: 2 additions & 0 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cocur\Chain\Link\Find;
use Cocur\Chain\Link\FlatMap;
use Cocur\Chain\Link\Flip;
use Cocur\Chain\Link\Includes;
use Cocur\Chain\Link\Intersect;
use Cocur\Chain\Link\IntersectAssoc;
use Cocur\Chain\Link\IntersectKey;
Expand Down Expand Up @@ -63,6 +64,7 @@ class Chain extends AbstractChain implements Countable
use First;
use FlatMap;
use Flip;
use Includes;
use Intersect;
use IntersectAssoc;
use IntersectKey;
Expand Down
28 changes: 28 additions & 0 deletions src/Link/Includes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Cocur\Chain\Link;

/**
* Includes.
*
* @author Nicolas Reynis
*/
trait Includes
{
/**
* Returns `true` if the given `needle` is in the array or `false` otherwise.
*
* @param $needle
* @param array $options options, including `strict` to also check the type
*
* @return bool
*/
public function includes($needle, array $options = []): bool
{
if (!empty($options['strict'])) {
return in_array($needle, $this->array, $options['strict']);
} else {
return in_array($needle, $this->array);
}
}
}
1 change: 1 addition & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function chainHasTraits(): void
$this->assertTrue(method_exists($c, 'first'));
$this->assertTrue(method_exists($c, 'flatMap'));
$this->assertTrue(method_exists($c, 'flip'));
$this->assertTrue(method_exists($c, 'includes'));
$this->assertTrue(method_exists($c, 'intersect'));
$this->assertTrue(method_exists($c, 'intersectAssoc'));
$this->assertTrue(method_exists($c, 'intersectKey'));
Expand Down
38 changes: 38 additions & 0 deletions tests/Link/IncludesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Cocur\Chain\Link;

/**
* IncludesTest.
*
* @author Nicolas Reynis
* @group unit
*/
class IncludesTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @covers \Cocur\Chain\Link\Includes::includes()
*/
public function includesFindElement(): void
{
/** @var Includes $mock */
$mock = $this->getMockForTrait(Includes::class);
$mock->array = ['foobar', 'bar'];
$this->assertTrue($mock->includes('bar'));
$this->assertFalse($mock->includes('baz'));
}

/**
* @test
* @covers \Cocur\Chain\Link\Includes::includes()
*/
public function includesFindElementStrictly(): void
{
/** @var Includes $mock */
$mock = $this->getMockForTrait(Includes::class);
$mock->array = ['42', '43'];
$this->assertTrue($mock->includes('42', ['strict' => true]));
$this->assertFalse($mock->includes(42, ['strict' => true]));
}
}

0 comments on commit 005a62e

Please sign in to comment.