|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the DunglasApiBundle package. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Dunglas\ApiBundle\Doctrine\Orm\Filter; |
| 13 | + |
| 14 | +use Doctrine\ORM\QueryBuilder; |
| 15 | +use Dunglas\ApiBundle\Api\ResourceInterface; |
| 16 | +use Dunglas\ApiBundle\Doctrine\Orm\Util\QueryNameGenerator; |
| 17 | +use Dunglas\ApiBundle\Exception\InvalidArgumentException; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | + |
| 20 | +/** |
| 21 | + * Filters the collection by range. |
| 22 | + * |
| 23 | + * @author Lee Siong Chan <[email protected]> |
| 24 | + */ |
| 25 | +class RangeFilter extends AbstractFilter |
| 26 | +{ |
| 27 | + const PARAMETER_BETWEEN = 'between'; |
| 28 | + const PARAMETER_GREATER_THAN = 'gt'; |
| 29 | + const PARAMETER_GREATER_THAN_OR_EQUAL = 'gte'; |
| 30 | + const PARAMETER_LESS_THAN = 'lt'; |
| 31 | + const PARAMETER_LESS_THAN_OR_EQUAL = 'lte'; |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder, Request $request) |
| 37 | + { |
| 38 | + foreach ($this->extractProperties($request) as $property => $values) { |
| 39 | + if ( |
| 40 | + !is_array($values) || |
| 41 | + !$this->isPropertyEnabled($property) || |
| 42 | + !$this->isPropertyMapped($property, $resource) |
| 43 | + ) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $alias = 'o'; |
| 48 | + $field = $property; |
| 49 | + |
| 50 | + if ($this->isPropertyNested($property)) { |
| 51 | + $propertyParts = $this->splitPropertyParts($property); |
| 52 | + |
| 53 | + $parentAlias = $alias; |
| 54 | + |
| 55 | + foreach ($propertyParts['associations'] as $association) { |
| 56 | + $alias = QueryNameGenerator::generateJoinAlias($association); |
| 57 | + $queryBuilder->join(sprintf('%s.%s', $parentAlias, $association), $alias); |
| 58 | + $parentAlias = $alias; |
| 59 | + } |
| 60 | + |
| 61 | + $field = $propertyParts['field']; |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($values as $operator => $value) { |
| 65 | + $this->addWhere( |
| 66 | + $queryBuilder, |
| 67 | + $alias, |
| 68 | + $field, |
| 69 | + $operator, |
| 70 | + $value |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Adds the where clause according to the operator. |
| 78 | + * |
| 79 | + * @param QueryBuilder $queryBuilder |
| 80 | + * @param string $alias |
| 81 | + * @param string $field |
| 82 | + * @param string $operator |
| 83 | + * @param string $value |
| 84 | + */ |
| 85 | + private function addWhere(QueryBuilder $queryBuilder, $alias, $field, $operator, $value) |
| 86 | + { |
| 87 | + $valueParameter = QueryNameGenerator::generateParameterName(sprintf('%s_%s', $field, $operator)); |
| 88 | + |
| 89 | + switch ($operator) { |
| 90 | + case self::PARAMETER_BETWEEN: |
| 91 | + $rangeValue = explode('..', $value); |
| 92 | + |
| 93 | + if (2 !== count($rangeValue)) { |
| 94 | + throw new InvalidArgumentException(sprintf('Invalid format for [%s], expected to be <min>..<max>', $operator)); |
| 95 | + } |
| 96 | + |
| 97 | + return $queryBuilder |
| 98 | + ->andWhere(sprintf('%1$s.%2$s BETWEEN :%3$s_1 AND :%3$s_2', $alias, $field, $valueParameter)) |
| 99 | + ->setParameter(sprintf('%s_1', $valueParameter), $rangeValue[0]) |
| 100 | + ->setParameter(sprintf('%s_2', $valueParameter), $rangeValue[1]); |
| 101 | + |
| 102 | + case self::PARAMETER_GREATER_THAN: |
| 103 | + return $queryBuilder |
| 104 | + ->andWhere(sprintf('%s.%s > :%s', $alias, $field, $valueParameter)) |
| 105 | + ->setParameter($valueParameter, $value); |
| 106 | + |
| 107 | + case self::PARAMETER_GREATER_THAN_OR_EQUAL: |
| 108 | + return $queryBuilder |
| 109 | + ->andWhere(sprintf('%s.%s >= :%s', $alias, $field, $valueParameter)) |
| 110 | + ->setParameter($valueParameter, $value); |
| 111 | + |
| 112 | + case self::PARAMETER_LESS_THAN: |
| 113 | + return $queryBuilder |
| 114 | + ->andWhere(sprintf('%s.%s < :%s', $alias, $field, $valueParameter)) |
| 115 | + ->setParameter($valueParameter, $value); |
| 116 | + |
| 117 | + case self::PARAMETER_LESS_THAN_OR_EQUAL: |
| 118 | + return $queryBuilder |
| 119 | + ->andWhere(sprintf('%s.%s <= :%s', $alias, $field, $valueParameter)) |
| 120 | + ->setParameter($valueParameter, $value); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * {@inheritdoc} |
| 126 | + */ |
| 127 | + public function getDescription(ResourceInterface $resource) |
| 128 | + { |
| 129 | + $description = []; |
| 130 | + |
| 131 | + $properties = $this->properties; |
| 132 | + if (null === $properties) { |
| 133 | + $properties = array_fill_keys($this->getClassMetadata($resource)->getFieldNames(), null); |
| 134 | + } |
| 135 | + |
| 136 | + foreach ($properties as $property => $operator) { |
| 137 | + if (!$this->isPropertyMapped($property, $resource)) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + $description += $this->getFilterDescription($property, self::PARAMETER_BETWEEN); |
| 142 | + $description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN); |
| 143 | + $description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN_OR_EQUAL); |
| 144 | + $description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN); |
| 145 | + $description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN_OR_EQUAL); |
| 146 | + } |
| 147 | + |
| 148 | + return $description; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Gets filter description. |
| 153 | + * |
| 154 | + * @param string $fieldName |
| 155 | + * @param string $period |
| 156 | + * |
| 157 | + * @return array |
| 158 | + */ |
| 159 | + private function getFilterDescription($fieldName, $period) |
| 160 | + { |
| 161 | + return [ |
| 162 | + sprintf('%s[%s]', $fieldName, $period) => [ |
| 163 | + 'property' => $fieldName, |
| 164 | + 'type' => 'string', |
| 165 | + 'required' => false, |
| 166 | + ], |
| 167 | + ]; |
| 168 | + } |
| 169 | +} |
0 commit comments