Skip to content

Commit 5183fc8

Browse files
committed
add has-method
1 parent beebad1 commit 5183fc8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Enum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ final public static function getConstants()
267267
return self::detectConstants(get_called_class());
268268
}
269269

270+
/**
271+
* Check for given value
272+
*
273+
* @param static|null|bool|int|float|string $value
274+
* @return bool
275+
*/
276+
final public static function has($value)
277+
{
278+
if ($value instanceof static && get_class($value) === get_called_class()) {
279+
return true;
280+
}
281+
282+
$class = get_called_class();
283+
$constants = self::detectConstants($class);
284+
285+
return in_array($value, $constants, true);
286+
}
287+
270288
/**
271289
* Detect all available constants by the given class
272290
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,15 @@ public function testNotUnserializable()
245245
$this->setExpectedException('LogicException');
246246
unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}");
247247
}
248+
249+
public function testHas()
250+
{
251+
$enum = EnumBasic::ONE();
252+
253+
$this->assertFalse($enum->has('invalid'));
254+
$this->assertFalse($enum->has(EnumInheritance::ONE()));
255+
$this->assertTrue($enum->has(1));
256+
$this->assertTrue($enum->has(EnumBasic::ONE()));
257+
$this->assertTrue($enum->has(EnumBasic::ONE));
258+
}
248259
}

0 commit comments

Comments
 (0)