src/Transformer/JsonSchemaForm/AbstractTransformer.php line 37

  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\FormInterface;
  19. use Symfony\Component\Form\FormTypeGuesserInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. abstract class AbstractTransformer implements TransformerInterface
  22. {
  23.     /**
  24.      * @var TranslatorInterface
  25.      */
  26.     protected $translator;
  27.     /**
  28.      * @var FormTypeGuesserInterface|null
  29.      */
  30.     protected $validatorGuesser;
  31.     public function __construct(TranslatorInterface $translatorFormTypeGuesserInterface $validatorGuesser null)
  32.     {
  33.         $this->translator $translator;
  34.         $this->validatorGuesser $validatorGuesser;
  35.     }
  36.     /**
  37.      * @param ExtensionInterface[] $extensions
  38.      */
  39.     protected function applyExtensions(array $extensionsFormInterface $form, array $schema): array
  40.     {
  41.         $newSchema $schema;
  42.         foreach ($extensions as $extension) {
  43.             $newSchema $extension->apply($form$newSchema);
  44.         }
  45.         return $newSchema;
  46.     }
  47.     /**
  48.      * @param ExtensionInterface[] $extensions
  49.      */
  50.     protected function addCommonSpecs(FormInterface $form, array $schema, ?string $widget, array $extensions = []): array
  51.     {
  52.         $schema $this->addLabel($form$schema);
  53.         $schema $this->addDisabled($form$schema);
  54.         $schema $this->addAttr($form$schema);
  55.         $schema $this->addPattern($form$schema);
  56.         $schema $this->addDescription($form$schema);
  57.         $schema $this->addWidget($schema$widget);
  58.         $schema $this->applyExtensions($extensions$form$schema);
  59.         return $schema;
  60.     }
  61.     protected function addPattern(FormInterface $form, array $schema): array
  62.     {
  63.         if ($attr $form->getConfig()->getOption('attr')) {
  64.             if (isset($attr['pattern'])) {
  65.                 $schema['pattern'] = $attr['pattern'];
  66.             }
  67.         }
  68.         return $schema;
  69.     }
  70.     protected function addLabel(FormInterface $form, array $schema): array
  71.     {
  72.         $translationDomain $form->getConfig()->getOption('translation_domain');
  73.         $title $form->getConfig()->getOption('label');
  74.         if (null === $title) {
  75.             $title $form->getName();
  76.         }
  77.         if (false === empty($title) && false !== $translationDomain) {
  78.             $title $this->translator->trans($title$form->getConfig()->getOption('label_translation_parameters', []), $translationDomain);
  79.         }
  80.         $schema['title'] = $title;
  81.         return $schema;
  82.     }
  83.     protected function addDisabled(FormInterface $form, array $schema): array
  84.     {
  85.         $schema['disabled'] = true === $form->getConfig()->getOption('disabled');
  86.         return $schema;
  87.     }
  88.     protected function addAttr(FormInterface $form, array $schema): array
  89.     {
  90.         $attr $form->getConfig()->getOption('attr');
  91.         if (false === empty($attr)) {
  92.             $schema['attr'] = $attr;
  93.         }
  94.         return $schema;
  95.     }
  96.     protected function addDescription(FormInterface $form, array $schema): array
  97.     {
  98.         $description $form->getConfig()->getOption('help');
  99.         if (null !== $description) {
  100.             $translationDomain $form->getConfig()->getOption('translation_domain');
  101.             if (false !== $translationDomain) {
  102.                 $description $this->translator->trans($description$form->getConfig()->getOption('help_translation_parameters', []), $translationDomain);
  103.             }
  104.             $schema['description'] = $description;
  105.         }
  106.         return $schema;
  107.     }
  108.     /**
  109.      * @param mixed $configWidget
  110.      */
  111.     protected function addWidget(array $schema$configWidget): array
  112.     {
  113.         if ($configWidget) {
  114.             $schema['widget'] = $configWidget;
  115.         }
  116.         return $schema;
  117.     }
  118.     protected function isRequired(FormInterface $form): bool
  119.     {
  120.         return $form->getConfig()->getOption('required');
  121.     }
  122. }