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