Passed
Branch dev (d14b82)
by Wilmer
12:57
created

JsonExpressionBuilder::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 20
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Builder;
6
7
use JsonException;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
use Yiisoft\Db\Expression\ArrayExpression;
13
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
14
use Yiisoft\Db\Expression\ExpressionInterface;
15
use Yiisoft\Db\Expression\JsonExpression;
16
use Yiisoft\Db\Query\Query;
17
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Yiisoft\Db\Query\QueryInterface;
19
use Yiisoft\Json\Json;
20
21
/**
22
 * The class JsonExpressionBuilder builds {@see JsonExpression} for PostgreSQL DBMS.
23
 */
24
final class JsonExpressionBuilder implements ExpressionBuilderInterface
25
{
26 16
    public function __construct(private QueryBuilderInterface $queryBuilder)
27
    {
28
    }
29
30
    /**
31
     * Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.
32
     *
33
     * @param ExpressionInterface $expression the expression to be built.
34
     * @param array $params the binding parameters.
35
     *
36
     * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException
37
     *
38
     * @return string the raw SQL that will not be additionally escaped or quoted.
39
     */
40 16
    public function build(ExpressionInterface $expression, array &$params = []): string
41
    {
42
        /**
43
         * @var JsonExpression $expression
44
         * @var array|mixed|QueryInterface $value
45
         */
46 16
        $value = $expression->getValue();
47
48 16
        if ($value instanceof Query) {
49 2
            [$sql, $params] = $this->queryBuilder->build($value, $params);
50 2
            return "($sql)" . $this->getTypecast($expression);
51
        }
52
53 14
        if ($value instanceof ArrayExpression) {
54 1
            $placeholder = 'array_to_json(' . $this->queryBuilder->buildExpression($value, $params) . ')';
55
        } else {
56 14
            $placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
57
        }
58
59 14
        return $placeholder . $this->getTypecast($expression);
60
    }
61
62
    /**
63
     * @param JsonExpression $expression
64
     *
65
     * @return string the typecast expression based on {@see type}.
66
     */
67 16
    protected function getTypecast(JsonExpression $expression): string
68
    {
69 16
        if ($expression->getType() === null) {
70 13
            return '';
71
        }
72
73 4
        return '::' . (string) $expression->getType();
74
    }
75
}
76