Passed
Push — master ( be06f7...c027e3 )
by Wilmer
08:58 queued 06:59
created

ArrayExpression::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Expression;
6
7
use Traversable;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Query\QueryInterface;
10
11
/**
12
 * Class ArrayExpression represents an array SQL expression.
13
 *
14
 * Expressions of this type can be used in conditions as well:
15
 *
16
 * ```php
17
 * $query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')])
18
 * ```
19
 *
20
 * which, depending on DBMS, will result in a well-prepared condition. For example, in PostgreSQL it will be compiled to
21
 * `WHERE "items" @> ARRAY[1, 2, 3]::integer[]`.
22
 */
23
class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable, \IteratorAggregate
24
{
25
    private ?string $type = null;
26
    private $value;
27
    private int $dimension;
28
29 8
    public function __construct($value = [], $type = null, $dimension = 1)
30
    {
31 8
        if ($value instanceof self) {
32 1
            $value = $value->getValue();
33
        }
34
35 8
        $this->value = $value;
36 8
        $this->type = $type;
37 8
        $this->dimension = $dimension;
38 8
    }
39
40
    /**
41
     * The type of the array elements. Defaults to `null` which means the type is not explicitly specified.
42
     *
43
     * Note that in case when type is not specified explicitly and DBMS can not guess it from the context, SQL error
44
     * will be raised.
45
     *
46
     * @return string|null
47
     */
48 31
    public function getType(): ?string
49
    {
50 31
        return $this->type;
51
    }
52
53
    /**
54
     * The array's content. In can be represented as an array of values or a {@see Query} that returns these values.
55
     *
56
     * @return array|QueryInterface
57
     */
58 31
    public function getValue()
59
    {
60 31
        return $this->value;
61
    }
62
63
    /**
64
     * @return int the number of indices needed to select an element
65
     */
66 28
    public function getDimension(): int
67
    {
68 28
        return $this->dimension;
69
    }
70
71
    /**
72
     * Whether a offset exists.
73
     *
74
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
75
     *
76
     * @param mixed $offset An offset to check for.
77
     *
78
     * @return bool true on success or false on failure.
79
     *
80
     * The return value will be casted to boolean if non-boolean was returned.
81
     */
82
    public function offsetExists($offset): bool
83
    {
84
        return isset($this->value[$offset]);
85
    }
86
87
    /**
88
     * Offset to retrieve.
89
     *
90
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
91
     *
92
     * @param mixed $offset The offset to retrieve.
93
     *
94
     * @return mixed Can return all value types.
95
     */
96
    public function offsetGet($offset)
97
    {
98
        return $this->value[$offset];
99
    }
100
101
    /**
102
     * Offset to set.
103
     *
104
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
105
     *
106
     * @param mixed $offset The offset to assign the value to.
107
     * @param mixed $value  The value to set.
108
     *
109
     * @return void
110
     */
111
    public function offsetSet($offset, $value): void
112
    {
113
        $this->value[$offset] = $value;
114
    }
115
116
    /**
117
     * Offset to unset.
118
     *
119
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
120
     *
121
     * @param mixed $offset
122
     *
123
     * @return void
124
     */
125
    public function offsetUnset($offset): void
126
    {
127
        unset($this->value[$offset]);
128
    }
129
130
    /**
131
     * Count elements of an object.
132
     *
133
     * @link http://php.net/manual/en/countable.count.php
134
     *
135
     * @return int The custom count as an integer.
136
     *
137
     * The return value is cast to an integer.
138
     */
139
    public function count(): int
140
    {
141
        return \count($this->value);
142
    }
143
144
    /**
145
     * Retrieve an external iterator.
146
     *
147
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
148
     *
149
     * @throws InvalidConfigException when ArrayExpression contains QueryInterface object
150
     *
151
     * @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
152
     */
153
    public function getIterator(): Traversable
154
    {
155
        $value = $this->getValue();
156
        if ($value instanceof QueryInterface) {
157
            throw new InvalidConfigException(
158
                'The ArrayExpression class can not be iterated when the value is a QueryInterface object'
159
            );
160
        }
161
        if ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always false.
Loading history...
162
            $value = [];
163
        }
164
165
        return new \ArrayIterator($value);
166
    }
167
}
168