1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hoooklife\DynamodbPodm\Grammars; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DynamoDBBuilder |
9
|
|
|
* |
10
|
|
|
* @package BaoPham\DynamoDb\DynamoDb |
11
|
|
|
* |
12
|
|
|
* Methods are in the form of `set<key_name>`, where `<key_name>` |
13
|
|
|
* is the key name of the query body to be sent. |
14
|
|
|
* |
15
|
|
|
* For example, to build a query: |
16
|
|
|
* [ |
17
|
|
|
* 'AttributeDefinitions' => ..., |
18
|
|
|
* 'GlobalSecondaryIndexUpdates' => ... |
19
|
|
|
* 'TableName' => ... |
20
|
|
|
* ] |
21
|
|
|
* |
22
|
|
|
* Do: |
23
|
|
|
* |
24
|
|
|
* $query = $query->setAttributeDefinitions(...)->setGlobalSecondaryIndexUpdates(...)->setTableName(...); |
25
|
|
|
* |
26
|
|
|
* When ready: |
27
|
|
|
* |
28
|
|
|
* $query->prepare()->updateTable(); |
29
|
|
|
* |
30
|
|
|
* Common methods: |
31
|
|
|
* |
32
|
|
|
* @method DynamoDBBuilder setExpressionAttributeNames(array $mapping) |
33
|
|
|
* @method DynamoDBBuilder setFilterExpression(string $expression) |
34
|
|
|
* @method DynamoDBBuilder setProjectionExpression(string $expression) |
35
|
|
|
* @method DynamoDBBuilder setUpdateExpression(string $expression) |
36
|
|
|
* @method DynamoDBBuilder setAttributeUpdates(array $updates) |
37
|
|
|
* @method DynamoDBBuilder setConsistentRead(bool $consistent) |
38
|
|
|
* @method DynamoDBBuilder setScanIndexForward(bool $forward) |
39
|
|
|
* @method DynamoDBBuilder setExclusiveStartKey(mixed $key) |
40
|
|
|
* @method DynamoDBBuilder setReturnValues(string $type) |
41
|
|
|
* @method DynamoDBBuilder setTableName(string $table) |
42
|
|
|
* @method DynamoDBBuilder setIndexName(string $index) |
43
|
|
|
* @method DynamoDBBuilder setSelect(string $select) |
44
|
|
|
* @method DynamoDBBuilder setItem(array $item) |
45
|
|
|
* @method DynamoDBBuilder setKeys(array $keys) |
46
|
|
|
* @method DynamoDBBuilder setLimit(int $limit) |
47
|
|
|
* @method DynamoDBBuilder setKey(array $key) |
48
|
|
|
*/ |
49
|
|
|
class DynamoDBBuilder |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Query body to be sent to AWS |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
public $query = []; |
57
|
|
|
public $batchWriteItem = []; |
58
|
|
|
|
59
|
|
|
/** @var $dynamodbClient DynamoDbClient */ |
|
|
|
|
60
|
|
|
protected $dynamodbClient; |
61
|
|
|
|
62
|
|
|
public function __construct(array $config) |
63
|
|
|
{ |
64
|
|
|
$this->dynamodbClient = new DynamoDbClient($config["S3Config"]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function hydrate(array $query) |
68
|
|
|
{ |
69
|
|
|
$this->query = $query; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setExpressionAttributeName($placeholder, $name) |
74
|
|
|
{ |
75
|
|
|
$this->query['ExpressionAttributeNames'][$placeholder] = $name; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setExpressionAttributeValue($placeholder, $value) |
80
|
|
|
{ |
81
|
|
|
$this->query['ExpressionAttributeValues'][$placeholder] = $value; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setKeyConditionExpression($mapping) |
86
|
|
|
{ |
87
|
|
|
if ($mapping) { |
88
|
|
|
$this->query['KeyConditionExpression'] = $mapping; |
89
|
|
|
} |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setExpressionAttributeValues($mapping) |
94
|
|
|
{ |
95
|
|
|
if ($mapping) { |
96
|
|
|
$this->query['ExpressionAttributeValues'] = $mapping; |
97
|
|
|
} |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setRequestItems($items) |
102
|
|
|
{ |
103
|
|
|
$this->batchWriteItem['RequestItems'] = $items; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function batchWriteItem() |
108
|
|
|
{ |
109
|
|
|
var_dump($this->batchWriteItem); |
|
|
|
|
110
|
|
|
return $this->dynamodbClient->batchWriteItem($this->batchWriteItem); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function scan() |
114
|
|
|
{ |
115
|
|
|
return $this->dynamodbClient->scan($this->query); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function query() |
119
|
|
|
{ |
120
|
|
|
var_dump($this->query); |
|
|
|
|
121
|
|
|
return $this->dynamodbClient->query($this->query); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $method |
126
|
|
|
* @param array $parameters |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function __call($method, $parameters) |
130
|
|
|
{ |
131
|
|
|
if (strpos($method, 'set') === 0) { |
132
|
|
|
$key = array_reverse(explode('set', $method, 2))[0]; |
133
|
|
|
$this->query[$key] = current($parameters); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
throw new BadMethodCallException(sprintf( |
|
|
|
|
137
|
|
|
'Method %s::%s does not exist.', |
138
|
|
|
static::class, |
139
|
|
|
$method |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
} |