Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includes (in_array) #50

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ $chain->reduce(function ($current, $value) {
- `->count()`
- `->countValues()`
- `->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 @@ -12,6 +12,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 @@ -60,6 +61,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 @@ -86,6 +86,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]));
}
}