ArrayExpression.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use Traversable;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * Class ArrayExpression represents an array SQL expression.
  12. *
  13. * Expressions of this type can be used in conditions as well:
  14. *
  15. * ```php
  16. * $query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')])
  17. * ```
  18. *
  19. * which, depending on DBMS, will result in a well-prepared condition. For example, in
  20. * PostgreSQL it will be compiled to `WHERE "items" @> ARRAY[1, 2, 3]::integer[]`.
  21. *
  22. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  23. * @since 2.0.14
  24. * @phpcs:disable Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore
  25. */
  26. class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable, \IteratorAggregate
  27. {
  28. /**
  29. * @var string|null the type of the array elements. Defaults to `null` which means the type is
  30. * not explicitly specified.
  31. *
  32. * Note that in case when type is not specified explicitly and DBMS can not guess it from the context,
  33. * SQL error will be raised.
  34. */
  35. private $type;
  36. /**
  37. * @var array|QueryInterface the array's content.
  38. * In can be represented as an array of values or a [[Query]] that returns these values.
  39. */
  40. private $value;
  41. /**
  42. * @var int the number of indices needed to select an element
  43. */
  44. private $dimension;
  45. /**
  46. * ArrayExpression constructor.
  47. *
  48. * @param array|QueryInterface|mixed $value the array content. Either represented as an array of values or a Query that
  49. * returns these values. A single value will be considered as an array containing one element.
  50. * @param string|null $type the type of the array elements. Defaults to `null` which means the type is
  51. * not explicitly specified. In case when type is not specified explicitly and DBMS can not guess it from the context,
  52. * SQL error will be raised.
  53. * @param int $dimension the number of indices needed to select an element
  54. */
  55. public function __construct($value, $type = null, $dimension = 1)
  56. {
  57. if ($value instanceof self) {
  58. $value = $value->getValue();
  59. }
  60. $this->value = $value;
  61. $this->type = $type;
  62. $this->dimension = $dimension;
  63. }
  64. /**
  65. * @return string|null
  66. */
  67. public function getType()
  68. {
  69. return $this->type;
  70. }
  71. /**
  72. * @return array|mixed|QueryInterface
  73. */
  74. public function getValue()
  75. {
  76. return $this->value;
  77. }
  78. /**
  79. * @return int the number of indices needed to select an element
  80. */
  81. public function getDimension()
  82. {
  83. return $this->dimension;
  84. }
  85. /**
  86. * Whether a offset exists
  87. *
  88. * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
  89. * @param mixed $offset <p>
  90. * An offset to check for.
  91. * </p>
  92. * @return bool true on success or false on failure.
  93. * </p>
  94. * <p>
  95. * The return value will be casted to boolean if non-boolean was returned.
  96. * @since 2.0.14
  97. */
  98. #[\ReturnTypeWillChange]
  99. public function offsetExists($offset)
  100. {
  101. return isset($this->value[$offset]);
  102. }
  103. /**
  104. * Offset to retrieve
  105. *
  106. * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
  107. * @param mixed $offset <p>
  108. * The offset to retrieve.
  109. * </p>
  110. * @return mixed Can return all value types.
  111. * @since 2.0.14
  112. */
  113. #[\ReturnTypeWillChange]
  114. public function offsetGet($offset)
  115. {
  116. return $this->value[$offset];
  117. }
  118. /**
  119. * Offset to set
  120. *
  121. * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
  122. * @param mixed $offset <p>
  123. * The offset to assign the value to.
  124. * </p>
  125. * @param mixed $value <p>
  126. * The value to set.
  127. * </p>
  128. * @return void
  129. * @since 2.0.14
  130. */
  131. #[\ReturnTypeWillChange]
  132. public function offsetSet($offset, $value)
  133. {
  134. $this->value[$offset] = $value;
  135. }
  136. /**
  137. * Offset to unset
  138. *
  139. * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
  140. * @param mixed $offset <p>
  141. * The offset to unset.
  142. * </p>
  143. * @return void
  144. * @since 2.0.14
  145. */
  146. #[\ReturnTypeWillChange]
  147. public function offsetUnset($offset)
  148. {
  149. unset($this->value[$offset]);
  150. }
  151. /**
  152. * Count elements of an object
  153. *
  154. * @link https://www.php.net/manual/en/countable.count.php
  155. * @return int The custom count as an integer.
  156. * </p>
  157. * <p>
  158. * The return value is cast to an integer.
  159. * @since 2.0.14
  160. */
  161. #[\ReturnTypeWillChange]
  162. public function count()
  163. {
  164. return count($this->value);
  165. }
  166. /**
  167. * Retrieve an external iterator
  168. *
  169. * @link https://www.php.net/manual/en/iteratoraggregate.getiterator.php
  170. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  171. * <b>Traversable</b>
  172. * @since 2.0.14.1
  173. * @throws InvalidConfigException when ArrayExpression contains QueryInterface object
  174. */
  175. #[\ReturnTypeWillChange]
  176. public function getIterator()
  177. {
  178. $value = $this->getValue();
  179. if ($value instanceof QueryInterface) {
  180. throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
  181. }
  182. if ($value === null) {
  183. $value = [];
  184. }
  185. return new \ArrayIterator($value);
  186. }
  187. }