• 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

97.44
/src/Doctrine/Common/Filter/NumericFilterTrait.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 numeric values.
22
 *
23
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
24
 * @author Teoh Han Hui <teohhanhui@gmail.com>
25
 * @author Alan Poulain <contact@alanpoulain.eu>
26
 */
27
trait NumericFilterTrait
28
{
29
    use PropertyHelperTrait;
30

31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getDescription(string $resourceClass): array
35
    {
UNCOV
36
        $description = [];
220✔
37

UNCOV
38
        $properties = $this->getProperties();
220✔
UNCOV
39
        if (null === $properties) {
220✔
UNCOV
40
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
2✔
41
        }
42

UNCOV
43
        foreach ($properties as $property => $unused) {
219✔
UNCOV
44
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isNumericField($property, $resourceClass)) {
219✔
45
                continue;
×
46
            }
47

UNCOV
48
            $propertyName = $this->normalizePropertyName($property);
219✔
UNCOV
49
            $filterParameterNames = [$propertyName, $propertyName.'[]'];
219✔
UNCOV
50
            foreach ($filterParameterNames as $filterParameterName) {
219✔
UNCOV
51
                $description[$filterParameterName] = [
219✔
UNCOV
52
                    'property' => $propertyName,
219✔
UNCOV
53
                    'type' => $this->getType((string) $this->getDoctrineFieldType($property, $resourceClass)),
219✔
UNCOV
54
                    'required' => false,
219✔
UNCOV
55
                    'is_collection' => str_ends_with((string) $filterParameterName, '[]'),
219✔
UNCOV
56
                ];
219✔
57
            }
58
        }
59

UNCOV
60
        return $description;
219✔
61
    }
62

63
    /**
64
     * Gets the PHP type corresponding to this Doctrine type.
65
     */
66
    abstract protected function getType(?string $doctrineType = null): string;
67

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

70
    abstract protected function getLogger(): LoggerInterface;
71

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

74
    /**
75
     * Determines whether the given property refers to a numeric field.
76
     */
77
    protected function isNumericField(string $property, string $resourceClass): bool
78
    {
UNCOV
79
        return isset(self::DOCTRINE_NUMERIC_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
228✔
80
    }
81

82
    protected function normalizeValues($value, string $property): ?array
83
    {
UNCOV
84
        if (!is_numeric($value) && (!\is_array($value) || !$this->isNumericArray($value))) {
23✔
UNCOV
85
            $this->getLogger()->notice('Invalid filter ignored', [
10✔
UNCOV
86
                'exception' => new InvalidArgumentException(\sprintf('Invalid numeric value for "%s" property', $property)),
10✔
UNCOV
87
            ]);
10✔
88

UNCOV
89
            return null;
10✔
90
        }
91

UNCOV
92
        $values = (array) $value;
13✔
93

UNCOV
94
        foreach ($values as $key => $val) {
13✔
UNCOV
95
            if (!\is_int($key)) {
13✔
96
                unset($values[$key]);
7✔
97

98
                continue;
7✔
99
            }
UNCOV
100
            $values[$key] = $val + 0; // coerce $val to the right type.
6✔
101
        }
102

UNCOV
103
        if (empty($values)) {
13✔
104
            $this->getLogger()->notice('Invalid filter ignored', [
7✔
105
                'exception' => new InvalidArgumentException(\sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)),
7✔
106
            ]);
7✔
107

108
            return null;
7✔
109
        }
110

UNCOV
111
        return array_values($values);
6✔
112
    }
113

114
    protected function isNumericArray(array $values): bool
115
    {
116
        foreach ($values as $value) {
12✔
117
            if (!is_numeric($value)) {
12✔
118
                return false;
3✔
119
            }
120
        }
121

122
        return true;
9✔
123
    }
124
}
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