• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 17758811669

16 Sep 2025 07:55AM UTC coverage: 21.943%. First build
17758811669

Pull #7387

github

web-flow
Merge c2735bbac into 25a4852f3
Pull Request #7387: feat(doctrine): Add MongoDB Atlas Search filter

0 of 54 new or added lines in 3 files covered. (0.0%)

11865 of 54072 relevant lines covered (21.94%)

12.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Doctrine/Odm/Extension/OrderExtension.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\Extension;
15

16
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
17
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
18
use ApiPlatform\Metadata\Operation;
19
use Doctrine\ODM\MongoDB\Aggregation\Builder;
20
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
21
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
22
use Doctrine\Persistence\ManagerRegistry;
23

24
/**
25
 * Applies selected ordering while querying resource collection.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 * @author Samuel ROZE <samuel.roze@gmail.com>
29
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
30
 * @author Alan Poulain <contact@alanpoulain.eu>
31
 */
32
final class OrderExtension implements AggregationCollectionExtensionInterface
33
{
34
    use MongoDbOdmPropertyHelperTrait;
35
    use PropertyHelperTrait;
36

37
    public function __construct(private readonly ?string $order = null, private readonly ?ManagerRegistry $managerRegistry = null)
38
    {
39
    }
×
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
45
    {
46
        // Do not apply order if already defined on $aggregationBuilder
47
        if ($this->hasSortStage($aggregationBuilder)) {
×
48
            return;
×
49
        }
50

51
        $classMetaData = $this->getClassMetadata($resourceClass);
×
52
        $identifiers = $classMetaData->getIdentifier();
×
53
        if (isset($context['operation'])) {
×
54
            $defaultOrder = $context['operation']->getOrder() ?? [];
×
55
        } else {
56
            $defaultOrder = $operation?->getOrder();
×
57
        }
58

59
        if ($defaultOrder) {
×
60
            foreach ($defaultOrder as $field => $order) {
×
61
                if (\is_int($field)) {
×
62
                    // Default direction
63
                    $field = $order;
×
64
                    $order = 'ASC';
×
65
                }
66

67
                if ($this->isPropertyNested($field, $resourceClass)) {
×
68
                    [$field] = $this->addLookupsForNestedProperty($field, $aggregationBuilder, $resourceClass, true);
×
69
                }
NEW
70
                $this->addSort(
×
NEW
71
                    $aggregationBuilder,
×
72
                    $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$field => $order]
×
73
                );
×
74
            }
75

76
            return;
×
77
        }
78

79
        if (null !== $this->order) {
×
80
            foreach ($identifiers as $identifier) {
×
NEW
81
                $this->addSort(
×
NEW
82
                    $aggregationBuilder,
×
83
                    $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$identifier => $this->order]
×
84
                );
×
85
            }
86
        }
87
    }
88

89
    protected function getManagerRegistry(): ManagerRegistry
90
    {
91
        return $this->managerRegistry;
×
92
    }
93

94
    private function addSort(Builder $aggregationBuilder, array $sortFields): void
95
    {
NEW
96
        $firstStage = $aggregationBuilder->getPipeline(0);
×
NEW
97
        if ($firstStage instanceof Search) {
×
98
            // The $search stage supports "sort" for performance, it's always first if present
NEW
99
            $firstStage->sort($sortFields);
×
100
        } else {
101
            // Append a $sort stage at the end of the pipeline
NEW
102
            $aggregationBuilder->sort($sortFields);
×
103
        }
104
    }
105

106
    private function hasSortStage(Builder $aggregationBuilder): bool
107
    {
108
        try {
NEW
109
            for ($index = 0; true; $index++) {
×
110
                if ($aggregationBuilder->getStage($index) instanceof Sort) {
×
111
                    // If at least one stage is sort, then it has sorting
112
                    return true;
×
113
                }
114
            }
NEW
115
        } catch (\OutOfRangeException) {
×
116
            // There is no more stages on the aggregation builder
NEW
117
            return false;
×
118
        }
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc