1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Marshaler; |
7
|
|
|
use Respect\Data\CollectionIterator; |
8
|
|
|
use Respect\Data\Collections\Collection; |
9
|
|
|
use Respect\Structural\Driver as BaseDriver; |
10
|
|
|
|
11
|
|
|
class Driver implements BaseDriver |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Aws\DynamoDb\DynamoDbClient |
15
|
|
|
*/ |
16
|
|
|
private $connection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Aws\DynamoDb\Marshaler |
20
|
|
|
*/ |
21
|
|
|
private $marshaler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Driver constructor. |
25
|
|
|
* |
26
|
|
|
* @param \Aws\DynamoDb\DynamoDbClient $connection |
27
|
|
|
* @param string $database |
|
|
|
|
28
|
|
|
*/ |
29
|
8 |
|
public function __construct(DynamoDbClient $connection) |
30
|
|
|
{ |
31
|
8 |
|
$this->connection = $connection; |
32
|
8 |
|
$this->marshaler = new Marshaler(); |
33
|
8 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return \Aws\DynamoDb\DynamoDbClient |
37
|
|
|
*/ |
38
|
3 |
|
public function getConnection() |
39
|
|
|
{ |
40
|
3 |
|
return $this->connection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \Iterator $cursor |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
1 |
|
public function fetch(\Iterator $cursor) |
49
|
|
|
{ |
50
|
1 |
|
$data = []; |
51
|
|
|
|
52
|
1 |
|
if ($cursor->valid()) { |
53
|
1 |
|
$data = $cursor->current(); |
54
|
1 |
|
$cursor->next(); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
return $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $collection |
62
|
|
|
* @param array $query |
63
|
|
|
* |
64
|
|
|
* @return \Iterator |
65
|
|
|
*/ |
66
|
2 |
|
public function find($collection, array $query = []) |
67
|
|
|
{ |
68
|
|
|
$expression = [ |
69
|
2 |
|
'TableName' => $collection, |
70
|
2 |
|
]; |
71
|
|
|
|
72
|
2 |
|
if (!empty($query)) { |
73
|
2 |
|
$expression['ScanFilter'] = $query; |
74
|
2 |
|
} |
75
|
|
|
|
76
|
2 |
|
$result = $this->getConnection()->scan($expression); |
77
|
|
|
|
78
|
2 |
|
return $this->formatResults($result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Collection $collection |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
3 |
|
public function generateQuery(Collection $collection) |
87
|
|
|
{ |
88
|
3 |
|
return $this->parseConditions($collection); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Collection $collection |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
3 |
|
protected function parseConditions(Collection $collection) |
97
|
|
|
{ |
98
|
3 |
|
$collections = iterator_to_array( |
99
|
3 |
|
CollectionIterator::recursive($collection) |
100
|
3 |
|
); |
101
|
|
|
|
102
|
3 |
|
$collections = array_slice($collections, 1); |
103
|
3 |
|
$condition = $this->getConditionArray($collection); |
104
|
|
|
|
105
|
3 |
|
foreach ($collections as $name => $coll) { |
106
|
1 |
|
$condition += $this->getConditionArray($coll); |
107
|
3 |
|
} |
108
|
|
|
|
109
|
3 |
|
return $condition; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Collection $collection |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
3 |
|
protected function getConditionArray(Collection $collection) |
118
|
|
|
{ |
119
|
3 |
|
$condition = $collection->getCondition(); |
120
|
|
|
|
121
|
3 |
|
if (!is_array($condition)) { |
122
|
2 |
|
$condition = ['_id' => $condition]; |
123
|
2 |
|
} |
124
|
|
|
|
125
|
3 |
|
$conditions = []; |
126
|
|
|
|
127
|
3 |
|
foreach ($condition as $key => $value) { |
128
|
|
|
$conditions = [ |
129
|
|
|
$key => [ |
130
|
|
|
'AttributeValueList' => [ |
131
|
2 |
|
$this->marshaler->marshalValue($value), |
132
|
2 |
|
], |
133
|
2 |
|
'ComparisonOperator' => 'EQ', |
134
|
2 |
|
], |
135
|
2 |
|
]; |
136
|
3 |
|
} |
137
|
|
|
|
138
|
3 |
|
return $conditions; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Collection $collection |
143
|
|
|
* @param $document |
144
|
|
|
*/ |
145
|
|
|
public function insert($collection, $document) |
146
|
|
|
{ |
147
|
|
|
$args = [ |
148
|
|
|
'TableName' => $collection, |
149
|
|
|
'Item' => $this->marshaler->marshalItem($document), |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$this->getConnection()->putItem($args); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Collection $collection |
157
|
|
|
* @param $criteria |
158
|
|
|
* @param $document |
159
|
|
|
*/ |
160
|
|
|
public function update($collection, $criteria, $document) |
161
|
|
|
{ |
162
|
|
|
$args = [ |
163
|
|
|
'TableName' => $collection, |
164
|
|
|
'Key' => $this->marshaler->marshalItem($criteria), |
165
|
|
|
'AttributeUpdates' => $this->formatAttributes($document), |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
$this->getConnection()->updateItem($args); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param Collection $collection |
173
|
|
|
* @param $criteria |
174
|
|
|
*/ |
175
|
|
|
public function remove($collection, $criteria) |
176
|
|
|
{ |
177
|
|
|
$args = [ |
178
|
|
|
'TableName' => $collection, |
179
|
|
|
'Key' => $this->marshaler->marshalItem($criteria), |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
$this->getConnection()->deleteItem($args); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $values |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
protected function formatAttributes($values) |
191
|
|
|
{ |
192
|
|
|
$attributes = []; |
193
|
|
|
|
194
|
|
|
foreach ($values as $key => $value) { |
195
|
|
|
$attributes[$key] = [ |
196
|
|
|
'Value' => $this->marshaler->marshalValue($value), |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $attributes; |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
protected function formatResults(\Aws\Result $result) |
204
|
|
|
{ |
205
|
2 |
|
$items = new \ArrayIterator(); |
206
|
|
|
|
207
|
2 |
|
if ($result['Count'] === 0) { |
208
|
1 |
|
return $items; |
209
|
|
|
} |
210
|
|
|
|
211
|
1 |
|
foreach ($result['Items'] as $item) { |
212
|
1 |
|
$items[] = $this->marshaler->unmarshalItem($item); |
213
|
1 |
|
} |
214
|
|
|
|
215
|
1 |
|
return $items; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.