• 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

81.25
/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 = [];
223✔
36

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

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

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

UNCOV
54
        return $description;
222✔
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);
222✔
69

UNCOV
70
        return [
222✔
UNCOV
71
            \sprintf('%s[%s]', $propertyName, $operator) => [
222✔
UNCOV
72
                'property' => $propertyName,
222✔
UNCOV
73
                'type' => 'string',
222✔
UNCOV
74
                'required' => false,
222✔
UNCOV
75
            ],
222✔
UNCOV
76
        ];
222✔
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];
31✔
82

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

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

94
            return null;
2✔
95
        }
96

UNCOV
97
        return $values;
29✔
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)) {
8✔
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])) {
8✔
114
            $this->getLogger()->notice('Invalid filter ignored', [
1✔
115
                'exception' => new InvalidArgumentException(\sprintf('Invalid values for "[%s]" range, expected numbers', self::PARAMETER_BETWEEN)),
1✔
116
            ]);
1✔
117

118
            return null;
1✔
119
        }
120

UNCOV
121
        return [$values[0] + 0, $values[1] + 0]; // coerce to the right types.
7✔
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)) {
24✔
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.
24✔
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