-
-
Notifications
You must be signed in to change notification settings - Fork 4
Version 1.3
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.
requires PHP version 5.6+
composer require phpenum/phpenum 1.3
PHPEnum contains three core enumeration classes, which are:
- Enum - tests This is a single-valued enumeration class, which is also the base class, and the enumeration class inherits this class
- ListEnum - tests This is a multi-valued enumeration class based on a single-valued enumeration class implementation that it can define any number of properties
- ArrayEnum - tests This is an implementation class of multi-value enumeration with predefined key and Value properties based on the enumeration scenario of the two most commonly used property values
The single-valued and multi-valued values mentioned here refer to the attribute values. In fact, there is no single-valued or multi-valued enumeration itself, but due to the nature of the language, there is no good way to implement it in a class.
Let's take an example of an enumeration that represents the user's gender to see how each enumeration is used and different.
ArrayEnum defined array need to pay attention to all the constant values need to be defined as an array of two elements, otherwise the system will throw an InvalidConstructException
First, we define user gender enumeration based on array enumeration, which is defined in the same way as normal constant definition:
class UserGenderEnum extends \PhpEnum\ArrayEnum
{
const MALE = [1, 'male'];
const FEMALE = [2, 'female'];
}
Once we have defined the user gender enumeration, let's take a look at what array enumeration provides for us:
-
When we want to get a male enumeration instance, we can always get the same enumeration instance through any of the following methods
UserGenderEnum::MALE() // Only the current instance is cached UserGenderEnum::ofEnumName('MALE') // All instances are cached UserGenderEnum::ofKey(1) // All instances are cached UserGenderEnum::ofValue('male') // All instances are cached
-
We can do this when we want to get an enumeration name or property value
UserGenderEnum::MALE()->name() // string(4) "MALE" UserGenderEnum::MALE()->getKey() // int(1) UserGenderEnum::MALE()->getValue() // string(4) "male"
-
When we want to compare a property, we can do this
UserGenderEnum::MALE()->enumNameEquals('MALE') // bool(true) UserGenderEnum::MALE()->keyEquals(1) // bool(true) UserGenderEnum::MALE()->valueEquals('male') // bool(true)
-
We can do this when we want to verify that the data passed through the front end is correct
UserGenderEnum::containsEnumName('MALE') // int(1) Returns the number of enumerations found UserGenderEnum::containsKey(1) // int(1) Returns the number of enumerations found UserGenderEnum::containsValue('male') // int(1) Returns the number of enumerations found
-
There may be times when we can't get a specific value to validate, for example in the Laravel validator, we need to get a string to validate, just do it
implode(',', UserGenderEnum::names()) // string(11) "MALE,FEMALE" implode(',', UserGenderEnum::getProperties('key')) // string(3) "1,2" implode(',', UserGenderEnum::getProperties('value')) // string(11) "male,female"
We've seen some of the ArrayEnum features in the above example, but you might find that the predefined property names Key and Value are not friendly to understanding what they mean in different scenarios, so you need to understand the use of the following ListEnum.
ListEnum must be to define the protected constructor listEnumConstruct when using it, and the ListEnum constant values must be an not empty array and array length must be equals with the number of constructor parameters number, otherwise the system will throw an InvalidConstructException
This time let's define the user gender enumeration based on the ListEnum, this time in addition to defining constants, we use the custom attribute name ID and name, and assign them by constructor
class UserGenderEnum extends \PhpEnum\ListEnum
{
const MALE = [1, 'male'];
const FEMALE = [2, 'female'];
private $id;
private $name;
protected function listEnumConstruct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
After we define the user gender enumeration, all the functions we have demonstrated in the ArrayEnum above will also take effect here, only we need to change the key into ID and the value into name, such as:
-
When we want to get a male enumeration instance, we can always get the same enumeration instance through any of the following methods
UserGenderEnum::MALE() UserGenderEnum::ofEnumName('MALE') UserGenderEnum::ofId(1) UserGenderEnum::ofName('male')
-
We can do this when we want to get an enumeration name or property value
UserGenderEnum::MALE()->name() // string(4) "MALE" UserGenderEnum::MALE()->getId() // int(1) UserGenderEnum::MALE()->getName() // string(4) "male"
-
When we want to compare a property, we can do this
UserGenderEnum::MALE()->enumNameEquals('MALE') // bool(true) UserGenderEnum::MALE()->idEquals(1) // bool(true) UserGenderEnum::MALE()->nameEquals('male') // bool(true)
-
We can do this when we want to verify that the data passed through the front end is correct
UserGenderEnum::containsEnumName('MALE') // int(1) Returns the number of enumerations found UserGenderEnum::containsId(1) // int(1) Returns the number of enumerations found UserGenderEnum::containsName('male') // int(1) Returns the number of enumerations found
-
We can do this when we need to get a string
implode(',', UserGenderEnum::names()) // string(11) "MALE,FEMALE" implode(',', UserGenderEnum::getProperties('id')) // string(3) "1,2" implode(',', UserGenderEnum::getProperties('name')) // string(11) "male,female"
The Enum is single-value enumeration with only the attribute name and the attribute value. The Enum does not limit the type of the attribute value, but at the same time, the attribute value is always a whole and cannot be further subdivided
This time let's define the user gender enumeration based on the base enumeration, which is the same definition as the ArrayEnum
class UserGenderEnum extends \PhpEnum\Enum
{
const MALE = [1, 'male'];
const FEMALE = [2, 'female'];
}
Once we've defined the user gender enumeration, let's take a look at what the base enumeration provides us with (note that the attribute value [1, 'male'] in the base enumeration can only be used as a whole):
-
When we want to get a male enumeration instance, we can always get the same enumeration instance through any of the following methods
UserGenderEnum::MALE() UserGenderEnum::ofEnumName('MALE') UserGenderEnum::ofEnumValue([1, 'male'])
-
We can do this when we want to get an enumeration name or property value
UserGenderEnum::MALE()->name() // string(4) "MALE" UserGenderEnum::MALE()->value() // array(2) {[0]=>int(1)[1]=>string(4) "male"}
-
When we want to compare a property, we can do this
UserGenderEnum::MALE()->enumNameEquals('MALE') // bool(true) UserGenderEnum::MALE()->enumValueEquals([1, 'male']) // bool(true)
-
We can do this when we want to verify that the data passed through the front end is correct
UserGenderEnum::containsEnumName('MALE') // int(1) Returns the number of enumerations found UserGenderEnum::containsValue([1, 'male']') // int(1) Returns the number of enumerations found
-
We can do this when we need to get a string
implode(',', UserGenderEnum::names()) // string(11) "MALE,FEMALE"
Here is the end of the document, three different types of enumeration provide different functions, according to the actual use of the scenario to choose the use