|
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\Extension; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryBuilderHelper; |
|
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
|
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Applies selected ordering while querying resource collection. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
* @author Samuel ROZE <[email protected]> |
|
26
|
|
|
* @author Vincent Chalamon <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class OrderExtension implements QueryCollectionExtensionInterface |
|
29
|
|
|
{ |
|
30
|
|
|
private $order; |
|
31
|
|
|
private $resourceMetadataFactory; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
36
|
|
|
$this->order = $order; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass); |
|
45
|
|
|
$identifiers = $classMetaData->getIdentifier(); |
|
46
|
|
|
if (null !== $this->resourceMetadataFactory) { |
|
47
|
|
|
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order'); |
|
48
|
|
|
if (null !== $defaultOrder) { |
|
49
|
|
|
foreach ($defaultOrder as $field => $order) { |
|
50
|
|
|
if (\is_int($field)) { |
|
51
|
|
|
// Default direction |
|
52
|
|
|
$field = $order; |
|
53
|
|
|
$order = 'ASC'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (false === ($pos = \strpos($field, '.')) |
|
57
|
|
|
|| isset($classMetaData->embeddedClasses[\substr($field, 0, $pos)])) { |
|
58
|
|
|
// Configure default filter with property |
|
59
|
|
|
$field = 'o.'.$field; |
|
60
|
|
|
} else { |
|
61
|
|
|
$alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, 'o', substr($field, 0, $pos)); |
|
62
|
|
|
$field = sprintf('%s.%s', $alias, substr($field, $pos + 1)); |
|
63
|
|
|
} |
|
64
|
|
|
$queryBuilder->addOrderBy($field, $order); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (null !== $this->order) { |
|
72
|
|
|
foreach ($identifiers as $identifier) { |
|
73
|
|
|
$queryBuilder->addOrderBy('o.'.$identifier, $this->order); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|