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

WhereJsonTrait   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 11
dl 0
loc 24
rs 10
c 5
b 2
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A buildJsonInjection() 0 17 1
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