CallbackFilter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\ORM;
  11. use Symfony\Component\Form\FormFactory;
  12. use Doctrine\ORM\QueryBuilder;
  13. class CallbackFilter extends Filter
  14. {
  15. /**
  16. * @param QueryBuilder $queryBuilder
  17. * @param mixed $value
  18. * @return array
  19. */
  20. protected function association($queryBuilder, $value)
  21. {
  22. return array($queryBuilder->getRootAlias(), false);
  23. }
  24. /**
  25. * @throws \RuntimeException
  26. * @param QueryBuilder $queryBuilder
  27. * @param string $alias
  28. * @param string $field
  29. * @param string $value
  30. * @return void
  31. */
  32. public function filter($queryBuilder, $alias, $field, $value)
  33. {
  34. if (!is_callable($this->getOption('callback'))) {
  35. throw new \RuntimeException(sprintf('Please provide a valid callback option "filter" for field "%s"', $this->getName()));
  36. }
  37. call_user_func($this->getOption('callback'), $queryBuilder, $alias, $field, $value);
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function getDefaultOptions()
  43. {
  44. return array(
  45. 'callback' => null,
  46. 'field_type' => 'text',
  47. );
  48. }
  49. }