Passed
Push — main ( 0cda90...67265c )
by Martin
40:49 queued 25:50
created

RowToJson::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 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
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\Exception\InvalidArgumentForVariadicFunctionException;
9
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\BooleanValidationTrait;
10
11
/**
12
 * Implementation of PostgreSQL ROW_TO_JSON().
13
 *
14
 * Returns the row as a JSON object. Line feeds will be added between level-1 elements if pretty_bool 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: "SELECT ROW_TO_JSON(e.row) FROM Entity e"
22
 * @example Using it in DQL with pretty_bool: "SELECT ROW_TO_JSON(e.row, 'true') FROM Entity e"
23
 */
24
class RowToJson 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\...AST\Functions\RowToJson.
Loading history...
27
28 3
    protected function customizeFunction(): void
29
    {
30 3
        $this->setFunctionPrototype('row_to_json(%s)');
31
    }
32
33 3
    protected function validateArguments(Node ...$arguments): void
34
    {
35 3
        $argumentCount = \count($arguments);
36 3
        if ($argumentCount < 1 || $argumentCount > 2) {
37 1
            throw InvalidArgumentForVariadicFunctionException::between('row_to_json', 1, 2);
38
        }
39
40
        // Validate that the second parameter is a valid boolean if provided
41 2
        if ($argumentCount === 2) {
42 2
            $this->validateBoolean($arguments[1], 'ROW_TO_JSON');
43
        }
44
    }
45
}
46