Skip to content

Instantly share code, notes, and snippets.

@Samorai
Last active May 29, 2020 12:31
Show Gist options
  • Save Samorai/3cf3aba60dbd8ac927ca0dd9ad998a2f to your computer and use it in GitHub Desktop.
Save Samorai/3cf3aba60dbd8ac927ca0dd9ad998a2f to your computer and use it in GitHub Desktop.
Enums and constants
<?php
namespace Samorai\Utils\Enum;
abstract class Enum
{
/**
* Enum value
* @var mixed
*/
public $value;
/**
* Constants cache.
*
* @var array
*/
protected static $constCacheArray = [];
/**
* Enum constructor.
* @param $enumValue
* @throws \ReflectionException
* @throws \Samorai\Utils\Enum\InvalidEnumMemberException
*/
public function __construct($enumValue)
{
if (!static::hasValue($enumValue)) {
throw new InvalidEnumMemberException($enumValue, $this);
}
$this->value = $enumValue;
}
/**
* @return string
*/
public function __toString(): string
{
return (string)$this->value;
}
/**
* @param $value
* @param bool $strict
* @return bool
* @throws \ReflectionException
*/
public static function hasValue($value, bool $strict = true): bool
{
$validValues = static::getValues();
if ($strict) {
return in_array($value, $validValues, true);
}
return in_array((string)$value, array_map('strval', $validValues), true);
}
/**
* @return array
* @throws \ReflectionException
*/
public static function getValues(): array
{
return array_values(static::getConstants());
}
/**
* @return array
* @throws \ReflectionException
*/
protected static function getConstants(): array
{
$calledClass = get_called_class();
if (!array_key_exists($calledClass, static::$constCacheArray)) {
$reflect = new \ReflectionClass($calledClass);
static::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return static::$constCacheArray[$calledClass];
}
/**
* @param $value
* @return string
* @throws \ReflectionException
*/
public static function getKey($value): string
{
return array_search($value, static::getConstants(), true);
}
/**
* @return array
* @throws \ReflectionException
*/
public static function toArray(): array
{
return static::getConstants();
}
/**
* @param string $key
* @return bool
* @throws \ReflectionException
*/
public static function hasKey(string $key): bool
{
return in_array($key, static::getKeys(), true);
}
/**
* @return array
* @throws \ReflectionException
*/
public static function getKeys(): array
{
return array_keys(static::getConstants());
}
/**
* @param string $key
* @return mixed
* @throws \ReflectionException
*/
public static function getValue(string $key)
{
return static::getConstants()[$key];
}
}
<?php
namespace Samorai\Utils\Enum;
final class Example extends Enum
{
public const CONSTANT = 'Y';
public const SECOND_CONSTANT = 'W';
public const THIRD_CONSTANT = 'C';
}
<?php
namespace Samorai\Utils\Enum;
use Exception;
class InvalidEnumMemberException extends Exception
{
/**
* Create an InvalidEnumMemberException.
*
* @param mixed $invalidValue
* @param Enum $enum
* @throws \ReflectionException
*/
public function __construct($invalidValue, Enum $enum)
{
$invalidValueType = gettype($invalidValue);
$enumValues = implode(', ', $enum::getValues());
$enumClassName = class_basename($enum);
parent::__construct(
sprintf(
'Cannot construct an instance of %s using the value (%s) `%s`. Possible values are [%s].',
$enumClassName,
$invalidValueType,
$invalidValue,
$enumValues
)
);
}
}
<?php
Example::getKeys();
Example::getValues();
Example::hasKey($key);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment