-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from nreynis/feature/add-includes
Add includes (in_array)
- Loading branch information
Showing
5 changed files
with
70 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
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,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); | ||
} | ||
} | ||
} |
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,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])); | ||
} | ||
} |