Skip to content

Commit

Permalink
Adjust array enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
yinfuyuan committed May 19, 2020
1 parent 894a327 commit 88c0832
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
84 changes: 80 additions & 4 deletions src/ArrayEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class ArrayEnum extends Enum
*
* @var integer
*/
protected const LENGTH = 2;
protected static $ENUM_LENGTH = 2;

/**
* The enum key.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);

}

}
6 changes: 3 additions & 3 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Enum
*
* @var integer
*/
protected const LENGTH = 0;
protected static $ENUM_LENGTH = 0;

/**
* Create a new enum instance.
Expand All @@ -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)) {
Expand All @@ -60,7 +60,7 @@ public static function __callStatic($name, $arguments)
*/
public function getLength()
{
return static::LENGTH;
return static::$ENUM_LENGTH;
}

}
2 changes: 1 addition & 1 deletion src/ListEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class ListEnum extends Enum
*
* @var integer
*/
protected const LENGTH = 1;
protected static $ENUM_LENGTH = 1;

/**
* The attribute key index.
Expand Down

0 comments on commit 88c0832

Please sign in to comment.