search
HomeBackend DevelopmentPHP ProblemHow to uniformly manage PHP Enum?

How to uniformly manage PHP Enum?

Jun 19, 2020 pm 06:04 PM
enumphp

How to uniformly manage PHP Enum?

Install

composer require fangx/php-enum

Create

Use the ./vendor/bin/enum command to create an enumeration class.

./vendor/bin/enum FooEnum --enum="1=foo" --enum="b=bar" --path=Enums

This command creates a FooEnum.php in the Enums directory of the current directory by default. File. The file content is as follows:

<?phpnamespace  Enums;use Fangx\Enum\AbstractEnum;class FooEnum extends AbstractEnum{
    const FOO = "f", __FOO = "foo";
    const BAR = "b", __BAR = "bar";}

Use

enumeration class to inherit by default \Fangx\Enum\AbstractEnum. The following methods can be called statically:

  • toArray(Format $format = null, Filter $filter = null)
  • toJson(Format $format = null, Filter $filter = null)
  • desc($key, $default = 'Undefined')

Get all enumeration values

<?phpclass  FooEnum extends \Fangx\Enum\AbstractEnum{
    const FOO = &#39;f&#39;, __FOO = &#39;foo&#39;;
    const BAR = &#39;b&#39;, __BAR = &#39;bar&#39;;}/**
 * [&#39;f&#39; => 'foo', 'b' => 'bar']
 */FooEnum::toArray();

Get the description information of the enumeration value

<?phpclass  FooEnum extends \Fangx\Enum\AbstractEnum{
    const FOO = &#39;f&#39;, __FOO = &#39;foo&#39;;
    const BAR = &#39;b&#39;, __BAR = &#39;bar&#39;;}/**
 * "foo"
 */FooEnum::desc(&#39;f&#39;);/**
 * "bar"
 */FooEnum::desc(FooEnum::BAR);

Use format to constrain return values

<?phpclass  FooFormat implements \Fangx\Enum\Contracts\Format{
    public function parse(\Fangx\Enum\Contracts\Definition $definition): array
    {
        return [[&#39;key&#39; => $definition->getKey() , 'value' => $definition->getValue()]];
    }}class FooEnum extends \Fangx\Enum\AbstractEnum{
    const FOO = 'f', __FOO = 'foo';
    const BAR = 'b', __BAR = 'bar';}/**
 * [['key' => 'f', 'value' => 'foo'], ['key' => 'b', 'value' => 'bar'],]
 */$format = new FooFormat();FooEnum::toArray($format);

Use rules to filter enumeration values.

class FooFilter implements \Fangx\Enum\Contracts\Filter{
    public function __invoke(\Fangx\Enum\Contracts\Definition $definition)
    {
        return $definition->getKey() === 'f';
    }}/**
 * ['f' => 'foo']
 */$filter = new FooFilter();FooEnum::toArray(null, $filter);

Use custom collections as all The enumeration type, other usage methods are consistent with FooEnum.

<?phpclass  BarEnum extends \Fangx\Enum\AbstractEnum{
    public function all()
    {
        return [
            new \Fangx\Enum\Definition(&#39;f&#39;, &#39;foo&#39;),
            new \Fangx\Enum\Definition(&#39;b&#39;, &#39;bar&#39;),
        ];
    }}

             

Recommended tutorial: "PHP

The above is the detailed content of How to uniformly manage PHP Enum?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor