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