JsonExpression.php 2.6 KB

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