Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Bridge/Doctrine/Common/Filter/DateFilterTrait.php (5 issues)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Bridge\Doctrine\Common\Filter;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
17
18
/**
19
 * Trait for filtering the collection by date intervals.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 * @author Théo FIDRY <[email protected]>
23
 * @author Alan Poulain <[email protected]>
24
 */
25
trait DateFilterTrait
26
{
27
    use PropertyHelperTrait;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getDescription(string $resourceClass): array
33
    {
34
        $description = [];
35
36
        $properties = $this->getProperties();
37
        if (null === $properties) {
38
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
39
        }
40
41
        foreach ($properties as $property => $nullManagement) {
42
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isDateField($property, $resourceClass)) {
43
                continue;
44
            }
45
46
            $description += $this->getFilterDescription($property, self::PARAMETER_BEFORE);
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...Trait::PARAMETER_BEFORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
            $description += $this->getFilterDescription($property, self::PARAMETER_STRICTLY_BEFORE);
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...RAMETER_STRICTLY_BEFORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
            $description += $this->getFilterDescription($property, self::PARAMETER_AFTER);
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...rTrait::PARAMETER_AFTER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
49
            $description += $this->getFilterDescription($property, self::PARAMETER_STRICTLY_AFTER);
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...ARAMETER_STRICTLY_AFTER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
50
        }
51
52
        return $description;
53
    }
54
55
    abstract protected function getProperties(): ?array;
56
57
    abstract protected function normalizePropertyName($property);
58
59
    /**
60
     * Determines whether the given property refers to a date field.
61
     */
62
    protected function isDateField(string $property, string $resourceClass): bool
63
    {
64
        return isset(self::DOCTRINE_DATE_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...it::DOCTRINE_DATE_TYPES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
    }
66
67
    /**
68
     * Gets filter description.
69
     */
70
    protected function getFilterDescription(string $property, string $period): array
71
    {
72
        $propertyName = $this->normalizePropertyName($property);
73
74
        return [
75
            sprintf('%s[%s]', $propertyName, $period) => [
76
                'property' => $propertyName,
77
                'type' => \DateTimeInterface::class,
78
                'required' => false,
79
            ],
80
        ];
81
    }
82
}
83