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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 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\Sort;
21
use Doctrine\Persistence\ManagerRegistry;
22

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

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

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

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

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

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

74
            return;
×
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
    private function hasSortStage(Builder $aggregationBuilder): bool
92
    {
93
        $shouldStop = false;
×
94
        $index = 0;
×
95

96
        do {
97
            try {
98
                if ($aggregationBuilder->getStage($index) instanceof Sort) {
×
99
                    // If at least one stage is sort, then it has sorting
100
                    return true;
×
101
                }
102
            } catch (\OutOfRangeException $outOfRangeException) {
×
103
                // There is no more stages on the aggregation builder
104
                $shouldStop = true;
×
105
            }
106

107
            ++$index;
×
108
        } while (!$shouldStop);
×
109

110
        // No stage was sort, and we iterated through all stages
111
        return false;
×
112
    }
113
}
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

© 2025 Coveralls, Inc