FilterFactory.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * @param array $types
  26. */
  27. public function __construct(ContainerInterface $container, array $types = array())
  28. {
  29. $this->container = $container;
  30. $this->types = $types;
  31. }
  32. /**
  33. * @throws \RunTimeException
  34. * @param $name
  35. * @param $type
  36. * @param $options
  37. * @return object|\Symfony\Component\DependencyInjection\The
  38. */
  39. public function create($name, $type, array $options = array())
  40. {
  41. if (!$type) {
  42. throw new \RunTimeException('The type must be defined');
  43. }
  44. $id = isset($this->types[$type]) ? $this->types[$type] : false;
  45. if (!$id) {
  46. throw new \RunTimeException(sprintf('No attached service to type named `%s`', $type));
  47. }
  48. $filter = $this->container->get($id);
  49. if (!$filter instanceof FilterInterface) {
  50. throw new \RunTimeException(sprintf('The service `%s` must implement `FilterInterface`', $id));
  51. }
  52. $filter->initialize($name, $options);
  53. return $filter;
  54. }
  55. }