Completed
Push — master ( ff5e40...f3ad7a )
by Dmitry
11:43
created

ArrayExpression::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
crap 2
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 8
    public function __construct($value, $type = null, $dimension = 1)
60
    {
61 8
        if ($value instanceof self) {
62 1
            $value = $value->getValue();
63
        }
64
65 8
        $this->value = $value;
66 8
        $this->type = $type;
67 8
        $this->dimension = $dimension;
68 8
    }
69
70
    /**
71
     * @return null|string
72
     * @see type
73
     */
74 31
    public function getType()
75
    {
76 31
        return $this->type;
77
    }
78
79
    /**
80
     * @return array|mixed|QueryInterface
81
     * @see value
82
     */
83 31
    public function getValue()
84
    {
85 31
        return $this->value;
86
    }
87
88
    /**
89
     * @return int the number of indices needed to select an element
90
     * @see dimensions
91
     */
92 28
    public function getDimension()
93
    {
94 28
        return $this->dimension;
95
    }
96
97
    /**
98
     * Whether a offset exists
99
     *
100
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
101
     * @param mixed $offset <p>
102
     * An offset to check for.
103
     * </p>
104
     * @return bool true on success or false on failure.
105
     * </p>
106
     * <p>
107
     * The return value will be casted to boolean if non-boolean was returned.
108
     * @since 2.0.14
109
     */
110
    public function offsetExists($offset)
111
    {
112
        return isset($this->value[$offset]);
113
    }
114
115
    /**
116
     * Offset to retrieve
117
     *
118
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
119
     * @param mixed $offset <p>
120
     * The offset to retrieve.
121
     * </p>
122
     * @return mixed Can return all value types.
123
     * @since 2.0.14
124
     */
125 4
    public function offsetGet($offset)
126
    {
127 4
        return $this->value[$offset];
128
    }
129
130
    /**
131
     * Offset to set
132
     *
133
     * @link http://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
    public function offsetSet($offset, $value)
144
    {
145
        $this->value[$offset] = $value;
146
    }
147
148
    /**
149
     * Offset to unset
150
     *
151
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
152
     * @param mixed $offset <p>
153
     * The offset to unset.
154
     * </p>
155
     * @return void
156
     * @since 2.0.14
157
     */
158
    public function offsetUnset($offset)
159
    {
160
        unset($this->value[$offset]);
161
    }
162
163
    /**
164
     * Count elements of an object
165
     *
166
     * @link http://php.net/manual/en/countable.count.php
167
     * @return int The custom count as an integer.
168
     * </p>
169
     * <p>
170
     * The return value is cast to an integer.
171
     * @since 2.0.14
172
     */
173
    public function count()
174
    {
175
        return count($this->value);
176
    }
177
178
    /**
179
     * Retrieve an external iterator
180
     *
181
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
182
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
183
     * <b>Traversable</b>
184
     * @since 2.0.14.1
185
     * @throws InvalidConfigException when ArrayExpression contains QueryInterface object
186
     */
187 5
    public function getIterator()
188
    {
189 5
        $value = $this->getValue();
190 5
        if ($value instanceof QueryInterface) {
191
            throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
192
        }
193 5
        if ($value === null) {
194
            $value = [];
195
        }
196
197 5
        return new \ArrayIterator($value);
198
    }
199
}
200