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\MongoDbOdm\Extension; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait; |
18
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
19
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
20
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
21
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Applies selected ordering while querying resource collection. |
25
|
|
|
* |
26
|
|
|
* @experimental |
27
|
|
|
* |
28
|
|
|
* @author Kévin Dunglas <[email protected]> |
29
|
|
|
* @author Samuel ROZE <[email protected]> |
30
|
|
|
* @author Vincent Chalamon <[email protected]> |
31
|
|
|
* @author Alan Poulain <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
final class OrderExtension implements AggregationCollectionExtensionInterface |
34
|
|
|
{ |
35
|
|
|
use PropertyHelperTrait; |
36
|
|
|
use MongoDbOdmPropertyHelperTrait; |
37
|
|
|
|
38
|
|
|
private $order; |
39
|
|
|
private $resourceMetadataFactory; |
40
|
|
|
private $managerRegistry; |
41
|
|
|
|
42
|
|
|
public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null, ManagerRegistry $managerRegistry = null) |
43
|
|
|
{ |
44
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
45
|
|
|
$this->order = $order; |
46
|
|
|
$this->managerRegistry = $managerRegistry; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function applyToCollection(Builder $aggregationBuilder, string $resourceClass = null, string $operationName = null, array &$context = []) |
53
|
|
|
{ |
54
|
|
|
if (null === $resourceClass) { |
55
|
|
|
throw new InvalidArgumentException('The "$resourceClass" parameter must not be null'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$classMetaData = $this->getClassMetadata($resourceClass); |
59
|
|
|
$identifiers = $classMetaData->getIdentifier(); |
60
|
|
|
if (null !== $this->resourceMetadataFactory) { |
61
|
|
|
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order'); |
62
|
|
|
if (null !== $defaultOrder) { |
63
|
|
|
foreach ($defaultOrder as $field => $order) { |
64
|
|
|
if (\is_int($field)) { |
65
|
|
|
// Default direction |
66
|
|
|
$field = $order; |
67
|
|
|
$order = 'ASC'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->isPropertyNested($field, $resourceClass)) { |
71
|
|
|
[$field] = $this->addLookupsForNestedProperty($field, $aggregationBuilder, $resourceClass); |
72
|
|
|
} |
73
|
|
|
$aggregationBuilder->sort( |
74
|
|
|
$context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$field => $order] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (null !== $this->order) { |
83
|
|
|
foreach ($identifiers as $identifier) { |
84
|
|
|
$aggregationBuilder->sort( |
85
|
|
|
$context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$identifier => $this->order] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getManagerRegistry(): ManagerRegistry |
92
|
|
|
{ |
93
|
|
|
return $this->managerRegistry; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|