ImmutableArrayType.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace Sonata\AdminBundle\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  15. class ImmutableArrayType extends AbstractType
  16. {
  17. /**
  18. * {@inheritDoc}
  19. */
  20. public function buildForm(FormBuilderInterface $builder, array $options)
  21. {
  22. foreach ($options['keys'] as $infos) {
  23. if ($infos instanceof FormBuilderInterface) {
  24. $builder->add($infos);
  25. } else {
  26. list($name, $type, $options) = $infos;
  27. if (is_callable($options)) {
  28. $options = $options($builder, $options);
  29. }
  30. $builder->add($name, $type, $options);
  31. }
  32. }
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function setDefaultOptions(OptionsResolverInterface $resolver)
  38. {
  39. $resolver->setDefaults(array(
  40. 'keys' => array(),
  41. ));
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getName()
  47. {
  48. return 'sonata_type_immutable_array';
  49. }
  50. }