Skip to content

Commit 7fcdcde

Browse files
prolicmarc-mabe
authored andcommitted
add has-method
1 parent ff71f6d commit 7fcdcde

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/MabeEnum/Enum.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,27 @@ final public static function getConstants()
258258
return self::detectConstants(get_called_class());
259259
}
260260

261+
/**
262+
* Check for given value
263+
*
264+
* @param static|null|bool|int|float|string $value
265+
* @return bool
266+
*/
267+
final public static function has($value)
268+
{
269+
if ($value instanceof static) {
270+
if (get_class($value) === get_called_class()) {
271+
return true;
272+
}
273+
$value = $value->getValue();
274+
}
275+
276+
$class = get_called_class();
277+
$constants = self::detectConstants($class);
278+
279+
return in_array($value, $constants, true);
280+
}
281+
261282
/**
262283
* Detect constants available by given class
263284
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,13 @@ public function testNotUnserializable()
238238
$this->setExpectedException('LogicException');
239239
unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}");
240240
}
241+
242+
public function testHas()
243+
{
244+
$enum = EnumBasic::ONE();
245+
$this->assertFalse($enum->has('invalid'));
246+
$this->assertTrue($enum->has(EnumInheritance::ONE()));
247+
$this->assertTrue($enum->has(EnumBasic::ONE()));
248+
$this->assertTrue($enum->has(EnumBasic::ONE));
249+
}
241250
}

0 commit comments

Comments
 (0)