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