FilterFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace Sonata\AdminBundle\Filter;
  11. use Sonata\AdminBundle\Filter\Filter as BaseFilter;
  12. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Sonata\AdminBundle\Filter\FilterFactoryInterface;
  15. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  16. use Symfony\Component\Form\FormFactory;
  17. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. class FilterFactory implements FilterFactoryInterface
  20. {
  21. protected $container;
  22. protected $types;
  23. /**
  24. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  25. */
  26. public function __construct(ContainerInterface $container, array $types = array())
  27. {
  28. $this->container = $container;
  29. $this->types = $types;
  30. }
  31. /**
  32. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  33. * @return void
  34. */
  35. public function create(FieldDescriptionInterface $fieldDescription, array $options = array())
  36. {
  37. if (!$fieldDescription->getType()) {
  38. throw new \RunTimeException('The type must be defined');
  39. }
  40. $type = $fieldDescription->getType();
  41. // $
  42. // switch($fieldDescription->getMappingType()) {
  43. // case ClassMetadataInfo::MANY_TO_ONE:
  44. // $options = $fieldDescription->getOption('filter_field_options');
  45. // $filter = new \Sonata\AdminBundle\Filter\ORM\IntegerFilter($fieldDescription);
  46. //
  47. // break;
  48. //
  49. // case ClassMetadataInfo::MANY_TO_MANY:
  50. // $options = $fieldDescription->getOption('filter_field_options');
  51. // $options['choices'] = $this->getChoices($fieldDescription);
  52. //
  53. //
  54. // $fieldDescription->setOption('filter_field_options', $options);
  55. //
  56. // $filter = new \Sonata\AdminBundle\Filter\ORM\ChoiceFilter($fieldDescription);
  57. //
  58. // break;
  59. //
  60. // default:
  61. // $class = $this->getFilterFieldClass($fieldDescription);
  62. // $filter = new $class($fieldDescription);
  63. // }
  64. $id = isset($this->types[$type]) ? $this->types[$type] : false;
  65. if (!$id) {
  66. throw new \RunTimeException(sprintf('No attached service to type named `%s`', $type));
  67. }
  68. $filter = $this->container->get($id);
  69. if (!$filter instanceof FilterInterface) {
  70. throw new \RunTimeException(sprintf('The service `%s` must implement `FilterInterface`', $id));
  71. }
  72. $filter->setFieldDescription($fieldDescription);
  73. $options['field_options']['required'] = false;
  74. $filter->initialize($options);
  75. $filter->defineFieldBuilder($this->container->get('form.factory'));
  76. return $filter;
  77. }
  78. }