Passed
Push — main ( 67265c...6a5ba9 )
by Martin
36:25 queued 21:26
created

ArrayToJson   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 24
ccs 10
cts 10
cp 1
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 ARRAY_TO_JSON().
13
 *
14
 * Returns the array as a JSON array. A PostgreSQL multidimensional array becomes a JSON array of arrays.
15
 * Line feeds will be added between dimension-1 elements if pretty_bool is true.
16
 *
17
 * @see https://www.postgresql.org/docs/16/functions-json.html
18
 * @since 0.10
19
 *
20
 * @author Martin Georgiev <[email protected]>
21
 *
22
 * @example Using it in DQL: "SELECT ARRAY_TO_JSON(e.array1) FROM Entity e"
23
 * @example Using it in DQL with pretty_bool: "SELECT ARRAY_TO_JSON(e.array1, 'true') FROM Entity e"
24
 */
25
class ArrayToJson extends BaseVariadicFunction
26
{
27
    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\ArrayToJson.
Loading history...
28
29 3
    protected function getNodeMappingPattern(): array
30
    {
31 3
        return ['StringPrimary'];
32
    }
33
34 3
    protected function customizeFunction(): void
35
    {
36 3
        $this->setFunctionPrototype('array_to_json(%s)');
37
    }
38
39 3
    protected function validateArguments(Node ...$arguments): void
40
    {
41 3
        $argumentCount = \count($arguments);
42 3
        if ($argumentCount < 1 || $argumentCount > 2) {
43 1
            throw InvalidArgumentForVariadicFunctionException::between('array_to_json', 1, 2);
44
        }
45
46
        // Validate that the second parameter is a valid boolean if provided
47 2
        if ($argumentCount === 2) {
48 2
            $this->validateBoolean($arguments[1], 'ARRAY_TO_JSON');
49
        }
50
    }
51
}
52