1
|
|
|
<?php |
2
|
|
|
namespace Fathomminds\Rest\Database\DynamoDb; |
3
|
|
|
|
4
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
5
|
|
|
use Aws\DynamoDb\Marshaler; |
6
|
|
|
|
7
|
|
|
class QueryConstructor |
8
|
|
|
{ |
9
|
14 |
|
public function marshalItem($resource) |
10
|
|
|
{ |
11
|
14 |
|
$marshaler = new Marshaler; |
12
|
14 |
|
$toMarshal = $resource; |
13
|
14 |
|
if (gettype($resource) === 'object') { |
14
|
11 |
|
$toMarshal = new \StdClass; |
15
|
11 |
|
foreach (get_object_vars($resource) as $name => $value) { |
16
|
11 |
|
$toMarshal->$name = $value; |
17
|
|
|
} |
18
|
|
|
} |
19
|
14 |
|
return $marshaler->marshalItem($toMarshal); |
20
|
|
|
} |
21
|
|
|
|
22
|
2 |
|
public function unmarshalItem($item) |
23
|
|
|
{ |
24
|
2 |
|
$marshaler = new Marshaler; |
25
|
2 |
|
if ($item === null) { |
26
|
|
|
return new \StdClass; |
27
|
|
|
} |
28
|
2 |
|
return $marshaler->unmarshalItem($item, true); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function unmarshalBatch($itemList) |
32
|
|
|
{ |
33
|
1 |
|
if ($itemList === null) { |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
1 |
|
$list = []; |
37
|
1 |
|
foreach ($itemList as $item) { |
38
|
1 |
|
$list[] = $this->unmarshalItem($item); |
39
|
|
|
} |
40
|
1 |
|
return $list; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function createGetQuery($tableName, $primaryKey, $resourceId) |
44
|
|
|
{ |
45
|
|
|
$query = [ |
46
|
1 |
|
'TableName' => $tableName, |
47
|
1 |
|
'Key' => $this->marshalItem([$primaryKey => $resourceId]), |
48
|
|
|
]; |
49
|
1 |
|
return $query; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function createScanQuery($tableName) |
53
|
|
|
{ |
54
|
|
|
$query = [ |
55
|
1 |
|
'TableName' => $tableName, |
56
|
|
|
]; |
57
|
1 |
|
return $query; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
public function createPostQuery($tableName, $primaryKey, $newResource) |
61
|
|
|
{ |
62
|
|
|
$query = [ |
63
|
6 |
|
'TableName' => $tableName, |
64
|
6 |
|
'Item' => $this->marshalItem($newResource), |
65
|
6 |
|
'ConditionExpression' => 'attribute_not_exists(#pk)', |
66
|
6 |
|
"ExpressionAttributeNames" => ["#pk" => $primaryKey], |
67
|
|
|
]; |
68
|
6 |
|
return $query; |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
public function createPutQuery($tableName, $primaryKey, $newResource) |
72
|
|
|
{ |
73
|
|
|
$query = [ |
74
|
5 |
|
'TableName' => $tableName, |
75
|
5 |
|
'Item' => $this->marshalItem($newResource), |
76
|
5 |
|
'ConditionExpression' => 'attribute_exists(#pk)', |
77
|
5 |
|
"ExpressionAttributeNames" => ["#pk" => $primaryKey], |
78
|
|
|
]; |
79
|
5 |
|
return $query; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function createDeleteQuery($tableName, $primaryKey, $resourceId) |
83
|
|
|
{ |
84
|
|
|
$query = [ |
85
|
2 |
|
'TableName' => $tableName, |
86
|
2 |
|
'Key' => $this->marshalItem([$primaryKey => $resourceId]), |
87
|
|
|
]; |
88
|
2 |
|
return $query; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|