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

PostgresGrammar   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileJsonArray() 0 3 1
A compileJsonObject() 0 7 1
A compileJsonValueSelect() 0 3 1
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