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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

92.86
/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

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

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

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

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

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

UNCOV
66
        return $description;
237✔
67
    }
68

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

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

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

UNCOV
80
        $value = strtoupper($value);
28✔
UNCOV
81
        if (!\in_array($value, [self::DIRECTION_ASC, self::DIRECTION_DESC], true)) {
28✔
82
            return null;
×
83
        }
84

UNCOV
85
        return $value;
28✔
86
    }
87
}
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