Filter.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Admin\FieldDescriptionInterface;
  12. use Sonata\AdminBundle\Filter\FilterInterface;
  13. use Doctrine\ORM\QueryBuilder;
  14. abstract class Filter implements FilterInterface
  15. {
  16. protected $fieldDescription = array();
  17. protected $name = null;
  18. protected $field = null;
  19. protected $value = null;
  20. protected $options = array();
  21. public function __construct(FieldDescriptionInterface $fieldDescription)
  22. {
  23. $this->name = $fieldDescription->getName();
  24. $this->fieldDescription = $fieldDescription;
  25. $this->options = array_replace(
  26. $this->getDefaultOptions(),
  27. $this->fieldDescription->getOption('filter_options', array())
  28. );
  29. }
  30. public function getName()
  31. {
  32. return $this->name;
  33. }
  34. public function getField()
  35. {
  36. if (!$this->field) {
  37. throw new \RuntimeException(sprintf('No field instance attached for the filter `%s`', $this->name));
  38. }
  39. return $this->field;
  40. }
  41. /**
  42. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  43. */
  44. public function getFieldDescription()
  45. {
  46. return $this->fieldDescription;
  47. }
  48. public function getDefaultOptions()
  49. {
  50. return array();
  51. }
  52. public function getOption($name, $default = null)
  53. {
  54. if (array_key_exists($name, $this->options)) {
  55. return $this->options[$name];
  56. }
  57. return $default;
  58. }
  59. }