Passed
Push — main ( c3cb08...0cda90 )
by Martin
11:26
created

JsonbPathExists::validateArguments()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

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
ccs 6
cts 6
cp 1
crap 4
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\BooleanValidationTrait;
10
11
/**
12
 * Implementation of PostgreSQL JSONB_PATH_EXISTS().
13
 *
14
 * Checks whether the JSON path returns any item for the specified JSON value.
15
 *
16
 * @see https://www.postgresql.org/docs/14/functions-json.html
17
 * @since 3.1
18
 *
19
 * @author Martin Georgiev <[email protected]>
20
 *
21
 * @example Using it in DQL: "SELECT JSONB_PATH_EXISTS(e.jsonbData, '$.a[*] ? (@ > 2)')"
22
 */
23
class JsonbPathExists extends BaseVariadicFunction
24
{
25
    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\...nctions\JsonbPathExists.
Loading history...
26
27 4
    protected function customizeFunction(): void
28
    {
29 4
        $this->setFunctionPrototype('jsonb_path_exists(%s)');
30
    }
31
32 4
    protected function validateArguments(Node ...$arguments): void
33
    {
34 4
        $argumentCount = \count($arguments);
35 4
        if ($argumentCount < 2 || $argumentCount > 4) {
36 2
            throw InvalidArgumentForVariadicFunctionException::between('jsonb_path_exists', 2, 4);
37
        }
38
39
        // Validate that the fourth parameter is a valid boolean if provided
40 2
        if ($argumentCount === 4) {
41 2
            $this->validateBoolean($arguments[3], 'JSONB_PATH_EXISTS');
42
        }
43
    }
44
}
45