Passed
Pull Request — main (#493)
by
unknown
12:20
created

DateTrunc::getNodeMappingPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\AST\Literal;
8
use Doctrine\ORM\Query\AST\Node;
9
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidTimezoneException;
10
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidTruncFieldException;
11
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\TimezoneValidationTrait;
12
13
/**
14
 * Implementation of PostgreSQL DATE_TRUNC().
15
 *
16
 * @see https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
17
 * @since 3.7
18
 *
19
 * @author Jan Klan <[email protected]>
20
 *
21
 * @example Using it in DQL: "SELECT DATE_TRUNC('day', e.timestampWithTz, 'Australia/Adelaide') FROM Entity e"
22
 */
23
class DateTrunc extends BaseVariadicFunction
24
{
25
    use TimezoneValidationTrait;
0 ignored issues
show
Bug introduced by
The trait MartinGeorgiev\Doctrine\...TimezoneValidationTrait requires the property $value which is not provided by MartinGeorgiev\Doctrine\...AST\Functions\DateTrunc.
Loading history...
26
    /**
27
     * @see https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
28
     */
29
    public const FIELDS = [
30
        'microseconds',
31
        'milliseconds',
32
        'second',
33
        'minute',
34
        'hour',
35
        'day',
36
        'week',
37
        'month',
38
        'quarter',
39
        'year',
40
        'decade',
41
        'century',
42
        'millennium',
43
    ];
44
45 24
    protected function getNodeMappingPattern(): array
46
    {
47 24
        return ['StringPrimary'];
48
    }
49
50 24
    protected function getFunctionName(): string
51
    {
52 24
        return 'date_trunc';
53
    }
54
55 24
    protected function getMinArgumentCount(): int
56
    {
57 24
        return 2;
58
    }
59
60 24
    protected function getMaxArgumentCount(): int
61
    {
62 24
        return 3;
63
    }
64
65 23
    protected function validateArguments(Node ...$arguments): void
66
    {
67 23
        parent::validateArguments(...$arguments);
68
69 22
        $this->validateTruncField($arguments[0]);
70
71
        // Validate that the third parameter is a valid timezone if provided
72 18
        if (\count($arguments) === 3) {
73 5
            $this->validateTimezone($arguments[2], $this->getFunctionName());
74
        }
75
    }
76
77
    /**
78
     * Validates that the given node represents a valid trunc field value.
79
     *
80
     * @throws InvalidTruncFieldException If the field value is invalid
81
     */
82 22
    protected function validateTruncField(Node $node): void
83
    {
84 22
        if (!$node instanceof Literal || !\is_string($node->value)) {
85
            throw InvalidTimezoneException::forNonLiteralNode($node::class, $this->getFunctionName());
86
        }
87
88 22
        $field = \strtolower(\trim((string) $node->value, "'\""));
89
90 22
        if (!\in_array($field, self::FIELDS, true)) {
91 4
            throw InvalidTruncFieldException::forInvalidField($field, $this->getFunctionName());
92
        }
93
    }
94
}
95