-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnum.php
35 lines (31 loc) · 848 Bytes
/
Enum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
namespace LangleyFoxall\Helpers\Traits;
trait Enum
{
/**
* Returns an array of all constants defined within the class.
*
* @return array
*/
public static function all(): array
{
try {
$reflection = new \ReflectionClass(self::class);
return array_values($reflection->getConstants());
} catch (\ReflectionException $e) {
// This will never happen as the function can only ever be executed in a context where self::class is valid.
return [];
}
}
/**
* Returns a bool indicating whether or not a value is present in the class.
*
* @param string $value
*
* @return bool
*/
public static function valid(string $value): bool
{
return array_search($value, self::all()) !== false;
}
}