src/Transformer/JsonSchemaForm/ChoiceTransformer.php line 29

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2019 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2019 TECLA Consulting Group oü
  16.  */
  17. namespace App\Transformer\JsonSchemaForm;
  18. use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
  19. use Symfony\Component\Form\FormInterface;
  20. class ChoiceTransformer extends AbstractTransformer
  21. {
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public function transform(FormInterface $form, array $extensions = [], string $widget null): array
  26.     {
  27.         $formView $form->createView();
  28.         $translationDomain $form->getConfig()->getOption('translation_domain');
  29.         $choices = [];
  30.         $titles = [];
  31.         foreach ($formView->vars['choices'] as $choiceView) {
  32.             if (true === $choiceView instanceof ChoiceGroupView) {
  33.                 foreach ($choiceView->choices as $choiceItem) {
  34.                     $title $choiceItem->label;
  35.                     if (false !== $translationDomain) {
  36.                         $title $this->translator->trans($title);
  37.                     }
  38.                     $choices[] = $choiceItem->value;
  39.                     $titles[] = $title;
  40.                 }
  41.             } else {
  42.                 $title $choiceView->label;
  43.                 if (false !== $translationDomain) {
  44.                     $title $this->translator->trans($title);
  45.                 }
  46.                 $choices[] = $choiceView->value;
  47.                 $titles[] = $title;
  48.             }
  49.         }
  50.         if ($formView->vars['multiple']) {
  51.             $schema $this->transformMultiple($form$choices$titles);
  52.         } else {
  53.             $schema $this->transformSingle($form$choices$titles);
  54.         }
  55.         $schema $this->addCommonSpecs($form$schema$widget$extensions);
  56.         return $schema;
  57.     }
  58.     private function transformSingle(FormInterface $form, array $choices, array $titles): array
  59.     {
  60.         $formView $form->createView();
  61.         $schema = [
  62.             'enum' => $choices,
  63.             'enum_titles' => $titles,
  64.             'type' => 'string',
  65.         ];
  66.         if ($formView->vars['expanded']) {
  67.             $schema['widget'] = 'choice-expanded';
  68.         }
  69.         return $schema;
  70.     }
  71.     private function transformMultiple(FormInterface $form, array $choices, array $titles): array
  72.     {
  73.         $formView $form->createView();
  74.         $schema = [
  75.             'items' => [
  76.                 'type' => 'string',
  77.                 'enum' => $choices,
  78.                 'enum_titles' => $titles,
  79.             ],
  80.             'minItems' => true === $this->isRequired($form) ? 0,
  81.             'uniqueItems' => true,
  82.             'type' => 'array',
  83.         ];
  84.         if (true === $formView->vars['expanded']) {
  85.             $schema['widget'] = 'choice-multiple-expanded';
  86.         }
  87.         return $schema;
  88.     }
  89. }