Passed
Pull Request — 2.2 (#20357)
by Wilmer
13:33 queued 05:55
created

JsonExpression::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\db;
10
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Class JsonExpression represents data that should be encoded to JSON.
15
 *
16
 * For example:
17
 *
18
 * ```php
19
 * new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'
20
 * ```
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 * @since 2.0.14
24
 */
25
class JsonExpression implements ExpressionInterface, \JsonSerializable
26
{
27
    const TYPE_JSON = 'json';
28
    const TYPE_JSONB = 'jsonb';
29
30
    /**
31
     * @var mixed the value to be encoded to JSON.
32
     * The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] input requirements.
33
     */
34
    protected $value;
35
    /**
36
     * @var string|null Type of JSON, expression should be casted to. Defaults to `null`, meaning
37
     * no explicit casting will be performed.
38
     * This property will be encountered only for DBMSs that support different types of JSON.
39
     * For example, PostgreSQL has `json` and `jsonb` types.
40
     */
41
    protected $type;
42
43
44
    /**
45
     * JsonExpression constructor.
46
     *
47
     * @param mixed $value the value to be encoded to JSON.
48
     * The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] requirements.
49
     * @param string|null $type the type of the JSON. See [[JsonExpression::type]]
50
     *
51
     * @see type
52
     */
53
    public function __construct($value, $type = null)
54
    {
55
        if ($value instanceof self) {
56
            $value = $value->getValue();
57
        }
58
59
        $this->value = $value;
60
        $this->type = $type;
61
    }
62
63
    /**
64
     * @return mixed
65
     * @see value
66
     */
67
    public function getValue()
68
    {
69
        return $this->value;
70
    }
71
72
    /**
73
     * @return string|null the type of JSON
74
     * @see type
75
     */
76
    public function getType()
77
    {
78
        return $this->type;
79
    }
80
81
    /**
82
     * Specify data which should be serialized to JSON
83
     *
84
     * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
85
     * @return mixed data which can be serialized by <b>json_encode</b>,
86
     * which is a value of any type other than a resource.
87
     * @since 2.0.14.2
88
     * @throws InvalidConfigException when JsonExpression contains QueryInterface object
89
     */
90
    #[\ReturnTypeWillChange]
91
    public function jsonSerialize()
92
    {
93
        $value = $this->getValue();
94
        if ($value instanceof QueryInterface) {
95
            throw new InvalidConfigException('The JsonExpression class can not be serialized to JSON when the value is a QueryInterface object');
96
        }
97
98
        return $value;
99
    }
100
}
101