Passed
Pull Request — master (#32)
by Wilmer
14:54
created

JsonExpressionBuilder::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
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\ExpressionBuilderTrait;
15
use Yiisoft\Db\Expression\ExpressionInterface;
16
use Yiisoft\Db\Expression\JsonExpression;
17
use Yiisoft\Db\Query\Query;
18
use Yiisoft\Json\Json;
19
20
final class JsonExpressionBuilder implements ExpressionBuilderInterface
21
{
22
    use ExpressionBuilderTrait;
23
24
    /**
25
     * Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.
26
     *
27
     * @param ExpressionInterface $expression the expression to be built.
28
     * @param array $params the binding parameters.
29
     *
30
     * @throws Exception
31
     * @throws JsonException
32
     * @throws InvalidArgumentException
33
     * @throws InvalidConfigException
34
     * @throws NotSupportedException
35
     *
36
     * @return string the raw SQL that will not be additionally escaped or quoted.
37
     */
38
    public function build(ExpressionInterface $expression, array &$params = []): string
39
    {
40
        $value = $expression->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Pdo\PdoValue or Yiisoft\Db\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Query\Conditions\SimpleCondition or Yiisoft\Db\Query\Conditi...BetweenColumnsCondition. ( Ignorable by Annotation )

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

40
        /** @scrutinizer ignore-call */ 
41
        $value = $expression->getValue();
Loading history...
41
42
        if ($value instanceof Query) {
43
            [$sql, $params] = $this->queryBuilder->build($value, $params);
44
            return "($sql)" . $this->getTypecast($expression);
45
        }
46
47
        if ($value instanceof ArrayExpression) {
48
            $placeholder = 'array_to_json(' . $this->queryBuilder->buildExpression($value, $params) . ')';
49
        } else {
50
            $placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
51
        }
52
53
        return $placeholder . $this->getTypecast($expression);
54
    }
55
56
    protected function getTypecast(JsonExpression $expression): string
57
    {
58
        if ($expression->getType() === null) {
59
            return '';
60
        }
61
62
        return '::' . $expression->getType();
63
    }
64
}
65