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

api-platform / core / 15133115926

20 May 2025 08:49AM UTC coverage: 27.496% (+0.003%) from 27.493%
15133115926

Pull #7161

github

web-flow
Merge 14375cec4 into efd00d1c2
Pull Request #7161: fix(metadata): infer parameter string type from schema

6 of 9 new or added lines in 3 files covered. (66.67%)

10 existing lines in 3 files now uncovered.

13478 of 49018 relevant lines covered (27.5%)

74.99 hits per line

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

83.33
/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
    {
35
        $description = [];
475✔
36

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

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

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

54
        return $description;
473✔
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
    {
68
        $propertyName = $this->normalizePropertyName($fieldName);
464✔
69

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

79
    private function normalizeValues(array $values, string $property): ?array
80
    {
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];
62✔
82

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

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

UNCOV
94
            return null;
4✔
95
        }
96

97
        return $values;
58✔
98
    }
99

100
    /**
101
     * Normalize the values array for between operator.
102
     */
103
    private function normalizeBetweenValues(array $values): ?array
104
    {
105
        if (2 !== \count($values)) {
16✔
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

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

118
            return null;
2✔
119
        }
120

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

124
    /**
125
     * Normalize the value.
126
     */
127
    private function normalizeValue(string $value, string $operator): float|int|null
128
    {
129
        if (!is_numeric($value)) {
48✔
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

137
        return $value + 0; // coerce $value to the right type.
48✔
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