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