bundles/Factory/SupportBundle/Repository/FormRepository.php line 73

Open in your IDE?
  1. <?php
  2. namespace Factory\SupportBundle\Repository;
  3. use Factory\OrderManagementBundle\Exception\FormValidationException;
  4. use Symfony\Component\DomCrawler\Form;
  5. use Symfony\Component\Form\FormError;
  6. use Symfony\Component\Form\FormInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class FormRepository
  14. {
  15.     const PAYLOAD_SERIALIZATION_KEY '_oldFormData';
  16.     /**
  17.      * @var [type]
  18.      */
  19.     protected $formFactory;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     protected $requestStack;
  24.     /**
  25.      * @var Session
  26.      */
  27.     protected $session;
  28.     /**
  29.      * @var EventDispatcherInterface
  30.      */
  31.     protected $dispatcher;
  32.     /**
  33.      * @var array
  34.      */
  35.     protected $oldData;
  36.     /**
  37.      * @var TranslatorInterface
  38.      */
  39.     protected $translator;
  40.     public function __construct(
  41.         $formFactory,
  42.         $session,
  43.         RequestStack $requestStack,
  44.         EventDispatcherInterface $dispatcher,
  45.         TranslatorInterface $translator
  46.     ) {
  47.         $this->formFactory $formFactory;
  48.         $this->session $session;
  49.         $this->requestStack $requestStack;
  50.         $this->dispatcher $dispatcher;
  51.         $this->translator $translator;
  52.     }
  53.     /**
  54.      * Creates and returns a Form instance from the type of the form.
  55.      *
  56.      * @final
  57.      */
  58.     public function createForm(string $type$data null, array $options = []): FormInterface
  59.     {
  60.         $form $this->formFactory->create($type$data$options);
  61.         if ($this->getFormOldData()) {
  62.             $old_data $this->getFormOldData();
  63.             $form->submit($old_data[$form->getName()] ?? $old_data);
  64.         }
  65.         return $form;
  66.     }
  67.     public function handleRequest(string $typeRequest $request$data null, array $options = []): ?FormInterface
  68.     {
  69.         $form $this->formFactory->create($type$data$options);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && !$form->isValid()) {
  72.             throw new FormValidationException($form$this->translator->trans('stoelting.form.validation.failed'));
  73.             $e $this->createErrorList($form->getErrors(truefalse));
  74.             return null;
  75.         }
  76.         return $form;
  77.     }
  78.     protected function createErrorList($errors$prefix null)
  79.     {
  80.         $_errors = [];
  81.         foreach ($errors as $error) {
  82.             if ($error instanceof \Symfony\Component\Form\FormErrorIterator) {
  83.                 $_errors array_merge(
  84.                     $_errors,
  85.                     $this->createErrorList($error$errors->getForm()->getName())
  86.                 );
  87.             } else {
  88.                 $_errors[trim($prefix.'.'.$error->getOrigin()->getName(), '.')] = $error->getMessage();
  89.             }
  90.         }
  91.         return $_errors;
  92.     }
  93.     public function serializePayload()
  94.     {
  95.         $payload array_merge(
  96.             $this->getRequest()->query->all(),
  97.             $this->getRequest()->request->all()
  98.         );
  99.         $this->session->getFlashBag()->add(self::PAYLOAD_SERIALIZATION_KEYserialize($payload));
  100.     }
  101.     protected function getFormOldData()
  102.     {
  103.         if ($this->oldData === null) {
  104.             $oldFormData $this->session->getFlashBag()->get(self::PAYLOAD_SERIALIZATION_KEY);
  105.             $this->oldData unserialize(array_pop($oldFormData));
  106.         }
  107.         return $this->oldData === false null $this->oldData;
  108.     }
  109.     protected function getRequest(): Request
  110.     {
  111.         return $this->requestStack->getCurrentRequest();
  112.     }
  113. }