<?php
namespace Factory\SupportBundle\Repository;
use Factory\OrderManagementBundle\Exception\FormValidationException;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
class FormRepository
{
const PAYLOAD_SERIALIZATION_KEY = '_oldFormData';
/**
* @var [type]
*/
protected $formFactory;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var Session
*/
protected $session;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var array
*/
protected $oldData;
/**
* @var TranslatorInterface
*/
protected $translator;
public function __construct(
$formFactory,
$session,
RequestStack $requestStack,
EventDispatcherInterface $dispatcher,
TranslatorInterface $translator
) {
$this->formFactory = $formFactory;
$this->session = $session;
$this->requestStack = $requestStack;
$this->dispatcher = $dispatcher;
$this->translator = $translator;
}
/**
* Creates and returns a Form instance from the type of the form.
*
* @final
*/
public function createForm(string $type, $data = null, array $options = []): FormInterface
{
$form = $this->formFactory->create($type, $data, $options);
if ($this->getFormOldData()) {
$old_data = $this->getFormOldData();
$form->submit($old_data[$form->getName()] ?? $old_data);
}
return $form;
}
public function handleRequest(string $type, Request $request, $data = null, array $options = []): ?FormInterface
{
$form = $this->formFactory->create($type, $data, $options);
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
throw new FormValidationException($form, $this->translator->trans('stoelting.form.validation.failed'));
$e = $this->createErrorList($form->getErrors(true, false));
return null;
}
return $form;
}
protected function createErrorList($errors, $prefix = null)
{
$_errors = [];
foreach ($errors as $error) {
if ($error instanceof \Symfony\Component\Form\FormErrorIterator) {
$_errors = array_merge(
$_errors,
$this->createErrorList($error, $errors->getForm()->getName())
);
} else {
$_errors[trim($prefix.'.'.$error->getOrigin()->getName(), '.')] = $error->getMessage();
}
}
return $_errors;
}
public function serializePayload()
{
$payload = array_merge(
$this->getRequest()->query->all(),
$this->getRequest()->request->all()
);
$this->session->getFlashBag()->add(self::PAYLOAD_SERIALIZATION_KEY, serialize($payload));
}
protected function getFormOldData()
{
if ($this->oldData === null) {
$oldFormData = $this->session->getFlashBag()->get(self::PAYLOAD_SERIALIZATION_KEY);
$this->oldData = unserialize(array_pop($oldFormData));
}
return $this->oldData === false ? null : $this->oldData;
}
protected function getRequest(): Request
{
return $this->requestStack->getCurrentRequest();
}
}