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