Skip to content

Commit 23f637c

Browse files
authored
Merge pull request #1 from corporealshift/filter-isa
Add filterIsA and filterIsOneOf
2 parents 94e644f + edb1c73 commit 23f637c

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=5.3.0"
14+
"php": ">=5.6.0"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "4.7.*"
17+
"phpunit/phpunit": "5.7.*"
1818
},
1919
"autoload": {
2020
"psr-0": { "PhpOption\\": "src/" }

src/PhpOption/LazyOption.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ public function filterNot($callable)
126126
return $this->option()->filterNot($callable);
127127
}
128128

129+
public function filterIsA($class)
130+
{
131+
return $this->option()->filterIsA($class);
132+
}
133+
134+
public function filterIsOneOf(...$classes)
135+
{
136+
return $this->option()->filterIsOneOf($classes);
137+
}
138+
129139
public function select($value)
130140
{
131141
return $this->option()->select($value);

src/PhpOption/None.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public function filterNot($callable)
101101
return $this;
102102
}
103103

104+
public function filterIsA($class)
105+
{
106+
return $this;
107+
}
108+
109+
public function filterIsOneOf(...$classes)
110+
{
111+
return $this;
112+
}
113+
104114
public function select($value)
105115
{
106116
return $this;

src/PhpOption/Option.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,29 @@ abstract public function filter($callable);
290290
*/
291291
abstract public function filterNot($callable);
292292

293+
/**
294+
* Check if the option is an instanceof or subclass of the given parameter
295+
*
296+
* @param string $class
297+
*
298+
* @return Option
299+
*/
300+
abstract public function filterIsA($class);
301+
302+
/**
303+
* Check if the option is an instanceof or subclass of any of the given parameters.
304+
* If the first parameter is an array, all elements of the array are checked instead.
305+
* Example:
306+
* $opt = Option::fromValue(new stdClass());
307+
* $opt->filterIsOneOf("unknown_type", stdClass::class) == true
308+
* $opt->filterIsOneOf(["unknown_type", stdClass::class]) == true
309+
*
310+
* @param string $classes
311+
*
312+
* @return Option
313+
*/
314+
abstract public function filterIsOneOf(...$classes);
315+
293316
/**
294317
* If the option is empty, it is returned immediately.
295318
*

src/PhpOption/Some.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ public function filterNot($callable)
117117
return None::create();
118118
}
119119

120+
public function filterIsA($class)
121+
{
122+
if (is_a($this->value, $class)) {
123+
return $this;
124+
}
125+
126+
return None::create();
127+
}
128+
129+
public function filterIsOneOf(...$classes)
130+
{
131+
if (count($classes) < 1) {
132+
return None::create();
133+
}
134+
135+
if (is_array($classes[0]) || $classes[0] instanceof \Traversable) {
136+
$classes = $classes[0];
137+
}
138+
139+
foreach($classes as $class) {
140+
if (is_a($this->value, $class)) {
141+
return $this;
142+
}
143+
}
144+
145+
return None::create();
146+
}
147+
120148
public function select($value)
121149
{
122150
if ($this->value === $value) {

tests/PhpOption/Tests/NoneTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public function testFilterNot()
9292
}));
9393
}
9494

95+
public function testFilterIsA()
96+
{
97+
$this->assertSame($this->none, $this->none->filterIsA(stdClass::class));
98+
}
99+
100+
public function testFilterIsOneOf()
101+
{
102+
$this->assertSame($this->none, $this->none->filterIsOneOf(stdClass::class));
103+
$this->assertSame($this->none, $this->none->filterIsOneOf([stdClass::class]));
104+
}
105+
95106
public function testSelect()
96107
{
97108
$this->assertSame($this->none, $this->none->select(null));

tests/PhpOption/Tests/SomeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ public function testFilterNot()
8888
$this->assertSame($some, $some->filterNot(function($v) { return strlen($v) === 0; }));
8989
}
9090

91+
public function testFilterIsA()
92+
{
93+
$some = new Some(new \stdClass());
94+
95+
$this->assertInstanceOf('PhpOption\None', $some->filterIsA('unknown'));
96+
$this->assertSame($some, $some->filterIsA(\stdClass::class));
97+
}
98+
99+
public function testFilterIsOneOf()
100+
{
101+
$some = new Some(new \stdClass());
102+
103+
$this->assertInstanceOf('PhpOption\None', $some->filterIsOneOf('unknown', 'unknown2'));
104+
$this->assertInstanceOf('PhpOption\None', $some->filterIsOneOf(['unknown', 'unknown2']));
105+
106+
$this->assertSame($some, $some->filterIsOneOf(\stdClass::class, 'unknown'));
107+
$this->assertSame($some, $some->filterIsOneOf([\stdClass::class, 'unknown']));
108+
$this->assertSame($some, $some->filterIsOneOf(new \ArrayIterator([\stdClass::class, 'unknown'])));
109+
}
110+
91111
public function testSelect()
92112
{
93113
$some = new Some('foo');

0 commit comments

Comments
 (0)