FilterFactory.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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)
  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. $fieldDescription->mergeOption('field_options', array('required' => false));
  73. $filter->setFieldDescription($fieldDescription);
  74. $filter->defineFieldBuilder($this->container->get('form.factory'));
  75. return $filter;
  76. }
  77. }