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

WhereJsonTrait::buildJsonInjection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 4
dl 0
loc 21
rs 9.8333
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\MySQL\Query\Traits;
13
14
use Cycle\Database\Driver\Jsoner;
15
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
16
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContains;
17
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContainsKey;
18
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContain;
19
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContainKey;
20
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength;
21
use Cycle\Database\Exception\BuilderException;
22
23
/**
24
 * @internal
25
 *
26
 * @psalm-internal Cycle\Database\Driver\MySQL
27
 */
28
trait WhereJsonTrait
29
{
30
    /**
31
     * @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...
32
     * @param non-empty-string $method
33
     * @param array<non-empty-string, mixed> $params
34
     */
35
    protected function buildJsonInjection(
36
        string $column,
37
        mixed $value,
38
        string $method,
39
        array $params,
40
    ): array {
41
        return match ($method) {
42
            'whereJson', 'orWhereJson' => [new CompileJson($column), $value],
43
            'whereJsonContains', 'orWhereJsonContains' => [new CompileJsonContains(
44
                $column,
45
                Jsoner::toJson($value, $params['encode'], $params['validate'])
46
            )],
47
            'whereJsonDoesntContain', 'orWhereJsonDoesntContain' => [new CompileJsonDoesntContain(
48
                $column,
49
                Jsoner::toJson($value, $params['encode'], $params['validate'])
50
            )],
51
            'whereJsonContainsKey', 'orWhereJsonContainsKey' => [new CompileJsonContainsKey($column)],
52
            'whereJsonDoesntContainKey', 'orWhereJsonDoesntContainKey' => [new CompileJsonDoesntContainKey($column)],
53
            'whereJsonLength', 'orWhereJsonLength' => [new CompileJsonLength($column, $value, $params['operator'])],
54
            default => null,
55
        } ?? throw new BuilderException("This database engine can't handle the `$method` method.");
56
    }
57
}
58