|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AsyncAws\DynamoDb\Input; |
| 4 | + |
| 5 | +use AsyncAws\Core\Exception\InvalidArgument; |
| 6 | +use AsyncAws\Core\Input; |
| 7 | +use AsyncAws\Core\Request; |
| 8 | +use AsyncAws\Core\Stream\StreamFactory; |
| 9 | +use AsyncAws\DynamoDb\Enum\ReturnConsumedCapacity; |
| 10 | +use AsyncAws\DynamoDb\Enum\ReturnItemCollectionMetrics; |
| 11 | +use AsyncAws\DynamoDb\ValueObject\WriteRequest; |
| 12 | + |
| 13 | +final class BatchWriteItemInput extends Input |
| 14 | +{ |
| 15 | + /** |
| 16 | + * A map of one or more table names and, for each table, a list of operations to be performed (`DeleteRequest` or |
| 17 | + * `PutRequest`). Each element in the map consists of the following:. |
| 18 | + * |
| 19 | + * @required |
| 20 | + * |
| 21 | + * @var array<string, array> |
| 22 | + */ |
| 23 | + private $RequestItems; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var null|ReturnConsumedCapacity::* |
| 27 | + */ |
| 28 | + private $ReturnConsumedCapacity; |
| 29 | + |
| 30 | + /** |
| 31 | + * Determines whether item collection metrics are returned. If set to `SIZE`, the response includes statistics about |
| 32 | + * item collections, if any, that were modified during the operation are returned in the response. If set to `NONE` (the |
| 33 | + * default), no statistics are returned. |
| 34 | + * |
| 35 | + * @var null|ReturnItemCollectionMetrics::* |
| 36 | + */ |
| 37 | + private $ReturnItemCollectionMetrics; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param array{ |
| 41 | + * RequestItems?: array<string, array>, |
| 42 | + * ReturnConsumedCapacity?: \AsyncAws\DynamoDb\Enum\ReturnConsumedCapacity::*, |
| 43 | + * ReturnItemCollectionMetrics?: \AsyncAws\DynamoDb\Enum\ReturnItemCollectionMetrics::*, |
| 44 | + * @region?: string, |
| 45 | + * } $input |
| 46 | + */ |
| 47 | + public function __construct(array $input = []) |
| 48 | + { |
| 49 | + $this->RequestItems = []; |
| 50 | + foreach ($input['RequestItems'] ?? [] as $key => $item) { |
| 51 | + $this->RequestItems[$key] = array_map(function ($v) {return WriteRequest::create($v); }, $item); |
| 52 | + } |
| 53 | + $this->ReturnConsumedCapacity = $input['ReturnConsumedCapacity'] ?? null; |
| 54 | + $this->ReturnItemCollectionMetrics = $input['ReturnItemCollectionMetrics'] ?? null; |
| 55 | + parent::__construct($input); |
| 56 | + } |
| 57 | + |
| 58 | + public static function create($input): self |
| 59 | + { |
| 60 | + return $input instanceof self ? $input : new self($input); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return array<string, array> |
| 65 | + */ |
| 66 | + public function getRequestItems(): array |
| 67 | + { |
| 68 | + return $this->RequestItems; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return ReturnConsumedCapacity::*|null |
| 73 | + */ |
| 74 | + public function getReturnConsumedCapacity(): ?string |
| 75 | + { |
| 76 | + return $this->ReturnConsumedCapacity; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return ReturnItemCollectionMetrics::*|null |
| 81 | + */ |
| 82 | + public function getReturnItemCollectionMetrics(): ?string |
| 83 | + { |
| 84 | + return $this->ReturnItemCollectionMetrics; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @internal |
| 89 | + */ |
| 90 | + public function request(): Request |
| 91 | + { |
| 92 | + // Prepare headers |
| 93 | + $headers = [ |
| 94 | + 'Content-Type' => 'application/x-amz-json-1.0', |
| 95 | + 'X-Amz-Target' => 'DynamoDB_20120810.BatchWriteItem', |
| 96 | + ]; |
| 97 | + |
| 98 | + // Prepare query |
| 99 | + $query = []; |
| 100 | + |
| 101 | + // Prepare URI |
| 102 | + $uriString = '/'; |
| 103 | + |
| 104 | + // Prepare Body |
| 105 | + $bodyPayload = $this->requestBody(); |
| 106 | + $body = empty($bodyPayload) ? '{}' : json_encode($bodyPayload); |
| 107 | + |
| 108 | + // Return the Request |
| 109 | + return new Request('POST', $uriString, $query, $headers, StreamFactory::create($body)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param array<string, array> $value |
| 114 | + */ |
| 115 | + public function setRequestItems(array $value): self |
| 116 | + { |
| 117 | + $this->RequestItems = $value; |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param ReturnConsumedCapacity::*|null $value |
| 124 | + */ |
| 125 | + public function setReturnConsumedCapacity(?string $value): self |
| 126 | + { |
| 127 | + $this->ReturnConsumedCapacity = $value; |
| 128 | + |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param ReturnItemCollectionMetrics::*|null $value |
| 134 | + */ |
| 135 | + public function setReturnItemCollectionMetrics(?string $value): self |
| 136 | + { |
| 137 | + $this->ReturnItemCollectionMetrics = $value; |
| 138 | + |
| 139 | + return $this; |
| 140 | + } |
| 141 | + |
| 142 | + private function requestBody(): array |
| 143 | + { |
| 144 | + $payload = []; |
| 145 | + |
| 146 | + foreach ($this->RequestItems as $name => $v) { |
| 147 | + $index = -1; |
| 148 | + foreach ($v as $listValue) { |
| 149 | + ++$index; |
| 150 | + $payload['RequestItems'][$name][$index] = $listValue->requestBody(); |
| 151 | + } |
| 152 | + } |
| 153 | + if (null !== $v = $this->ReturnConsumedCapacity) { |
| 154 | + if (!ReturnConsumedCapacity::exists($v)) { |
| 155 | + throw new InvalidArgument(sprintf('Invalid parameter "ReturnConsumedCapacity" for "%s". The value "%s" is not a valid "ReturnConsumedCapacity".', __CLASS__, $v)); |
| 156 | + } |
| 157 | + $payload['ReturnConsumedCapacity'] = $v; |
| 158 | + } |
| 159 | + if (null !== $v = $this->ReturnItemCollectionMetrics) { |
| 160 | + if (!ReturnItemCollectionMetrics::exists($v)) { |
| 161 | + throw new InvalidArgument(sprintf('Invalid parameter "ReturnItemCollectionMetrics" for "%s". The value "%s" is not a valid "ReturnItemCollectionMetrics".', __CLASS__, $v)); |
| 162 | + } |
| 163 | + $payload['ReturnItemCollectionMetrics'] = $v; |
| 164 | + } |
| 165 | + |
| 166 | + return $payload; |
| 167 | + } |
| 168 | +} |
0 commit comments