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