phpvms/app/Contracts/Enum.php

175 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Contracts;
/**
* Borrowed some ideas from myclabs/php-enum after this was created
*/
abstract class Enum
{
protected static array $cache = [];
protected static array $codes = [];
protected static array $labels = [];
/**
2018-08-27 00:40:04 +08:00
* @var int
*/
protected int $value;
/**
* Create an instance of this Enum
2018-08-27 00:40:04 +08:00
*
* @param $val
*/
public function __construct($val)
{
$this->value = $val;
}
/**
* Return the value that's been set if this is an instance
2018-08-27 00:40:04 +08:00
*
* @return int|null
*/
final public function getValue(): ?int
{
return $this->value;
}
/**
* Return the label, try to return the translated version as well
2018-08-27 00:40:04 +08:00
*
* @param $value
2018-08-27 00:40:04 +08:00
*
* @return string
*/
final public static function label($value): string
{
if (isset(static::$labels[$value])) {
$val = static::$labels[$value];
if (strpos($val, '.') !== false) {
return trans($val);
}
return $val;
}
return $value;
}
/**
* Return all of the (translated) labels
*/
final public static function labels(): array
{
$labels = [];
foreach (static::$labels as $key => $label) {
$labels[$key] = trans($label);
}
return $labels;
}
2018-03-23 06:17:37 +08:00
/**
* Get the numeric value from a string code
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @param $code
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @return mixed|null
*/
public static function getFromCode($code)
{
return array_search($code, static::$codes, true);
2018-03-23 06:17:37 +08:00
}
/**
* Convert the integer value into one of the codes
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @param $value
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @return false|int|string
*/
public static function convertToCode($value): bool|int|string|null
2018-03-23 06:17:37 +08:00
{
$value = (int) $value;
if (!array_key_exists($value, static::$codes)) {
return null;
}
return static::$codes[$value];
2018-03-23 06:17:37 +08:00
}
/**
* Select box entry items
2018-08-27 00:40:04 +08:00
*
* @param bool $add_blank
*
* @return array
*/
public static function select($add_blank = false): array
{
$labels = [];
if ($add_blank) {
$labels[] = '';
}
foreach (static::$labels as $key => $label) {
$labels[$key] = trans($label);
}
return $labels;
}
/**
* Returns all possible values as an array
2018-08-27 00:40:04 +08:00
*
* @throws \ReflectionException
2018-08-27 00:40:04 +08:00
*
* @return array Constant name in key, constant value in value
*/
public static function toArray(): array
{
$class = static::class;
if (!array_key_exists($class, static::$cache)) {
$reflection = new \ReflectionClass($class);
static::$cache[$class] = $reflection->getConstants();
}
return static::$cache[$class];
}
/**
* @param Enum $enum
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
2018-08-27 00:40:04 +08:00
final public function equals(self $enum): bool
{
2018-08-27 02:50:08 +08:00
return $this->getValue() === $enum->getValue() && static::class === \get_class($enum);
}
/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a
* class constant
2018-08-27 00:40:04 +08:00
*
* @param string $name
* @param array $arguments
2018-08-27 00:40:04 +08:00
*
* @throws \BadMethodCallException
* @throws \ReflectionException
2018-08-27 00:40:04 +08:00
*
* @return static
*/
public static function __callStatic($name, $arguments)
{
$array = static::toArray();
if (isset($array[$name])) {
return new static($array[$name]);
}
2018-08-27 00:40:04 +08:00
throw new \BadMethodCallException(
2018-08-27 02:50:08 +08:00
"No static method or enum constant '$name' in class ".static::class
);
}
}