-
-
Notifications
You must be signed in to change notification settings - Fork 3
Home
PHPEnum is an enumeration class library for PHP developers. The idea comes from Java enumeration, and using the PHP features to implement single-value enumeration and multi-value enumeration. PHPEnum runs in most PHP applications. It is easily integrated with Laravel.
If you are using an older version, you can find the correct documentation through the link below
-
Version-1.3 document requires PHP Version 5.6
-
Version-1.2 document requires PHP Version 7.1
The base class references Java enumerations completely, and if you know Java enumerations before, you can use them in the same way here.
The enumeration constant format defined in Java is the invocation of the construct parameter, the parenthesis and constructor can be omitted when there are no parameters, and the constructor corresponding to the parameter format must be provided when there are parameters.
Ex. :
// Parentheses and constructors can be omitted if there are no arguments
public enum Color {
RED, GREEN, BLUE;
}
// You must provide the correct constructor if you have a parameter
public enum Color {
RED("FF0000"), GREEN("00FF00"), BLUE("0000FF");
Color(String hexadecimal) {
}
}
PHP syntax does not allow this, so PhpEnum uses constant arrays to represent parameters in a constructor, the constructor can be omitted if there are no arguments, but the parameter array cannot be omitted, and when there are arguments, a constructor corresponding to the parameter format must be provided.
The constructor method in PhpEnum is named construct and must be decorated with protected
Ex. :
// The constructor can be omitted if there are no arguments, but the parameter array must be an empty array
class Color extends \PhpEnum\Enum
{
const RED = [];
const GREEN = [];
const BLUE = [];
}
// You must provide the correct constructor if you have a parameter
class Color extends \PhpEnum\Enum
{
const RED = ["FF0000"];
const GREEN = ["00FF00"];
const BLUE = ["0000FF"];
protected function construct(string $hexadecimal)
{
}
}
We usually need to define the getters for the properties and properties of the parameters to make them accessible
Ex. :
class Color extends \PhpEnum\Enum
{
const RED = ["FF0000"];
const GREEN = ["00FF00"];
const BLUE = ["0000FF"];
private string $hexadecimal;
protected function construct(string $hexadecimal)
{
$this->hexadecimal = $hexadecimal;
}
public function getHexadecimal(): string
{
return $this->hexadecimal;
}
}
The base class provides the following method
- name:string Returns the name of the enumeration
- ordinal:int Returns the order defined by the enumeration, starting at 0
- equals:bool Returns is it equal to another enumeration
- static values:static[] Returning all enumeration instances, using the proper prefix reduces memory overhead and returns the correct result if the same enumeration value exists, although it is not recommended to define the same value in the same enumeration
- static valueOf:static Returns an enumeration instance by enumeration name
An extended class is a subclass of the base class, so it is defined and used exactly the same way as the base class, providing more ways to use enumerations. Most of the methods provided by extended classes are related to enumeration properties, so you should properly define properties and their getters when using extended classes.
Ex. :
class Color extends \PhpEnum\PhpEnum
{
const RED = ["FF0000"];
const GREEN = ["00FF00"];
const BLUE = ["0000FF"];
private string $hexadecimal;
protected function construct(string $hexadecimal)
{
$this->hexadecimal = $hexadecimal;
}
public function getHexadecimal(): string
{
return $this->hexadecimal;
}
}
The extension class provides the following method
- get:mixed Gets a property value. Note that this method does not replace the getter for the property. Instead, when your property is decorated with private attributes, you should provide the getter for the property to ensure the correctness of the method, which is the basis of other methods (although we can also obtain the property's value through reflection, but that incurs a large memory overhead).
- enumNameEquals:bool Determines whether the name of the enumeration is equal to the specified name
- propertyEquals:bool Check whether the enumerated property value is equal to the specified property value. Note that bccomp is preferred when the property type is floating point. You need to override the scale method to set the correct decimal number and set the global effect
- static containsEnumName:bool Determines whether the enumeration name contains the specified enumeration name
- static containsProperty:int Returns the enumeration property value equal to the number of enumerations for the specified value
- static ofProperty:static Returns an enumeration instance where the enumeration property value is equal to the specified value, and an exception is thrown when the number of enumerations found is greater than 1
- static properties:array Returns an array of all enumerated values for a property
- static names:string[] Returns all enumeration names
- static count:int Returns the count of all enumerations
The extension class provides magic method triggers for Equals, contains, of, and automatically finds the correct method call when a method contains a keyword or property name.
Ex. :
var_dump(Color::RED()->hexadecimalEquals('FF0000')); // bool(true)
var_dump(Color::containsHexadecimal('FF0000')); // int(1)
var_dump(Color::ofHexadecimal('FF0000')); // object(Color)