• 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

85.19
/src/Doctrine/Common/Filter/DateFilterTrait.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

19
/**
20
 * Trait for filtering the collection by date intervals.
21
 *
22
 * @author Kévin Dunglas <dunglas@gmail.com>
23
 * @author Théo FIDRY <theo.fidry@gmail.com>
24
 * @author Alan Poulain <contact@alanpoulain.eu>
25
 */
26
trait DateFilterTrait
27
{
28
    use PropertyHelperTrait;
29

30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getDescription(string $resourceClass): array
34
    {
UNCOV
35
        $description = [];
242✔
36

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

UNCOV
42
        foreach ($properties as $property => $nullManagement) {
242✔
UNCOV
43
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isDateField($property, $resourceClass)) {
242✔
UNCOV
44
                continue;
220✔
45
            }
46

UNCOV
47
            $description += $this->getFilterDescription($property, self::PARAMETER_BEFORE);
240✔
UNCOV
48
            $description += $this->getFilterDescription($property, self::PARAMETER_STRICTLY_BEFORE);
240✔
UNCOV
49
            $description += $this->getFilterDescription($property, self::PARAMETER_AFTER);
240✔
UNCOV
50
            $description += $this->getFilterDescription($property, self::PARAMETER_STRICTLY_AFTER);
240✔
51
        }
52

UNCOV
53
        return $description;
241✔
54
    }
55

56
    abstract protected function getProperties(): ?array;
57

58
    abstract protected function normalizePropertyName(string $property): string;
59

60
    /**
61
     * Determines whether the given property refers to a date field.
62
     */
63
    protected function isDateField(string $property, string $resourceClass): bool
64
    {
UNCOV
65
        return isset(self::DOCTRINE_DATE_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
256✔
66
    }
67

68
    /**
69
     * Gets filter description.
70
     */
71
    protected function getFilterDescription(string $property, string $period): array
72
    {
UNCOV
73
        $propertyName = $this->normalizePropertyName($property);
240✔
74

UNCOV
75
        return [
240✔
UNCOV
76
            \sprintf('%s[%s]', $propertyName, $period) => [
240✔
UNCOV
77
                'property' => $propertyName,
240✔
UNCOV
78
                'type' => \DateTimeInterface::class,
240✔
UNCOV
79
                'required' => false,
240✔
UNCOV
80
            ],
240✔
UNCOV
81
        ];
240✔
82
    }
83

84
    private function normalizeValue($value, string $operator): ?string
85
    {
UNCOV
86
        if (false === \is_string($value)) {
34✔
87
            $this->getLogger()->notice('Invalid filter ignored', [
×
88
                'exception' => new InvalidArgumentException(\sprintf('Invalid value for "[%s]", expected string', $operator)),
×
89
            ]);
×
90

91
            return null;
×
92
        }
93

UNCOV
94
        return $value;
34✔
95
    }
96
}
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