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