ArrayExpression.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 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. */
  25. class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable, \IteratorAggregate
  26. {
  27. /**
  28. * @var null|string the type of the array elements. Defaults to `null` which means the type is
  29. * not explicitly specified.
  30. *
  31. * Note that in case when type is not specified explicitly and DBMS can not guess it from the context,
  32. * SQL error will be raised.
  33. */
  34. private $type;
  35. /**
  36. * @var array|QueryInterface the array's content.
  37. * In can be represented as an array of values or a [[Query]] that returns these values.
  38. */
  39. private $value;
  40. /**
  41. * @var int the number of indices needed to select an element
  42. */
  43. private $dimension;
  44. /**
  45. * ArrayExpression constructor.
  46. *
  47. * @param array|QueryInterface|mixed $value the array content. Either represented as an array of values or a Query that
  48. * returns these values. A single value will be considered as an array containing one element.
  49. * @param string|null $type the type of the array elements. Defaults to `null` which means the type is
  50. * not explicitly specified. In case when type is not specified explicitly and DBMS can not guess it from the context,
  51. * SQL error will be raised.
  52. * @param int $dimension the number of indices needed to select an element
  53. */
  54. public function __construct($value, $type = null, $dimension = 1)
  55. {
  56. if ($value instanceof self) {
  57. $value = $value->getValue();
  58. }
  59. $this->value = $value;
  60. $this->type = $type;
  61. $this->dimension = $dimension;
  62. }
  63. /**
  64. * @return null|string
  65. * @see type
  66. */
  67. public function getType()
  68. {
  69. return $this->type;
  70. }
  71. /**
  72. * @return array|mixed|QueryInterface
  73. * @see value
  74. */
  75. public function getValue()
  76. {
  77. return $this->value;
  78. }
  79. /**
  80. * @return int the number of indices needed to select an element
  81. * @see dimensions
  82. */
  83. public function getDimension()
  84. {
  85. return $this->dimension;
  86. }
  87. /**
  88. * Whether a offset exists
  89. *
  90. * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
  91. * @param mixed $offset <p>
  92. * An offset to check for.
  93. * </p>
  94. * @return bool true on success or false on failure.
  95. * </p>
  96. * <p>
  97. * The return value will be casted to boolean if non-boolean was returned.
  98. * @since 2.0.14
  99. */
  100. public function offsetExists($offset)
  101. {
  102. return isset($this->value[$offset]);
  103. }
  104. /**
  105. * Offset to retrieve
  106. *
  107. * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
  108. * @param mixed $offset <p>
  109. * The offset to retrieve.
  110. * </p>
  111. * @return mixed Can return all value types.
  112. * @since 2.0.14
  113. */
  114. public function offsetGet($offset)
  115. {
  116. return $this->value[$offset];
  117. }
  118. /**
  119. * Offset to set
  120. *
  121. * @link https://secure.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. public function offsetSet($offset, $value)
  132. {
  133. $this->value[$offset] = $value;
  134. }
  135. /**
  136. * Offset to unset
  137. *
  138. * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
  139. * @param mixed $offset <p>
  140. * The offset to unset.
  141. * </p>
  142. * @return void
  143. * @since 2.0.14
  144. */
  145. public function offsetUnset($offset)
  146. {
  147. unset($this->value[$offset]);
  148. }
  149. /**
  150. * Count elements of an object
  151. *
  152. * @link https://secure.php.net/manual/en/countable.count.php
  153. * @return int The custom count as an integer.
  154. * </p>
  155. * <p>
  156. * The return value is cast to an integer.
  157. * @since 2.0.14
  158. */
  159. public function count()
  160. {
  161. return count($this->value);
  162. }
  163. /**
  164. * Retrieve an external iterator
  165. *
  166. * @link https://secure.php.net/manual/en/iteratoraggregate.getiterator.php
  167. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  168. * <b>Traversable</b>
  169. * @since 2.0.14.1
  170. * @throws InvalidConfigException when ArrayExpression contains QueryInterface object
  171. */
  172. public function getIterator()
  173. {
  174. $value = $this->getValue();
  175. if ($value instanceof QueryInterface) {
  176. throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
  177. }
  178. if ($value === null) {
  179. $value = [];
  180. }
  181. return new \ArrayIterator($value);
  182. }
  183. }