1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
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 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter; |
||
15 | |||
16 | use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\OrderFilterInterface; |
||
17 | use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\OrderFilterTrait; |
||
18 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
||
19 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
20 | use Doctrine\ORM\Query\Expr\Join; |
||
21 | use Doctrine\ORM\QueryBuilder; |
||
22 | use Psr\Log\LoggerInterface; |
||
23 | use Symfony\Component\HttpFoundation\Request; |
||
24 | use Symfony\Component\HttpFoundation\RequestStack; |
||
25 | use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
||
26 | |||
27 | /** |
||
28 | * Order the collection by given properties. |
||
29 | * |
||
30 | * The ordering is done in the same sequence as they are specified in the query, |
||
31 | * and for each property a direction value can be specified. |
||
32 | * |
||
33 | * For each property passed, if the resource does not have such property or if the |
||
34 | * direction value is different from "asc" or "desc" (case insensitive), the property |
||
35 | * is ignored. |
||
36 | * |
||
37 | * @author Kévin Dunglas <[email protected]> |
||
38 | * @author Théo FIDRY <[email protected]> |
||
39 | */ |
||
40 | class OrderFilter extends AbstractContextAwareFilter implements OrderFilterInterface |
||
41 | { |
||
42 | use OrderFilterTrait; |
||
43 | |||
44 | public function __construct(ManagerRegistry $managerRegistry, ?RequestStack $requestStack = null, string $orderParameterName = 'order', LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null) |
||
45 | { |
||
46 | if (null !== $properties) { |
||
47 | $properties = array_map(function ($propertyOptions) { |
||
48 | // shorthand for default direction |
||
49 | if (\is_string($propertyOptions)) { |
||
50 | $propertyOptions = [ |
||
51 | 'default_direction' => $propertyOptions, |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | return $propertyOptions; |
||
56 | }, $properties); |
||
57 | } |
||
58 | |||
59 | parent::__construct($managerRegistry, $requestStack, $logger, $properties, $nameConverter); |
||
60 | |||
61 | $this->orderParameterName = $orderParameterName; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) |
||
68 | { |
||
69 | if (isset($context['filters']) && !isset($context['filters'][$this->orderParameterName])) { |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | if (!isset($context['filters'][$this->orderParameterName]) || !\is_array($context['filters'][$this->orderParameterName])) { |
||
74 | parent::apply($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); |
||
75 | |||
76 | return; |
||
77 | } |
||
78 | |||
79 | foreach ($context['filters'][$this->orderParameterName] as $property => $value) { |
||
80 | $this->filterProperty($this->denormalizePropertyName($property), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | protected function filterProperty(string $property, $direction, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
||
88 | { |
||
89 | if (!$this->isPropertyEnabled($property, $resourceClass) || !$this->isPropertyMapped($property, $resourceClass)) { |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | $direction = $this->normalizeValue($direction, $property); |
||
94 | if (null === $direction) { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | $alias = $queryBuilder->getRootAliases()[0]; |
||
99 | $field = $property; |
||
100 | |||
101 | if ($this->isPropertyNested($property, $resourceClass)) { |
||
102 | [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::LEFT_JOIN); |
||
103 | } |
||
104 | |||
105 | if (null !== $nullsComparison = $this->properties[$property]['nulls_comparison'] ?? null) { |
||
106 | $nullsDirection = self::NULLS_DIRECTION_MAP[$nullsComparison][$direction]; |
||
107 | |||
108 | $nullRankHiddenField = sprintf('_%s_%s_null_rank', $alias, $field); |
||
109 | |||
110 | $queryBuilder->addSelect(sprintf('CASE WHEN %s.%s IS NULL THEN 0 ELSE 1 END AS HIDDEN %s', $alias, $field, $nullRankHiddenField)); |
||
111 | $queryBuilder->addOrderBy($nullRankHiddenField, $nullsDirection); |
||
112 | } |
||
113 | |||
114 | $queryBuilder->addOrderBy(sprintf('%s.%s', $alias, $field), $direction); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | protected function extractProperties(Request $request/*, string $resourceClass*/): array |
||
0 ignored issues
–
show
|
|||
121 | { |
||
122 | @trigger_error(sprintf('The use of "%s::extractProperties()" is deprecated since 2.2. Use the "filters" key of the context instead.', __CLASS__), E_USER_DEPRECATED); |
||
123 | $properties = $request->query->get($this->orderParameterName); |
||
124 | |||
125 | return \is_array($properties) ? $properties : []; |
||
126 | } |
||
127 | } |
||
128 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.