Issues (97)

src/Grammars/Traits/CompilesMySqlJsonQueries.php (3 issues)

1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Grammars\Traits;
4
5
use Illuminate\Contracts\Database\Query\Expression;
6
use Illuminate\Database\ConnectionInterface;
7
8
trait CompilesMySqlJsonQueries
9
{
10
    /**
11
     * Compile a "JSON array" statement into SQL.
12
     *
13
     * @param string $column
14
     * @return string
15
     */
16
    public function compileJsonArray($column)
17
    {
18
        return 'json_array('.$this->wrap($column).')';
0 ignored issues
show
It seems like wrap() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

18
        return 'json_array('.$this->/** @scrutinizer ignore-call */ wrap($column).')';
Loading history...
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
    public function compileJsonObject($column, $levels)
29
    {
30
        return str_repeat('json_object(?, ', $levels)
31
            .$this->wrap($column)
32
            .str_repeat(')', $levels);
33
    }
34
35
    /**
36
     * Compile a "JSON value select" statement into SQL.
37
     *
38
     * @param string $column
39
     * @return string
40
     */
41
    public function compileJsonValueSelect(string $column): string
42
    {
43
        return $this->wrap($column);
44
    }
45
46
    /**
47
     * Determine whether the database supports the "member of" operator.
48
     *
49
     * @param \Illuminate\Database\ConnectionInterface $connection
50
     * @return bool
51
     */
52
    public function supportsMemberOf(ConnectionInterface $connection): bool
53
    {
54
        if ($connection->isMaria()) {
55
            return false;
56
        }
57
58
        return version_compare($connection->getServerVersion(), '8.0.17') >= 0;
59
    }
60
61
    /**
62
     * Compile a "member of" statement into SQL.
63
     *
64
     * @param string $column
65
     * @param mixed $value
66
     * @return string
67
     */
68
    public function compileMemberOf(string $column, ?string $objectKey, mixed $value): string
69
    {
70
        $columnWithKey = $objectKey ? $column . (str_contains($column, '->') ? '[*]' : '') . "->$objectKey" : $column;
71
72
        [$field, $path] = $this->wrapJsonFieldAndPath($columnWithKey);
0 ignored issues
show
It seems like wrapJsonFieldAndPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-call */ 
73
        [$field, $path] = $this->wrapJsonFieldAndPath($columnWithKey);
Loading history...
73
74
        if ($objectKey && !str_contains($column, '->')) {
75
            $path = ", '$[*]" . substr($path, 4);
76
        }
77
78
        $sql = $path ? "json_extract($field$path)" : $field;
79
80
        if ($value instanceof Expression) {
81
            return $value->getValue($this) . " member of($sql)";
0 ignored issues
show
$this of type Staudenmeir\EloquentJson...ompilesMySqlJsonQueries is incompatible with the type Illuminate\Database\Grammar expected by parameter $grammar of Illuminate\Database\Query\Expression::getValue(). ( Ignorable by Annotation )

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

81
            return $value->getValue(/** @scrutinizer ignore-type */ $this) . " member of($sql)";
Loading history...
82
        }
83
84
        return "? member of($sql)";
85
    }
86
87
    /**
88
     * Prepare the bindings for a "member of" statement.
89
     *
90
     * @param mixed $value
91
     * @return array
92
     */
93
    public function prepareBindingsForMemberOf(mixed $value): array
94
    {
95
        return $value instanceof Expression ? [] : [$value];
96
    }
97
}
98