Skip to content

Commit 3a5af48

Browse files
authored
Merge pull request #105 from marc-mabe/has-value-name
added Enum::hasValue() and Enum::hasName()
2 parents aaef512 + 8320ddd commit 3a5af48

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/Enum.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -323,21 +323,43 @@ final public static function getConstants()
323323
}
324324

325325
/**
326-
* Is the given enumerator part of this enumeration
326+
* Test if the given enumerator is part of this enumeration
327327
*
328328
* @param static|null|bool|int|float|string $value
329329
* @return bool
330330
*/
331-
final public static function has($value)
331+
final public static function has($enumerator)
332332
{
333-
if ($value instanceof static && \get_class($value) === static::class) {
333+
if ($enumerator instanceof static && \get_class($enumerator) === static::class) {
334334
return true;
335335
}
336336

337+
return static::hasValue($enumerator);
338+
}
339+
340+
/**
341+
* Test if the given enumerator value is part of this enumeration
342+
*
343+
* @param null|bool|int|float|string $value
344+
* @return bool
345+
*/
346+
final public static function hasValue($value)
347+
{
337348
$constants = self::detectConstants(static::class);
338349
return \in_array($value, $constants, true);
339350
}
340351

352+
/**
353+
* Test if the given enumerator name is part of this enumeration
354+
*
355+
* @param string $name
356+
* @return bool
357+
*/
358+
final public static function hasName($name)
359+
{
360+
return \is_string($name) && \defined("static::$name");
361+
}
362+
341363
/**
342364
* Detect all public available constants of given enumeration class
343365
*

tests/MabeEnumTest/EnumTest.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,34 @@ public function testNotUnserializable()
386386
}
387387

388388
public function testHas()
389+
{
390+
$this->assertFalse(EnumBasic::has('invalid'));
391+
$this->assertFalse(EnumBasic::has(EnumInheritance::ONE()));
392+
$this->assertTrue(EnumBasic::has(EnumBasic::ONE()));
393+
$this->assertTrue(EnumBasic::has(EnumBasic::ONE));
394+
}
395+
396+
public function testHasName()
397+
{
398+
$this->assertFalse(EnumBasic::hasName(''));
399+
$this->assertFalse(EnumBasic::hasName(false));
400+
$this->assertFalse(EnumBasic::hasName(true));
401+
$this->assertFalse(EnumBasic::hasName('str'));
402+
$this->assertFalse(EnumBasic::hasName(new \stdClass()));
403+
$this->assertTrue(EnumBasic::hasName('ONE'));
404+
$this->assertTrue(EnumBasic::hasName('STR'));
405+
}
406+
407+
public function testHasValue()
389408
{
390409
$enum = EnumBasic::ONE();
391410

392-
$this->assertFalse($enum->has('invalid'));
393-
$this->assertFalse($enum->has(EnumInheritance::ONE()));
394-
$this->assertTrue($enum->has(EnumBasic::ONE()));
395-
$this->assertTrue($enum->has(EnumBasic::ONE));
411+
$this->assertFalse(EnumBasic::hasValue('invalid'));
412+
$this->assertFalse(EnumBasic::hasValue(EnumInheritance::ONE()));
413+
$this->assertFalse(EnumBasic::hasValue(EnumBasic::ONE()));
414+
$this->assertTrue(EnumBasic::hasValue(EnumBasic::ONE));
396415
}
397-
416+
398417
public function testConstVisibility()
399418
{
400419
if (PHP_VERSION_ID < 70100) {

0 commit comments

Comments
 (0)