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

api-platform / core / 17054069864

18 Aug 2025 10:27PM UTC coverage: 21.952% (+0.2%) from 21.769%
17054069864

Pull #7151

github

web-flow
Merge 0da010d8d into 6491bfc7a
Pull Request #7151: fix: 7119 parameter array shape uses invalid syntax

11524 of 52497 relevant lines covered (21.95%)

11.86 hits per line

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

78.79
/src/Doctrine/Common/Filter/OrderFilterTrait.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\Common\Filter;
15

16
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18

19
/**
20
 * Trait for ordering the collection by given properties.
21
 *
22
 * @author Kévin Dunglas <dunglas@gmail.com>
23
 * @author Théo FIDRY <theo.fidry@gmail.com>
24
 * @author Alan Poulain <contact@alanpoulain.eu>
25
 */
26
trait OrderFilterTrait
27
{
28
    use PropertyHelperTrait;
29

30
    /**
31
     * @var string Keyword used to retrieve the value
32
     */
33
    protected string $orderParameterName;
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getDescription(string $resourceClass): array
39
    {
40
        $description = [];
9✔
41

42
        $properties = $this->getProperties();
9✔
43
        if (null === $properties && $fieldNames = $this->getClassMetadata($resourceClass)->getFieldNames()) {
9✔
44
            $properties = array_fill_keys($fieldNames, null);
×
45
        }
46

47
        foreach ($properties ?? [] as $property => $propertyOptions) {
9✔
48
            if (!$this->isPropertyMapped($property, $resourceClass)) {
9✔
49
                continue;
5✔
50
            }
51
            $propertyName = $this->normalizePropertyName($property);
8✔
52
            $description[\sprintf('%s[%s]', $this->orderParameterName, $propertyName)] = [
8✔
53
                'property' => $propertyName,
8✔
54
                'type' => 'string',
8✔
55
                'required' => false,
8✔
56
                'schema' => [
8✔
57
                    'type' => 'string',
8✔
58
                    'default' => strtolower($propertyOptions['default_direction'] ?? OrderFilterInterface::DIRECTION_ASC),
8✔
59
                    'enum' => [
8✔
60
                        strtolower(OrderFilterInterface::DIRECTION_ASC),
8✔
61
                        strtolower(OrderFilterInterface::DIRECTION_DESC),
8✔
62
                    ],
8✔
63
                ],
8✔
64
            ];
8✔
65
        }
66

67
        return $description;
8✔
68
    }
69

70
    abstract protected function getProperties(): ?array;
71

72
    abstract protected function normalizePropertyName(string $property): string;
73

74
    private function normalizeValue(mixed $value, string $property): ?string
75
    {
76
        if (empty($value) && null !== $defaultDirection = $this->getProperties()[$property]['default_direction'] ?? null) {
11✔
77
            // fallback to default direction
78
            $value = $defaultDirection;
×
79
        }
80

81
        if (!\is_string($value)) {
11✔
82
            $this->getLogger()->notice('Invalid filter ignored', [
×
83
                'exception' => new InvalidArgumentException(\sprintf('Invalid string value for "%s" property', $property)),
×
84
            ]);
×
85

86
            return null;
×
87
        }
88

89
        $value = strtoupper($value);
11✔
90
        if (!\in_array($value, [self::DIRECTION_ASC, self::DIRECTION_DESC], true)) {
11✔
91
            return null;
×
92
        }
93

94
        return $value;
11✔
95
    }
96
}
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