ColumnSchema.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\pgsql;
  8. use yii\db\ArrayExpression;
  9. use yii\db\ExpressionInterface;
  10. use yii\db\JsonExpression;
  11. /**
  12. * Class ColumnSchema for PostgreSQL database.
  13. *
  14. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  15. */
  16. class ColumnSchema extends \yii\db\ColumnSchema
  17. {
  18. /**
  19. * @var int the dimension of array. Defaults to 0, means this column is not an array.
  20. */
  21. public $dimension = 0;
  22. /**
  23. * @var bool whether the column schema should OMIT using JSON support feature.
  24. * You can use this property to make upgrade to Yii 2.0.14 easier.
  25. * Default to `false`, meaning JSON support is enabled.
  26. *
  27. * @since 2.0.14.1
  28. * @deprecated Since 2.0.14.1 and will be removed in 2.1.
  29. */
  30. public $disableJsonSupport = false;
  31. /**
  32. * @var bool whether the column schema should OMIT using PgSQL Arrays support feature.
  33. * You can use this property to make upgrade to Yii 2.0.14 easier.
  34. * Default to `false`, meaning Arrays support is enabled.
  35. *
  36. * @since 2.0.14.1
  37. * @deprecated Since 2.0.14.1 and will be removed in 2.1.
  38. */
  39. public $disableArraySupport = false;
  40. /**
  41. * @var bool whether the Array column value should be unserialized to an [[ArrayExpression]] object.
  42. * You can use this property to make upgrade to Yii 2.0.14 easier.
  43. * Default to `true`, meaning arrays are unserialized to [[ArrayExpression]] objects.
  44. *
  45. * @since 2.0.14.1
  46. * @deprecated Since 2.0.14.1 and will be removed in 2.1.
  47. */
  48. public $deserializeArrayColumnToArrayExpression = true;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function dbTypecast($value)
  53. {
  54. if ($value === null) {
  55. return $value;
  56. }
  57. if ($value instanceof ExpressionInterface) {
  58. return $value;
  59. }
  60. if ($this->dimension > 0) {
  61. return $this->disableArraySupport
  62. ? (string) $value
  63. : new ArrayExpression($value, $this->dbType, $this->dimension);
  64. }
  65. if (!$this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
  66. return new JsonExpression($value, $this->dbType);
  67. }
  68. return $this->typecast($value);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function phpTypecast($value)
  74. {
  75. if ($this->dimension > 0) {
  76. if ($this->disableArraySupport) {
  77. return $value;
  78. }
  79. if (!is_array($value)) {
  80. $value = $this->getArrayParser()->parse($value);
  81. }
  82. if (is_array($value)) {
  83. array_walk_recursive($value, function (&$val, $key) {
  84. $val = $this->phpTypecastValue($val);
  85. });
  86. } elseif ($value === null) {
  87. return null;
  88. }
  89. return $this->deserializeArrayColumnToArrayExpression
  90. ? new ArrayExpression($value, $this->dbType, $this->dimension)
  91. : $value;
  92. }
  93. return $this->phpTypecastValue($value);
  94. }
  95. /**
  96. * Casts $value after retrieving from the DBMS to PHP representation.
  97. *
  98. * @param string|null $value
  99. * @return bool|mixed|null
  100. */
  101. protected function phpTypecastValue($value)
  102. {
  103. if ($value === null) {
  104. return null;
  105. }
  106. switch ($this->type) {
  107. case Schema::TYPE_BOOLEAN:
  108. switch (strtolower($value)) {
  109. case 't':
  110. case 'true':
  111. return true;
  112. case 'f':
  113. case 'false':
  114. return false;
  115. }
  116. return (bool) $value;
  117. case Schema::TYPE_JSON:
  118. return $this->disableJsonSupport ? $value : json_decode($value, true);
  119. }
  120. return parent::phpTypecast($value);
  121. }
  122. /**
  123. * Creates instance of ArrayParser
  124. *
  125. * @return ArrayParser
  126. */
  127. protected function getArrayParser()
  128. {
  129. static $parser = null;
  130. if ($parser === null) {
  131. $parser = new ArrayParser();
  132. }
  133. return $parser;
  134. }
  135. }