diff --git a/src/ArrayEnum.php b/src/ArrayEnum.php index 0ac1b72..e790678 100644 --- a/src/ArrayEnum.php +++ b/src/ArrayEnum.php @@ -17,7 +17,7 @@ abstract class ArrayEnum extends Enum * * @var integer */ - protected const LENGTH = 2; + protected static $ENUM_LENGTH = 2; /** * The enum key. @@ -69,6 +69,28 @@ public function getValue() return $this->value; } + /** + * Compare the keys to be equal. + * + * @param mixed $key + * @return bool + */ + public function keyEquals($key) + { + return $this->getKey() == $key; + } + + /** + * Compare the values to be equal. + * + * @param mixed $value + * @return bool + */ + public function valueEquals($value) + { + return $this->getValue() == $value; + } + /** * Get the array enum all keys. * @@ -100,9 +122,6 @@ public static function getValues($prefix = '') $constants = $reflectionClass->getConstants(); foreach ($constants as $key => $value) { - if($key == 'LENGTH') { - continue; - } if(empty($prefix) || strstr($key, strtoupper($prefix))) { $values[reset($value)] = end($value); } @@ -112,6 +131,48 @@ public static function getValues($prefix = '') } + /** + * Determine if the key exists. + * + * @param mixed $key + * @param string $prefix + * @return bool + */ + public static function keyExist($key, $prefix = '') + { + + $values = self::getValues($prefix); + + if(empty($values[$key])) { + return false; + } + + return true; + + } + + /** + * Determine if the value exists. + * + * @param mixed $value + * @param string $prefix + * @return bool + */ + public static function valueExist($value, $prefix = '') + { + + $values = self::getValues($prefix); + + $key = array_search($value, $values); + + if(false === $key) { + return false; + } + + return true; + + } + /** * Search the key based on the value. * @@ -154,4 +215,19 @@ public static function searchValue($key, $prefix = '') } + /** + * Get enum size. + * + * @param string $prefix + * @return int + */ + public static function getSize($prefix = '') + { + + $values = self::getValues($prefix); + + return count($values); + + } + } diff --git a/src/Enum.php b/src/Enum.php index f8f76b3..ca48cd3 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -17,7 +17,7 @@ abstract class Enum * * @var integer */ - protected const LENGTH = 0; + protected static $ENUM_LENGTH = 0; /** * Create a new enum instance. @@ -33,7 +33,7 @@ public function __construct($attributes) if(!is_array($attributes)) { throw new \InvalidArgumentException('Enum attribute only accepts array.'); } - if($this->getLength() <= self::LENGTH) { + if($this->getLength() <= self::$ENUM_LENGTH) { throw new \LengthException('Enum attribute length must be greater than zero'); } if($this->getLength() != count($attributes)) { @@ -60,7 +60,7 @@ public static function __callStatic($name, $arguments) */ public function getLength() { - return static::LENGTH; + return static::$ENUM_LENGTH; } } diff --git a/src/ListEnum.php b/src/ListEnum.php index afd602f..d1d2268 100644 --- a/src/ListEnum.php +++ b/src/ListEnum.php @@ -44,7 +44,7 @@ abstract class ListEnum extends Enum * * @var integer */ - protected const LENGTH = 1; + protected static $ENUM_LENGTH = 1; /** * The attribute key index.