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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 hits per line

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

0.0
/src/Elasticsearch/Extension/SortExtension.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\Elasticsearch\Extension;
15

16
use ApiPlatform\Elasticsearch\Util\FieldDatatypeTrait;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\ResourceClassResolverInterface;
21
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22

23
/**
24
 * Applies selected sorting while querying resource collection.
25
 *
26
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
27
 *
28
 * @experimental
29
 *
30
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
31
 */
32
final class SortExtension implements RequestBodySearchCollectionExtensionInterface
33
{
34
    use FieldDatatypeTrait;
35

36
    public function __construct(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly ?NameConverterInterface $nameConverter = null, private readonly ?string $defaultDirection = null)
37
    {
38
        $this->propertyMetadataFactory = $propertyMetadataFactory;
×
39
        $this->resourceClassResolver = $resourceClassResolver;
×
40
    }
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function applyToCollection(array $requestBody, string $resourceClass, ?Operation $operation = null, array $context = []): array
46
    {
47
        $orders = [];
×
48

49
        if (
50
            $operation
×
51
            && null !== ($defaultOrder = $operation->getOrder())
×
52
        ) {
UNCOV
53
            foreach ($defaultOrder as $property => $direction) {
×
54
                if (\is_int($property)) {
×
55
                    $property = $direction;
×
56
                    $direction = 'asc';
×
57
                }
58

UNCOV
59
                $orders[] = $this->getOrder($resourceClass, $property, $direction);
×
60
            }
UNCOV
61
        } elseif (null !== $this->defaultDirection) {
×
62
            $property = 'id';
×
63
            if ($operation instanceof HttpOperation) {
×
64
                $uriVariables = $operation->getUriVariables()[0] ?? null;
×
65
                $property = $uriVariables ? $uriVariables->getIdentifiers()[0] ?? 'id' : 'id';
×
66
            }
67

UNCOV
68
            $orders[] = $this->getOrder(
×
69
                $resourceClass,
×
70
                $property,
×
71
                $this->defaultDirection
×
72
            );
×
73
        }
74

UNCOV
75
        if (!$orders) {
×
76
            return $requestBody;
×
77
        }
78

UNCOV
79
        $requestBody['sort'] = array_merge_recursive($requestBody['sort'] ?? [], $orders);
×
80

UNCOV
81
        return $requestBody;
×
82
    }
83

84
    private function getOrder(string $resourceClass, string $property, string $direction): array
85
    {
UNCOV
86
        $order = ['order' => strtolower($direction)];
×
87

UNCOV
88
        if (null !== $nestedPath = $this->getNestedFieldPath($resourceClass, $property)) {
×
89
            $nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath, $resourceClass);
×
90
            $order['nested'] = ['path' => $nestedPath];
×
91
        }
92

UNCOV
93
        $property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property, $resourceClass);
×
94

UNCOV
95
        return [$property => $order];
×
96
    }
97
}
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