Passed
Pull Request — main (#493)
by
unknown
10:39
created

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