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

api-platform / core / 14967095168

12 May 2025 08:08AM UTC coverage: 22.155% (+13.7%) from 8.457%
14967095168

Pull #7135

github

web-flow
Merge 574a2b863 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

1 of 2 new or added lines in 2 files covered. (50.0%)

120 existing lines in 12 files now uncovered.

10846 of 48956 relevant lines covered (22.15%)

10.12 hits per line

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

71.88
/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
            $description[$propertyName] = [
2✔
57
                'property' => $propertyName,
2✔
58
                'type' => 'string',
2✔
59
                'required' => false,
2✔
60
                'schema' => [
2✔
61
                    'type' => 'string',
2✔
62
                    'enum' => array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases()),
2✔
63
                ],
2✔
64
            ];
2✔
65
        }
66

67
        return $description;
2✔
68
    }
69

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

72
    abstract protected function getLogger(): LoggerInterface;
73

74
    abstract protected function normalizePropertyName(string $property): string;
75

76
    /**
77
     * Determines whether the given property refers to a backed enum field.
78
     */
79
    abstract protected function isBackedEnumField(string $property, string $resourceClass): bool;
80

81
    private function normalizeValue($value, string $property): mixed
82
    {
83
        $firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
2✔
84
        if (
85
            \is_int($firstCase?->value)
2✔
86
            && false !== filter_var($value, \FILTER_VALIDATE_INT)
2✔
87
        ) {
88
            $value = (int) $value;
1✔
89
        }
90

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

93
        if (\in_array($value, $values, true)) {
2✔
94
            return $value;
2✔
95
        }
96

UNCOV
97
        $this->getLogger()->notice('Invalid filter ignored', [
×
UNCOV
98
            'exception' => new InvalidArgumentException(\sprintf('Invalid backed enum value for "%s" property, expected one of ( "%s" )',
×
UNCOV
99
                $property,
×
UNCOV
100
                implode('" | "', $values)
×
UNCOV
101
            )),
×
UNCOV
102
        ]);
×
103

UNCOV
104
        return null;
×
105
    }
106
}
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