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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

100.0
/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 = [];
469✔
37

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

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

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

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

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

89
            return null;
20✔
90
        }
91

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

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

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

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

108
            return null;
14✔
109
        }
110

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

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

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