Test Failed
Push — regexp ( bbbe5d )
by Martin
12:32 queued 21s
created

DateSubtract::validateArguments()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 10
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\Node;
8
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException;
9
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\TimezoneValidationTrait;
10
11
/**
12
 * Implementation of PostgreSQL DATE_SUBTRACT().
13
 *
14
 * Subtracts an interval from a timestamp with time zone, computing times of day and daylight-savings
15
 * adjustments according to the time zone.
16
 *
17
 * @see https://www.postgresql.org/docs/16/functions-datetime.html
18
 * @since 3.1
19
 *
20
 * @author Martin Georgiev <[email protected]>
21
 *
22
 * @example Using it in DQL: "SELECT DATE_SUBTRACT(e.timestampWithTz, '1 day', 'Europe/Sofia') FROM Entity e"
23
 */
24
class DateSubtract extends BaseVariadicFunction
25
{
26
    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\...\Functions\DateSubtract.
Loading history...
27
28
    protected function getNodeMappingPattern(): array
29
    {
30
        return ['StringPrimary'];
31
    }
32
33
    protected function customizeFunction(): void
34
    {
35
        $this->setFunctionPrototype('date_subtract(%s)');
36
    }
37
38
    protected function validateArguments(Node ...$arguments): void
39
    {
40
        $argumentCount = \count($arguments);
41
        if ($argumentCount < 2 || $argumentCount > 3) {
42
            throw InvalidArgumentForVariadicFunctionException::between('date_subtract', 2, 3);
43
        }
44
45
        // Validate that the third parameter is a valid timezone if provided
46
        if ($argumentCount === 3) {
47
            $this->validateTimezone($arguments[2], 'DATE_SUBTRACT');
48
        }
49
    }
50
}
51