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

WhereJsonTrait::buildJsonInjection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 4
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
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\SQLite\Query\Traits;
13
14
use Cycle\Database\Driver\SQLite\Injection\CompileJson;
15
use Cycle\Database\Driver\SQLite\Injection\CompileJsonContainsKey;
16
use Cycle\Database\Driver\SQLite\Injection\CompileJsonDoesntContainKey;
17
use Cycle\Database\Driver\SQLite\Injection\CompileJsonLength;
18
use Cycle\Database\Exception\BuilderException;
19
use Cycle\Database\Exception\DriverException;
20
21
/**
22
 * @internal
23
 *
24
 * @psalm-internal Cycle\Database\Driver\SQLite
25
 */
26
trait WhereJsonTrait
27
{
28
    /**
29
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
30
     * @param non-empty-string $method
31
     * @param array<non-empty-string, mixed> $params
32
     */
33
    protected function buildJsonInjection(
34
        string $column,
35
        mixed $value,
36
        string $method,
37
        array $params,
38
    ): array {
39
        return match ($method) {
40
            'whereJson', 'orWhereJson' => [new CompileJson($column), $value],
41
            'whereJsonContains', 'orWhereJsonContains', 'whereJsonDoesntContain',
42
            'orWhereJsonDoesntContain' => throw new DriverException(
43
                'This SQLite does not support JSON contains operations.'
44
            ),
45
            'whereJsonContainsKey', 'orWhereJsonContainsKey' => [new CompileJsonContainsKey($column)],
46
            'whereJsonDoesntContainKey', 'orWhereJsonDoesntContainKey' => [new CompileJsonDoesntContainKey($column)],
47
            'whereJsonLength', 'orWhereJsonLength' => [new CompileJsonLength($column, $value, $params['operator'])],
48
            default => null,
49
        } ?? throw new BuilderException("This database engine can't handle the `$method` method.");
50
    }
51
}
52