Passed
Push — master ( af341e...2b2ec9 )
by Jonas
07:18 queued 05:16
created

PostgresGrammar::compileJsonValueSelect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Grammars;
4
5
use Illuminate\Database\Query\Expression;
6
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base;
7
8
class PostgresGrammar extends Base implements JsonGrammar
9
{
10
    /**
11
     * Compile a "JSON array" statement into SQL.
12
     *
13
     * @param string $column
14
     * @return string
15
     */
16 45
    public function compileJsonArray($column)
17
    {
18 45
        return 'jsonb_build_array('.$this->wrap($column).')';
19
    }
20
21
    /**
22
     * Compile a "JSON object" statement into SQL.
23
     *
24
     * @param string $column
25
     * @param int $levels
26
     * @return string
27
     */
28 22
    public function compileJsonObject($column, $levels)
29
    {
30 22
        $sql = str_repeat('jsonb_build_object(?::text, ', $levels)
31 22
                .$this->wrap($column)
32 22
                .str_repeat(')', $levels);
33
34 22
        return $this->compileJsonArray(new Expression($sql));
35
    }
36
37
    /**
38
     * Compile a "JSON value select" statement into SQL.
39
     *
40
     * @param string $column
41
     * @return string
42
     */
43 11
    public function compileJsonValueSelect(string $column): string
44
    {
45 11
        return $this->wrap($column);
46
    }
47
}
48