PHP code example of fangx / php-enum

1. Go to this page and download the library: Download fangx/php-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fangx / php-enum example snippets



namespace Enums;

use Fangx\Enum\AbstractEnum;

class FooEnum extends AbstractEnum
{
    const FOO = "f", __FOO = "foo";
    const BAR = "b", __BAR = "bar";
}



class FooEnum extends \Fangx\Enum\AbstractEnum
{
    const FOO = 'f', __FOO = 'foo';
    const BAR = 'b', __BAR = 'bar';
}

/**
 * ['f' => 'foo', 'b' => 'bar']
 */
FooEnum::toArray();



class FooEnum extends \Fangx\Enum\AbstractEnum
{
    const FOO = 'f', __FOO = 'foo';
    const BAR = 'b', __BAR = 'bar';
}

/**
 * "foo"
 */
FooEnum::desc('f');

/**
 * "bar"
 */
FooEnum::desc('b');


class FooFormat implements \Fangx\Enum\Contracts\Format
{
    public function parse(\Fangx\Enum\Contracts\Definition $definition): array
    {
        return [['key' => $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);

class FooFilter implements \Fangx\Enum\Contracts\Filter
{
    public function __invoke(\Fangx\Enum\Contracts\Definition $definition)
    {
        return $definition->getKey() !== 'f';
    }
}

/**
 * ['f' => 'foo']
 */
$fooFilter = new FooFilter();
$barFilter = new BarFilter();
FooEnum::toArray(null, $fooFilter, $barFilter);
FooEnum::addFilter($fooFilter)->addFilter($barFilter)->toArray(); # ^1.3


class BarEnum extends \Fangx\Enum\AbstractEnum
{
    public function all()
    {
        return [
            new \Fangx\Enum\Definition('f', 'foo'),
            new \Fangx\Enum\Definition('b', 'bar'),
        ];
    }
}


declare(strict_types=1);

namespace Fangx\Tests\Stubs;

use Fangx\Enum\WithoutDefault;

class HasDefaultFiltersEnum extends ExampleEnum
{
    public function filters()
    {
        return [
            new WithoutDefault(),
            new WithoutDefault('unknown'),
        ];
    }
}



declare(strict_types=1);

namespace Fangx\Tests\Stubs;

use Fangx\Enum\Contracts\Format;

class HasDefaultFormatEnum extends ExampleEnum
{
    public function format(): ?Format
    {
        return new CustomFormat();
    }
}

bash
composer