PostgresGrammar::prepareBindingsForMemberOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Grammars;
4
5
use Illuminate\Database\ConnectionInterface;
6
use Illuminate\Database\Query\Expression;
7
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base;
8
use RuntimeException;
9
10
class PostgresGrammar extends Base implements JsonGrammar
11
{
12
    /**
13
     * Compile a "JSON array" statement into SQL.
14
     *
15
     * @param string $column
16
     * @return string
17
     */
18
    public function compileJsonArray($column)
19
    {
20
        return 'jsonb_build_array('.$this->wrap($column).')';
21
    }
22
23
    /**
24
     * Compile a "JSON object" statement into SQL.
25
     *
26
     * @param string $column
27
     * @param int $levels
28
     * @return string
29
     */
30
    public function compileJsonObject($column, $levels)
31
    {
32
        $sql = str_repeat('jsonb_build_object(?::text, ', $levels)
33
                .$this->wrap($column)
34
                .str_repeat(')', $levels);
35
36
        return $this->compileJsonArray(new Expression($sql));
0 ignored issues
show
Bug introduced by
new Illuminate\Database\Query\Expression($sql) of type Illuminate\Database\Query\Expression is incompatible with the type string expected by parameter $column of Staudenmeir\EloquentJson...mar::compileJsonArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        return $this->compileJsonArray(/** @scrutinizer ignore-type */ new Expression($sql));
Loading history...
37
    }
38
39
    /**
40
     * Compile a "JSON value select" statement into SQL.
41
     *
42
     * @param string $column
43
     * @return string
44
     */
45
    public function compileJsonValueSelect(string $column): string
46
    {
47
        return $this->wrap($column);
48
    }
49
50
    /**
51
     * Determine whether the database supports the "member of" operator.
52
     *
53
     * @param \Illuminate\Database\ConnectionInterface $connection
54
     * @return bool
55
     */
56
    public function supportsMemberOf(ConnectionInterface $connection): bool
57
    {
58
        return false;
59
    }
60
61
    /**
62
     * Compile a "member of" statement into SQL.
63
     *
64
     * @param string $column
65
     * @param string|null $objectKey
66
     * @param mixed $value
67
     * @return string
68
     */
69
    public function compileMemberOf(string $column, ?string $objectKey, mixed $value): string
70
    {
71
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
72
    }
73
74
    /**
75
     * Prepare the bindings for a "member of" statement.
76
     *
77
     * @param mixed $value
78
     * @return array
79
     */
80
    public function prepareBindingsForMemberOf(mixed $value): array
81
    {
82
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
83
    }
84
}
85