• 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

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

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

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

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

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

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

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

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

UNCOV
94
            return null;
2✔
95
        }
96

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

UNCOV
118
            return null;
1✔
119
        }
120

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