src/Backend/Modules/Editions/Domain/Registration/LegalFormType.php line 10

Open in your IDE?
  1. <?php
  2. namespace Backend\Modules\Editions\Domain\Registration;
  3. use Symfony\Component\Form\CallbackTransformer;
  4. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\OptionsResolver\OptionsResolver;
  7. class LegalFormType extends ChoiceType
  8. {
  9.     public function buildForm(FormBuilderInterface $builder, array $options): void
  10.     {
  11.         parent::buildForm($builder$options);
  12.         $builder
  13.             ->addModelTransformer(
  14.                 new CallbackTransformer(
  15.                     static function (?LegalForm $legalForm): ?string {
  16.                         if (!$legalForm instanceof LegalForm) {
  17.                             return null;
  18.                         }
  19.                         return (string) $legalForm;
  20.                     },
  21.                     static function (?string $legalForm): ?LegalForm {
  22.                         if ($legalForm === null) {
  23.                             return null;
  24.                         }
  25.                         try {
  26.                             return new LegalForm($legalForm);
  27.                         } catch (InvalidLegalForm $exception) {
  28.                             return null;
  29.                         }
  30.                     }
  31.                 )
  32.             )
  33.         ;
  34.     }
  35.     public function configureOptions(OptionsResolver $resolver): void
  36.     {
  37.         parent::configureOptions($resolver);
  38.         $resolver->setDefaults(
  39.             [
  40.                 'choices' => array_combine(LegalForm::POSSIBLE_VALUESLegalForm::POSSIBLE_VALUES),
  41.                 'multiple' => false,
  42.                 'expanded' => false,
  43.             ]
  44.         );
  45.     }
  46. }