|
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 setExpressionAttributeValues(array $mapping) |
|
34
|
|
|
* @method DynamoDBBuilder setFilterExpression(string $expression) |
|
35
|
|
|
* @method DynamoDBBuilder setKeyConditionExpression(string $expression) |
|
36
|
|
|
* @method DynamoDBBuilder setProjectionExpression(string $expression) |
|
37
|
|
|
* @method DynamoDBBuilder setUpdateExpression(string $expression) |
|
38
|
|
|
* @method DynamoDBBuilder setAttributeUpdates(array $updates) |
|
39
|
|
|
* @method DynamoDBBuilder setConsistentRead(bool $consistent) |
|
40
|
|
|
* @method DynamoDBBuilder setScanIndexForward(bool $forward) |
|
41
|
|
|
* @method DynamoDBBuilder setExclusiveStartKey(mixed $key) |
|
42
|
|
|
* @method DynamoDBBuilder setReturnValues(string $type) |
|
43
|
|
|
* @method DynamoDBBuilder setRequestItems(array $items) |
|
44
|
|
|
* @method DynamoDBBuilder setTableName(string $table) |
|
45
|
|
|
* @method DynamoDBBuilder setIndexName(string $index) |
|
46
|
|
|
* @method DynamoDBBuilder setSelect(string $select) |
|
47
|
|
|
* @method DynamoDBBuilder setItem(array $item) |
|
48
|
|
|
* @method DynamoDBBuilder setKeys(array $keys) |
|
49
|
|
|
* @method DynamoDBBuilder setLimit(int $limit) |
|
50
|
|
|
* @method DynamoDBBuilder setKey(array $key) |
|
51
|
|
|
*/ |
|
52
|
|
|
class DynamoDBBuilder |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* Query body to be sent to AWS |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
public $query = []; |
|
60
|
|
|
|
|
61
|
|
|
protected $dynamodbClient; |
|
62
|
|
|
|
|
63
|
|
|
public function __construct(array $config) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->dynamodbClient = new DynamoDbClient($config["S3Config"]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function hydrate(array $query) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->query = $query; |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
public function setExpressionAttributeName($placeholder, $name) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->query['ExpressionAttributeNames'][$placeholder] = $name; |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
public function setExpressionAttributeValue($placeholder, $value) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->query['ExpressionAttributeValues'][$placeholder] = $value; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
public function scan() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->dynamodbClient->scan($this->query); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function query() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->dynamodbClient->query($this->query); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $method |
|
97
|
|
|
* @param array $parameters |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __call($method, $parameters) |
|
101
|
|
|
{ |
|
102
|
|
|
if (strpos($method, 'set') === 0) { |
|
103
|
|
|
$key = array_reverse(explode('set', $method, 2))[0]; |
|
104
|
|
|
$this->query[$key] = current($parameters); |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
throw new BadMethodCallException(sprintf( |
|
|
|
|
|
|
108
|
|
|
'Method %s::%s does not exist.', |
|
109
|
|
|
static::class, |
|
110
|
|
|
$method |
|
111
|
|
|
)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
} |