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

CompileJsonContainsKey   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A compile() 0 21 2
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