1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class RawDynamoDbQuery |
7
|
|
|
* |
8
|
|
|
* @package BaoPham\DynamoDb |
9
|
|
|
*/ |
10
|
|
|
class RawDynamoDbQuery implements \IteratorAggregate, \ArrayAccess, \Countable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* 'Scan', 'Query', etc. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $op; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The query body being sent to AWS |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
public $query; |
25
|
|
|
|
26
|
135 |
|
public function __construct($op, $query) |
27
|
|
|
{ |
28
|
135 |
|
$this->op = $op; |
29
|
135 |
|
$this->query = $query; |
30
|
135 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Perform any final clean up. |
34
|
|
|
* Remove any empty values to avoid errors. |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
135 |
|
public function finalize() |
39
|
|
|
{ |
40
|
|
|
$this->query = array_filter($this->query, function ($value) { |
41
|
135 |
|
return !empty($value) || is_bool($value) || is_numeric($value); |
42
|
135 |
|
}); |
43
|
|
|
|
44
|
135 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whether a offset exists |
49
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
50
|
|
|
* @param mixed $offset <p> |
51
|
|
|
* An offset to check for. |
52
|
|
|
* </p> |
53
|
|
|
* @return boolean true on success or false on failure. |
54
|
|
|
* </p> |
55
|
|
|
* <p> |
56
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
57
|
|
|
* @since 5.0.0 |
58
|
|
|
*/ |
59
|
|
|
#[\ReturnTypeWillChange] |
60
|
|
|
public function offsetExists($offset) |
61
|
|
|
{ |
62
|
|
|
return isset($this->internal()[$offset]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Offset to retrieve |
67
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
68
|
|
|
* @param mixed $offset <p> |
69
|
|
|
* The offset to retrieve. |
70
|
|
|
* </p> |
71
|
|
|
* @return mixed Can return all value types. |
72
|
|
|
* @since 5.0.0 |
73
|
|
|
*/ |
74
|
|
|
#[\ReturnTypeWillChange] |
75
|
|
|
public function offsetGet($offset) |
76
|
|
|
{ |
77
|
|
|
return $this->internal()[$offset]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Offset to set |
82
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
83
|
|
|
* @param mixed $offset <p> |
84
|
|
|
* The offset to assign the value to. |
85
|
|
|
* </p> |
86
|
|
|
* @param mixed $value <p> |
87
|
|
|
* The value to set. |
88
|
|
|
* </p> |
89
|
|
|
* @return void |
90
|
|
|
* @since 5.0.0 |
91
|
|
|
*/ |
92
|
|
|
#[\ReturnTypeWillChange] |
93
|
|
|
public function offsetSet($offset, $value) |
94
|
|
|
{ |
95
|
|
|
$this->internal()[$offset] = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Offset to unset |
100
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
101
|
|
|
* @param mixed $offset <p> |
102
|
|
|
* The offset to unset. |
103
|
|
|
* </p> |
104
|
|
|
* @return void |
105
|
|
|
* @since 5.0.0 |
106
|
|
|
*/ |
107
|
|
|
#[\ReturnTypeWillChange] |
108
|
|
|
public function offsetUnset($offset) |
109
|
|
|
{ |
110
|
|
|
unset($this->internal()[$offset]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Retrieve an external iterator |
115
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
116
|
|
|
* @return \Traversable An instance of an object implementing <b>Iterator</b> or |
117
|
|
|
* <b>Traversable</b> |
118
|
|
|
* @since 5.0.0 |
119
|
|
|
*/ |
120
|
|
|
#[\ReturnTypeWillChange] |
121
|
|
|
public function getIterator() |
122
|
|
|
{ |
123
|
|
|
return new \ArrayObject($this->internal()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Count elements of an object |
128
|
|
|
* @link http://php.net/manual/en/countable.count.php |
129
|
|
|
* @return int The custom count as an integer. |
130
|
|
|
* </p> |
131
|
|
|
* <p> |
132
|
|
|
* The return value is cast to an integer. |
133
|
|
|
* @since 5.1.0 |
134
|
|
|
*/ |
135
|
|
|
#[\ReturnTypeWillChange] |
136
|
|
|
public function count() |
137
|
|
|
{ |
138
|
|
|
return count($this->internal()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* For backward compatibility, previously we use array to represent the raw query |
143
|
|
|
* |
144
|
|
|
* @var array |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
private function internal() |
149
|
|
|
{ |
150
|
|
|
return [$this->op, $this->query]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|