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