Completed
Push — 2.x ( 6c47ee...c394f3 )
by Aleksei
24s queued 15s
created

CompileJsonLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\Postgres\Injection;
13
14
class CompileJsonLength extends PostgresJsonExpression
15
{
16
    /**
17
     * @param non-empty-string $statement
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
18
     * @param int<0, max> $length
19
     * @param non-empty-string $operator
20
     */
21
    public function __construct(
22
        string $statement,
23
        int $length,
24
        protected string $operator
25
    ) {
26
        parent::__construct($statement, $length);
27
    }
28
29
    protected function compile(string $statement): string
30
    {
31
        $path = $this->getPath($statement);
32
        $attribute = $this->getAttribute($statement);
33
        $field = $this->getField($statement);
34
35
        $fullPath = !empty($path)
36
            ? \sprintf('%s->%s->%s', $field, $path, $attribute)
37
            : \sprintf('%s->%s', $field, $attribute);
38
39
        return \sprintf('jsonb_array_length((%s)::jsonb) %s ?', $fullPath, $this->operator);
40
    }
41
}
42