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

CompileJsonContainsKey::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.7998
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\SQLServer\Injection;
13
14
class CompileJsonContainsKey extends SQLServerJsonExpression
15
{
16
    protected function compile(string $statement): string
17
    {
18
        $path = \explode('->', $statement);
19
        $key = $this->parseArraySyntax(\array_pop($path));
20
21
        if (\count($key) === 1) {
22
            return \sprintf(
23
                '\'%s\' IN (SELECT [key] FROM openjson(%s%s))',
24
                \array_shift($key),
25
                $this->getField($statement),
26
                $this->getPath(\implode('->', $path))
27
            );
28
        }
29
30
        $path[] = \array_shift($key);
31
32
        return \sprintf(
33
            '%s IN (SELECT [key] FROM openjson(%s%s))',
34
            \array_pop($key),
35
            $this->getField($statement),
36
            $this->getPath(\implode('->', $path))
37
        );
38
    }
39
}
40