NumberFilter.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Sonata\AdminBundle\Form\Type\Filter\NumberType;
  12. class NumberFilter extends Filter
  13. {
  14. /**
  15. * @param QueryBuilder $queryBuilder
  16. * @param string $alias
  17. * @param string $field
  18. * @param string $value
  19. * @return
  20. */
  21. public function filter($queryBuilder, $alias, $field, $value)
  22. {
  23. if ($value == null || !is_array($value)) {
  24. return;
  25. }
  26. $operator = $this->getOperator((int) $value['type']);
  27. if (!$operator) {
  28. return;
  29. }
  30. // c.name > '1' => c.name OPERATOR :FIELDNAME
  31. $queryBuilder->andWhere(sprintf('%s.%s %s :%s', $alias, $field, $operator, $this->getName()));
  32. $queryBuilder->setParameter($this->getName(), $value['value']);
  33. }
  34. /**
  35. * @param $type
  36. * @return bool
  37. */
  38. private function getOperator($type)
  39. {
  40. $choices = array(
  41. NumberType::TYPE_EQUAL => '=',
  42. NumberType::TYPE_GREATER_EQUAL => '>=',
  43. NumberType::TYPE_GREATER_THAN => '>',
  44. NumberType::TYPE_LESS_EQUAL => '<=',
  45. NumberType::TYPE_LESS_THAN => '<',
  46. );
  47. return isset($choices[$type]) ? $choices[$type] : false;
  48. }
  49. }