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

api-platform / core / 16705318661

03 Aug 2025 01:05PM UTC coverage: 0.0% (-21.9%) from 21.944%
16705318661

Pull #7317

github

web-flow
Merge 1ca8642ff into d06b1a0a0
Pull Request #7317: Fix/4372 skip null values in hal

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

11680 existing lines in 376 files now uncovered.

0 of 51817 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Doctrine/Common/Filter/RangeFilterTrait.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 range.
22
 *
23
 * @author Lee Siong Chan <ahlee2326@me.com>
24
 * @author Alan Poulain <contact@alanpoulain.eu>
25
 */
26
trait RangeFilterTrait
27
{
28
    use PropertyHelperTrait;
29

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

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

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

UNCOV
47
            $description += $this->getFilterDescription($property, self::PARAMETER_BETWEEN);
×
UNCOV
48
            $description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN);
×
UNCOV
49
            $description += $this->getFilterDescription($property, self::PARAMETER_GREATER_THAN_OR_EQUAL);
×
UNCOV
50
            $description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN);
×
UNCOV
51
            $description += $this->getFilterDescription($property, self::PARAMETER_LESS_THAN_OR_EQUAL);
×
52
        }
53

UNCOV
54
        return $description;
×
55
    }
56

57
    abstract protected function getProperties(): ?array;
58

59
    abstract protected function getLogger(): LoggerInterface;
60

61
    abstract protected function normalizePropertyName(string $property): string;
62

63
    /**
64
     * Gets filter description.
65
     */
66
    protected function getFilterDescription(string $fieldName, string $operator): array
67
    {
UNCOV
68
        $propertyName = $this->normalizePropertyName($fieldName);
×
69

UNCOV
70
        return [
×
UNCOV
71
            \sprintf('%s[%s]', $propertyName, $operator) => [
×
UNCOV
72
                'property' => $propertyName,
×
UNCOV
73
                'type' => 'string',
×
UNCOV
74
                'required' => false,
×
UNCOV
75
            ],
×
UNCOV
76
        ];
×
77
    }
78

79
    private function normalizeValues(array $values, string $property): ?array
80
    {
UNCOV
81
        $operators = [self::PARAMETER_BETWEEN, self::PARAMETER_GREATER_THAN, self::PARAMETER_GREATER_THAN_OR_EQUAL, self::PARAMETER_LESS_THAN, self::PARAMETER_LESS_THAN_OR_EQUAL];
×
82

UNCOV
83
        foreach ($values as $operator => $value) {
×
UNCOV
84
            if (!\in_array($operator, $operators, true)) {
×
85
                unset($values[$operator]);
×
86
            }
87
        }
88

UNCOV
89
        if (empty($values)) {
×
90
            $this->getLogger()->notice('Invalid filter ignored', [
×
91
                'exception' => new InvalidArgumentException(\sprintf('At least one valid operator ("%s") is required for "%s" property', implode('", "', $operators), $property)),
×
92
            ]);
×
93

94
            return null;
×
95
        }
96

UNCOV
97
        return $values;
×
98
    }
99

100
    /**
101
     * Normalize the values array for between operator.
102
     */
103
    private function normalizeBetweenValues(array $values): ?array
104
    {
UNCOV
105
        if (2 !== \count($values)) {
×
106
            $this->getLogger()->notice('Invalid filter ignored', [
×
107
                'exception' => new InvalidArgumentException(\sprintf('Invalid format for "[%s]", expected "<min>..<max>"', self::PARAMETER_BETWEEN)),
×
108
            ]);
×
109

110
            return null;
×
111
        }
112

UNCOV
113
        if (!is_numeric($values[0]) || !is_numeric($values[1])) {
×
114
            $this->getLogger()->notice('Invalid filter ignored', [
×
115
                'exception' => new InvalidArgumentException(\sprintf('Invalid values for "[%s]" range, expected numbers', self::PARAMETER_BETWEEN)),
×
116
            ]);
×
117

118
            return null;
×
119
        }
120

UNCOV
121
        return [$values[0] + 0, $values[1] + 0]; // coerce to the right types.
×
122
    }
123

124
    /**
125
     * Normalize the value.
126
     */
127
    private function normalizeValue(string $value, string $operator): float|int|null
128
    {
UNCOV
129
        if (!is_numeric($value)) {
×
130
            $this->getLogger()->notice('Invalid filter ignored', [
×
131
                'exception' => new InvalidArgumentException(\sprintf('Invalid value for "[%s]", expected number', $operator)),
×
132
            ]);
×
133

134
            return null;
×
135
        }
136

UNCOV
137
        return $value + 0; // coerce $value to the right type.
×
138
    }
139
}
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