Skip to content

Commit

Permalink
Merge pull request #1 from corporealshift/filter-isa
Browse files Browse the repository at this point in the history
Add filterIsA and filterIsOneOf
  • Loading branch information
treyhyde authored Sep 13, 2017
2 parents 94e644f + edb1c73 commit 23f637c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "4.7.*"
"phpunit/phpunit": "5.7.*"
},
"autoload": {
"psr-0": { "PhpOption\\": "src/" }
Expand Down
10 changes: 10 additions & 0 deletions src/PhpOption/LazyOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public function filterNot($callable)
return $this->option()->filterNot($callable);
}

public function filterIsA($class)
{
return $this->option()->filterIsA($class);
}

public function filterIsOneOf(...$classes)
{
return $this->option()->filterIsOneOf($classes);
}

public function select($value)
{
return $this->option()->select($value);
Expand Down
10 changes: 10 additions & 0 deletions src/PhpOption/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public function filterNot($callable)
return $this;
}

public function filterIsA($class)
{
return $this;
}

public function filterIsOneOf(...$classes)
{
return $this;
}

public function select($value)
{
return $this;
Expand Down
23 changes: 23 additions & 0 deletions src/PhpOption/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,29 @@ abstract public function filter($callable);
*/
abstract public function filterNot($callable);

/**
* Check if the option is an instanceof or subclass of the given parameter
*
* @param string $class
*
* @return Option
*/
abstract public function filterIsA($class);

/**
* Check if the option is an instanceof or subclass of any of the given parameters.
* If the first parameter is an array, all elements of the array are checked instead.
* Example:
* $opt = Option::fromValue(new stdClass());
* $opt->filterIsOneOf("unknown_type", stdClass::class) == true
* $opt->filterIsOneOf(["unknown_type", stdClass::class]) == true
*
* @param string $classes
*
* @return Option
*/
abstract public function filterIsOneOf(...$classes);

/**
* If the option is empty, it is returned immediately.
*
Expand Down
28 changes: 28 additions & 0 deletions src/PhpOption/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ public function filterNot($callable)
return None::create();
}

public function filterIsA($class)
{
if (is_a($this->value, $class)) {
return $this;
}

return None::create();
}

public function filterIsOneOf(...$classes)
{
if (count($classes) < 1) {
return None::create();
}

if (is_array($classes[0]) || $classes[0] instanceof \Traversable) {
$classes = $classes[0];
}

foreach($classes as $class) {
if (is_a($this->value, $class)) {
return $this;
}
}

return None::create();
}

public function select($value)
{
if ($this->value === $value) {
Expand Down
11 changes: 11 additions & 0 deletions tests/PhpOption/Tests/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function testFilterNot()
}));
}

public function testFilterIsA()
{
$this->assertSame($this->none, $this->none->filterIsA(stdClass::class));
}

public function testFilterIsOneOf()
{
$this->assertSame($this->none, $this->none->filterIsOneOf(stdClass::class));
$this->assertSame($this->none, $this->none->filterIsOneOf([stdClass::class]));
}

public function testSelect()
{
$this->assertSame($this->none, $this->none->select(null));
Expand Down
20 changes: 20 additions & 0 deletions tests/PhpOption/Tests/SomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ public function testFilterNot()
$this->assertSame($some, $some->filterNot(function($v) { return strlen($v) === 0; }));
}

public function testFilterIsA()
{
$some = new Some(new \stdClass());

$this->assertInstanceOf('PhpOption\None', $some->filterIsA('unknown'));
$this->assertSame($some, $some->filterIsA(\stdClass::class));
}

public function testFilterIsOneOf()
{
$some = new Some(new \stdClass());

$this->assertInstanceOf('PhpOption\None', $some->filterIsOneOf('unknown', 'unknown2'));
$this->assertInstanceOf('PhpOption\None', $some->filterIsOneOf(['unknown', 'unknown2']));

$this->assertSame($some, $some->filterIsOneOf(\stdClass::class, 'unknown'));
$this->assertSame($some, $some->filterIsOneOf([\stdClass::class, 'unknown']));
$this->assertSame($some, $some->filterIsOneOf(new \ArrayIterator([\stdClass::class, 'unknown'])));
}

public function testSelect()
{
$some = new Some('foo');
Expand Down

0 comments on commit 23f637c

Please sign in to comment.