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

JsonbPathQueryFirst   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 24
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodeMappingPattern() 0 3 1
A validateArguments() 0 10 4
A customizeFunction() 0 3 1
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_QUERY_FIRST().
13
 *
14
 * Returns the first JSON item returned by the JSON path for the specified JSON value.
15
 * Returns NULL if there are no results.
16
 *
17
 * @see https://www.postgresql.org/docs/14/functions-json.html
18
 * @since 3.1
19
 *
20
 * @author Martin Georgiev <[email protected]>
21
 *
22
 * @example Using it in DQL: "SELECT JSONB_PATH_QUERY_FIRST(e.jsonbData, '$.items[*] ? (@.price > 100)') FROM Entity e"
23
 */
24
class JsonbPathQueryFirst 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\...ons\JsonbPathQueryFirst.
Loading history...
27
28
    protected function getNodeMappingPattern(): array
29
    {
30
        return ['StringPrimary'];
31
    }
32
33
    protected function customizeFunction(): void
34
    {
35
        $this->setFunctionPrototype('jsonb_path_query_first(%s)');
36
    }
37
38
    protected function validateArguments(Node ...$arguments): void
39
    {
40
        $argumentCount = \count($arguments);
41
        if ($argumentCount < 2 || $argumentCount > 4) {
42
            throw InvalidArgumentForVariadicFunctionException::between('jsonb_path_query_first', 2, 4);
43
        }
44
45
        // Validate that the fourth parameter is a valid boolean if provided
46
        if ($argumentCount === 4) {
47
            $this->validateBoolean($arguments[3], 'JSONB_PATH_QUERY_FIRST');
48
        }
49
    }
50
}
51