<?php
namespace MLDev\FormBundle\Service;
use MLDev\FormBundle\Form\FormBuilder;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
class FormProvider
{
private $parameters;
/**
* FormProvider constructor.
* @param $parameters
*/
public function __construct(FormFactoryInterface $formFactory, $parameters)
{
$this->parameters = $parameters;
$this->formFactory = $formFactory;
}
public function getForm($formName)
{
list('elements' => $elements) = $this->getFormConfigByName($formName);
$formBuilder = $this->formFactory->createNamedBuilder($formName, FormBuilder::class, null, [
'elements' => $elements
]);
return $formBuilder->getForm();
}
public function getFormConfigByName($formName)
{
if (!isset($this->parameters[$formName])) {
throw new InvalidConfigurationException(
sprintf('The %s form does not exist.', $formName)
);
}
return $this->parameters[$formName];
}
}