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

api-platform / core / 17063252025

19 Aug 2025 07:54AM UTC coverage: 22.209% (-0.03%) from 22.236%
17063252025

push

github

soyuka
Merge 4.1

11 of 157 new or added lines in 17 files covered. (7.01%)

2 existing lines in 2 files now uncovered.

11699 of 52676 relevant lines covered (22.21%)

24.2 hits per line

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

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

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

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

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

53
        return $description;
20✔
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
    {
65
        return isset(self::DOCTRINE_DATE_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
50✔
66
    }
67

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

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

84
    private function normalizeValue(mixed $value, string $operator): ?string
85
    {
86
        if (false === \is_string($value)) {
38✔
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

94
        if ('' === $value) {
38✔
NEW
95
            $this->getLogger()->notice('Invalid filter ignored', [
×
NEW
96
                'exception' => new InvalidArgumentException(\sprintf('Invalid value for "[%s]", expected non-empty string', $operator)),
×
NEW
97
            ]);
×
98

NEW
99
            return null;
×
100
        }
101

102
        return $value;
38✔
103
    }
104
}
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