• 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

76.32
/src/Doctrine/Common/Filter/BackedEnumFilterTrait.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
use Psr\Log\LoggerInterface;
19

20
/**
21
 * Trait for filtering the collection by backed enum values.
22
 *
23
 * Filters collection on equality of backed enum properties.
24
 *
25
 * For each property passed, if the resource does not have such property or if
26
 * the value is not one of cases the property is ignored.
27
 *
28
 * @author Rémi Marseille <marseille.remi@gmail.com>
29
 */
30
trait BackedEnumFilterTrait
31
{
32
    use PropertyHelperTrait;
33

34
    /**
35
     * @var array<string, class-string>
36
     */
37
    private array $enumTypes;
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getDescription(string $resourceClass): array
43
    {
44
        $description = [];
2✔
45

46
        $properties = $this->getProperties();
2✔
47
        if (null === $properties) {
2✔
48
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
×
49
        }
50

51
        foreach ($properties as $property => $unused) {
2✔
52
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBackedEnumField($property, $resourceClass)) {
2✔
53
                continue;
×
54
            }
55
            $propertyName = $this->normalizePropertyName($property);
2✔
56
            $filterParameterNames = [$propertyName];
2✔
57
            $filterParameterNames[] = $propertyName.'[]';
2✔
58

59
            foreach ($filterParameterNames as $filterParameterName) {
2✔
60
                $isCollection = str_ends_with($filterParameterName, '[]');
2✔
61

62
                $enumValues = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());
2✔
63

64
                $schema = $isCollection
2✔
65
                    ? ['type' => 'array', 'items' => ['type' => 'string', 'enum' => $enumValues]]
2✔
66
                    : ['type' => 'string', 'enum' => $enumValues];
2✔
67

68
                $description[$filterParameterName] = [
2✔
69
                    'property' => $propertyName,
2✔
70
                    'type' => 'string',
2✔
71
                    'required' => false,
2✔
72
                    'is_collection' => $isCollection,
2✔
73
                    'schema' => $schema,
2✔
74
                ];
2✔
75
            }
76
        }
77

78
        return $description;
2✔
79
    }
80

81
    abstract protected function getProperties(): ?array;
82

83
    abstract protected function getLogger(): LoggerInterface;
84

85
    abstract protected function normalizePropertyName(string $property): string;
86

87
    /**
88
     * Determines whether the given property refers to a backed enum field.
89
     */
90
    abstract protected function isBackedEnumField(string $property, string $resourceClass): bool;
91

92
    private function normalizeValue(mixed $value, string $property): mixed
93
    {
94
        $firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
2✔
95
        if (
96
            \is_int($firstCase?->value)
2✔
97
            && false !== filter_var($value, \FILTER_VALIDATE_INT)
2✔
98
        ) {
99
            $value = (int) $value;
1✔
100
        }
101

102
        $values = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());
2✔
103

104
        if (\in_array($value, $values, true)) {
2✔
105
            return $value;
2✔
106
        }
107

108
        $this->getLogger()->notice('Invalid filter ignored', [
×
109
            'exception' => new InvalidArgumentException(\sprintf('Invalid backed enum value for "%s" property, expected one of ( "%s" )',
×
110
                $property,
×
111
                implode('" | "', $values)
×
112
            )),
×
113
        ]);
×
114

115
        return null;
×
116
    }
117
}
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