How to uniformly manage PHP Enum?

Guanhui
Release: 2023-03-01 16:16:02
forward
2367 people have browsed it

How to uniformly manage PHP Enum?

Install

composer require fangx/php-enum
Copy after login

Create

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

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

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";}
Copy after login

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();
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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('f', 'foo'),
            new \Fangx\Enum\Definition('b', 'bar'),
        ];
    }}
Copy after login

             

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template