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

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 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
    {
36
        $description = [];
685✔
37

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

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

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

60
        return $description;
685✔
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
    {
79
        return isset(self::DOCTRINE_NUMERIC_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
676✔
80
    }
81

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

89
            return null;
12✔
90
        }
91

92
        $values = (array) $value;
30✔
93

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

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

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

108
            return null;
21✔
109
        }
110

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

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

122
        return true;
27✔
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

© 2026 Coveralls, Inc