123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- */
- namespace Sonata\AdminBundle\Form;
- use Sonata\AdminBundle\Builder\FormContractorInterface;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Symfony\Component\Form\FormBuilder;
- /**
- * This class is use to simulate the Form API
- *
- */
- class FormMapper
- {
- protected $formBuilder;
- protected $formContractor;
- protected $admin;
- public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
- {
- $this->formBuilder = $formBuilder;
- $this->formContractor = $formContractor;
- $this->admin = $admin;
- }
- /**
- * The method add a new field to the provided FormBuilder, there are 4 ways to add new field :
- *
- * - if $name is a string with no related FieldDescription, then the form will use the FormFactory
- * to instantiate a new Field
- * - if $name is a FormDescription, the method uses information defined in the FormDescription to
- * instantiate a new Field
- * - if $name is a FormBuilder, then a FieldDescriptionInterface is created, the FormBuilder is added to
- * the form
- * - if $name is a string with a related FieldDescription, then the method uses information defined in the
- * FormDescription to instantiate a new Field
- *
- * @throws \RuntimeException
- * @param string $name
- * @param array $fieldOptions
- * @param array $fieldDescriptionOptions
- * @return \Symfony\Component\Form\FieldInterface|void
- */
- public function add($name, array $fieldOptions = array(), array $fieldDescriptionOptions = array())
- {
- $fieldType = false;
- if ($name instanceof FieldDescriptionInterface) {
- $fieldDescription = $name;
- $fieldDescription->mergeOptions($fieldDescriptionOptions);
- } else if ($name instanceof FormBuilder) {
- $fieldType = $name;
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $fieldType->getName(),
- $fieldDescriptionOptions
- );
- $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
- $this->admin->addFormFieldDescription($fieldType->getName(), $fieldDescription);
- } else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $name,
- $fieldDescriptionOptions
- );
- // set default configuration
- $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
- // add the FieldDescription
- $this->admin->addFormFieldDescription($name, $fieldDescription);
- } else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
- $fieldDescription = $this->admin->getFormFieldDescription($name);
- // update configuration
- $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
- } else {
- throw new \RuntimeException('invalid state');
- }
- $fieldDescription->mergeOption('form_field_options', $fieldOptions);
- // nothing to build as a Field is provided
- if ($fieldType) {
- $this->formBuilder->add($fieldType);
- }
- // add the field with the FormBuilder
- $this->formContractor->addField(
- $this->formBuilder,
- $fieldDescription
- );
- return $this;
- }
- /**
- * @param string $name
- * @param string $type
- * @param array $options
- * @param array $fieldDescriptionOptions
- * @return \Symfony\Component\Form\FormInterface
- */
- public function addType($name, $type, array $options = array(), array $fieldDescriptionOptions = array())
- {
- if (!isset($fieldDescriptionOptions['type'])) {
- $fieldDescriptionOptions['type'] = $type;
- }
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $name,
- $fieldDescriptionOptions
- );
- $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
- $this->admin->addFormFieldDescription($name, $fieldDescription);
- $this->formBuilder->add($name, $type, $options);
- return $this;
- }
- /**
- * @param string $name
- * @return \Symfony\Component\Form\FieldInterface
- */
- public function get($name)
- {
- return $this->formBuilder->get($name);
- }
- /**
- * @param string $key
- * @return boolean
- */
- public function has($key)
- {
- return $this->formBuilder->has($key);
- }
- /**
- * @param string $key
- * @return void
- */
- public function remove($key)
- {
- $this->admin->removeFormFieldDescription($key);
- $this->formBuilder->remove($key);
- }
- /**
- * @return \Symfony\Component\Form\FormBuilder
- */
- public function getFormBuilder()
- {
- return $this->formBuilder;
- }
- /**
- * @return \Sonata\AdminBundle\Admin\AdminInterface
- */
- public function getAdmin()
- {
- return $this->admin;
- }
- }
|