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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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 = [];
226✔
37

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

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

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

60
        return $description;
224✔
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)]);
242✔
80
    }
81

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

89
            return null;
16✔
90
        }
91

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

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

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

103
        if (empty($values)) {
16✔
UNCOV
104
            $this->getLogger()->notice('Invalid filter ignored', [
7✔
UNCOV
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✔
UNCOV
106
            ]);
7✔
107

UNCOV
108
            return null;
7✔
109
        }
110

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

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

UNCOV
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