src/Transformer/JsonSchemaForm/StringTransformer.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 App\Util\JsonSchemaForm\JsonSchemaFormUtil;
  19. use Symfony\Component\Form\FormInterface;
  20. class StringTransformer extends AbstractTransformer
  21. {
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public function transform(FormInterface $form, array $extensions = [], string $widget null): array
  26.     {
  27.         $schema = ['type' => 'string'];
  28.         $schema $this->addCommonSpecs($form$schema$widget$extensions);
  29.         $schema $this->addMaxLength($form$schema);
  30.         $schema $this->addMinLength($form$schema);
  31.         return $schema;
  32.     }
  33.     protected function addMaxLength(FormInterface $form, array $schema): array
  34.     {
  35.         if ($attr $form->getConfig()->getOption('attr')) {
  36.             if (true === isset($attr['maxlength'])) {
  37.                 $schema['maxLength'] = $attr['maxlength'];
  38.             }
  39.         }
  40.         return $schema;
  41.     }
  42.     protected function addMinLength(FormInterface $form, array $schema): array
  43.     {
  44.         if ($attr $form->getConfig()->getOption('attr')) {
  45.             if (isset($attr['minlength'])) {
  46.                 $schema['minLength'] = $attr['minlength'];
  47.                 return $schema;
  48.             }
  49.         }
  50.         if (null === $this->validatorGuesser) {
  51.             return $schema;
  52.         }
  53.         $class JsonSchemaFormUtil::findDataClass($form);
  54.         if (null === $class) {
  55.             return $schema;
  56.         }
  57.         $minLengthGuess $this->validatorGuesser->guessMinLength($class$form->getName());
  58.         $minLength null !== $minLengthGuess $minLengthGuess->getValue() : null;
  59.         if ($minLength) {
  60.             $schema['minLength'] = $minLength;
  61.         }
  62.         return $schema;
  63.     }
  64. }