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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 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/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 = [];
×
43

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

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

UNCOV
61
        return $description;
×
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)]);
×
76
    }
77

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

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

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

UNCOV
97
        return null;
×
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