PHP code example of yokai / enum-bundle

1. Go to this page and download the library: Download yokai/enum-bundle 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/ */

    

yokai / enum-bundle example snippets



return [
    Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
];



declare(strict_types=1);

namespace App\Enum;

use Yokai\EnumBundle\Enum;

class StatusEnum extends Enum
{
    public function __construct()
    {
        parent::__construct(['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
    }
}



declare(strict_types=1);

namespace App\Model;

use App\Enum\StatusEnum;
use Yokai\EnumBundle\Validator\Constraints\Enum;

class Member
{
     #[Enum(enum: StatusEnum::class)]
    public ?string $status = null;
}



declare(strict_types=1);

namespace App\Form\Type;

use App\Model\Member;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MemberType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            // Because we added the #[Enum] constraint to Member::$status property
            // the bundle will be able to find out the appropriate form type automatically
            ->add('status')
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefault('data_class', Member::class);
    }
}



declare(strict_types=1);

namespace App\Enum;

use Symfony\Contracts\Translation\TranslatorInterface;
use Yokai\EnumBundle\TranslatedEnum;

class StatusEnum extends TranslatedEnum
{
    public function __construct(TranslatorInterface $translator)
    {
        parent::__construct(['new', 'validated', 'disabled'], $translator, 'status.%s');
    }
}