• 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

100.0
/src/Doctrine/Common/Filter/BooleanFilterTrait.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 boolean values.
22
 *
23
 * Filters collection on equality of boolean properties. The value is specified
24
 * as one of ( "true" | "false" | "1" | "0" ) in the query.
25
 *
26
 * For each property passed, if the resource does not have such property or if
27
 * the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored.
28
 *
29
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
30
 * @author Teoh Han Hui <teohhanhui@gmail.com>
31
 * @author Alan Poulain <contact@alanpoulain.eu>
32
 */
33
trait BooleanFilterTrait
34
{
35
    use PropertyHelperTrait;
36

37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getDescription(string $resourceClass): array
41
    {
UNCOV
42
        $description = [];
228✔
43

UNCOV
44
        $properties = $this->getProperties();
228✔
UNCOV
45
        if (null === $properties) {
228✔
UNCOV
46
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
149✔
47
        }
48

UNCOV
49
        foreach ($properties as $property => $unused) {
227✔
UNCOV
50
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBooleanField($property, $resourceClass)) {
227✔
UNCOV
51
                continue;
227✔
52
            }
UNCOV
53
            $propertyName = $this->normalizePropertyName($property);
227✔
UNCOV
54
            $description[$propertyName] = [
227✔
UNCOV
55
                'property' => $propertyName,
227✔
UNCOV
56
                'type' => 'bool',
227✔
UNCOV
57
                'required' => false,
227✔
UNCOV
58
            ];
227✔
59
        }
60

UNCOV
61
        return $description;
227✔
62
    }
63

64
    abstract protected function getProperties(): ?array;
65

66
    abstract protected function getLogger(): LoggerInterface;
67

68
    abstract protected function normalizePropertyName(string $property): string;
69

70
    /**
71
     * Determines whether the given property refers to a boolean field.
72
     */
73
    protected function isBooleanField(string $property, string $resourceClass): bool
74
    {
UNCOV
75
        return isset(self::DOCTRINE_BOOLEAN_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
239✔
76
    }
77

78
    private function normalizeValue($value, string $property): ?bool
79
    {
UNCOV
80
        if (\in_array($value, [true, 'true', '1'], true)) {
24✔
UNCOV
81
            return true;
10✔
82
        }
83

UNCOV
84
        if (\in_array($value, [false, 'false', '0'], true)) {
14✔
UNCOV
85
            return false;
10✔
86
        }
87

UNCOV
88
        $this->getLogger()->notice('Invalid filter ignored', [
4✔
UNCOV
89
            'exception' => new InvalidArgumentException(\sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $property, implode('" | "', [
4✔
UNCOV
90
                'true',
4✔
UNCOV
91
                'false',
4✔
UNCOV
92
                '1',
4✔
UNCOV
93
                '0',
4✔
UNCOV
94
            ]))),
4✔
UNCOV
95
        ]);
4✔
96

UNCOV
97
        return null;
4✔
98
    }
99
}
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