Passed
Push — main ( 6a5ba9...e111dd )
by Martin
12:18
created

JsonbInsert::getMaxArgumentCount()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Traits\BooleanValidationTrait;
9
10
/**
11
 * Implementation of PostgreSQL JSONB_INSERT().
12
 *
13
 * Inserts a new value into a JSONB field at the specified path.
14
 * If the path already exists, the value is not changed unless the last parameter is true.
15
 *
16
 * @see https://www.postgresql.org/docs/16/functions-json.html
17
 * @since 0.10
18
 *
19
 * @author Martin Georgiev <[email protected]>
20
 *
21
 * @example Using it in DQL with path and value: "SELECT JSONB_INSERT(e.jsonbData, '{country}', '{\"iso_3166_a3_code\":\"BGR\"}') FROM Entity e"
22
 * @example Using it in DQL with create_if_missing flag: "SELECT JSONB_INSERT(e.jsonbData, '{country}', '{\"iso_3166_a3_code\":\"BGR\"}', true) FROM Entity e"
23
 */
24
class JsonbInsert extends BaseVariadicFunction
25
{
26
    use BooleanValidationTrait;
0 ignored issues
show
Bug introduced by
The trait MartinGeorgiev\Doctrine\...\BooleanValidationTrait requires the property $value which is not provided by MartinGeorgiev\Doctrine\...T\Functions\JsonbInsert.
Loading history...
27
28 4
    protected function getNodeMappingPattern(): array
29
    {
30 4
        return ['StringPrimary'];
31
    }
32
33 4
    protected function getFunctionName(): string
34
    {
35 4
        return 'jsonb_insert';
36
    }
37
38 4
    protected function getMinArgumentCount(): int
39
    {
40 4
        return 3;
41
    }
42
43 4
    protected function getMaxArgumentCount(): int
44
    {
45 4
        return 4;
46
    }
47
48 3
    protected function validateArguments(Node ...$arguments): void
49
    {
50 3
        parent::validateArguments(...$arguments);
51
52
        // Validate that the fourth parameter is a valid boolean if provided
53 2
        if (\count($arguments) === 4) {
54 2
            $this->validateBoolean($arguments[3], $this->getFunctionName());
55
        }
56
    }
57
}
58