Marshaler.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace Aws\DynamoDb;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Marshals and unmarshals JSON documents and PHP arrays into DynamoDB items.
  6. */
  7. class Marshaler
  8. {
  9. /** @var array Default options to merge into provided options. */
  10. private static $defaultOptions = [
  11. 'ignore_invalid' => false,
  12. 'nullify_invalid' => false,
  13. 'wrap_numbers' => false,
  14. ];
  15. /** @var array Marshaler options. */
  16. private $options;
  17. /**
  18. * Instantiates a DynamoDB Marshaler.
  19. *
  20. * The following options are valid.
  21. *
  22. * - ignore_invalid: (bool) Set to `true` if invalid values should be
  23. * ignored (i.e., not included) during marshaling.
  24. * - nullify_invalid: (bool) Set to `true` if invalid values should be set
  25. * to null.
  26. * - wrap_numbers: (bool) Set to `true` to wrap numbers with `NumberValue`
  27. * objects during unmarshaling to preserve the precision.
  28. *
  29. * @param array $options Marshaler options
  30. */
  31. public function __construct(array $options = [])
  32. {
  33. $this->options = $options + self::$defaultOptions;
  34. }
  35. /**
  36. * Creates a special object to represent a DynamoDB binary (B) value.
  37. *
  38. * This helps disambiguate binary values from string (S) values.
  39. *
  40. * @param mixed $value A binary value compatible with Guzzle streams.
  41. *
  42. * @return BinaryValue
  43. * @see GuzzleHttp\Stream\Stream::factory
  44. */
  45. public function binary($value)
  46. {
  47. return new BinaryValue($value);
  48. }
  49. /**
  50. * Creates a special object to represent a DynamoDB number (N) value.
  51. *
  52. * This helps maintain the precision of large integer/float in PHP.
  53. *
  54. * @param string|int|float $value A number value.
  55. *
  56. * @return NumberValue
  57. */
  58. public function number($value)
  59. {
  60. return new NumberValue($value);
  61. }
  62. /**
  63. * Creates a special object to represent a DynamoDB set (SS/NS/BS) value.
  64. *
  65. * This helps disambiguate set values from list (L) values.
  66. *
  67. * @param array $values The values of the set.
  68. *
  69. * @return SetValue
  70. *
  71. */
  72. public function set(array $values)
  73. {
  74. return new SetValue($values);
  75. }
  76. /**
  77. * Marshal a JSON document from a string to a DynamoDB item.
  78. *
  79. * The result is an array formatted in the proper parameter structure
  80. * required by the DynamoDB API for items.
  81. *
  82. * @param string $json A valid JSON document.
  83. *
  84. * @return array Item formatted for DynamoDB.
  85. * @throws \InvalidArgumentException if the JSON is invalid.
  86. */
  87. public function marshalJson($json)
  88. {
  89. $data = json_decode($json);
  90. if (!($data instanceof \stdClass)) {
  91. throw new \InvalidArgumentException(
  92. 'The JSON document must be valid and be an object at its root.'
  93. );
  94. }
  95. return current($this->marshalValue($data));
  96. }
  97. /**
  98. * Marshal a native PHP array of data to a DynamoDB item.
  99. *
  100. * The result is an array formatted in the proper parameter structure
  101. * required by the DynamoDB API for items.
  102. *
  103. * @param array|\stdClass $item An associative array of data.
  104. *
  105. * @return array Item formatted for DynamoDB.
  106. */
  107. public function marshalItem($item)
  108. {
  109. return current($this->marshalValue($item));
  110. }
  111. /**
  112. * Marshal a native PHP value into a DynamoDB attribute value.
  113. *
  114. * The result is an associative array that is formatted in the proper
  115. * `[TYPE => VALUE]` parameter structure required by the DynamoDB API.
  116. *
  117. * @param mixed $value A scalar, array, or `stdClass` value.
  118. *
  119. * @return array Attribute formatted for DynamoDB.
  120. * @throws \UnexpectedValueException if the value cannot be marshaled.
  121. */
  122. public function marshalValue($value)
  123. {
  124. $type = gettype($value);
  125. // Handle string values.
  126. if ($type === 'string') {
  127. return ['S' => $value];
  128. }
  129. // Handle number values.
  130. if ($type === 'integer'
  131. || $type === 'double'
  132. || $value instanceof NumberValue
  133. ) {
  134. return ['N' => (string) $value];
  135. }
  136. // Handle boolean values.
  137. if ($type === 'boolean') {
  138. return ['BOOL' => $value];
  139. }
  140. // Handle null values.
  141. if ($type === 'NULL') {
  142. return ['NULL' => true];
  143. }
  144. // Handle set values.
  145. if ($value instanceof SetValue) {
  146. if (count($value) === 0) {
  147. return $this->handleInvalid('empty sets are invalid');
  148. }
  149. $previousType = null;
  150. $data = [];
  151. foreach ($value as $v) {
  152. $marshaled = $this->marshalValue($v);
  153. $setType = key($marshaled);
  154. if (!$previousType) {
  155. $previousType = $setType;
  156. } elseif ($setType !== $previousType) {
  157. return $this->handleInvalid('sets must be uniform in type');
  158. }
  159. $data[] = current($marshaled);
  160. }
  161. return [$previousType . 'S' => array_values(array_unique($data))];
  162. }
  163. // Handle list and map values.
  164. $dbType = 'L';
  165. if ($value instanceof \stdClass) {
  166. $type = 'array';
  167. $dbType = 'M';
  168. }
  169. if ($type === 'array' || $value instanceof \Traversable) {
  170. $data = [];
  171. $index = 0;
  172. foreach ($value as $k => $v) {
  173. if ($v = $this->marshalValue($v)) {
  174. $data[$k] = $v;
  175. if ($dbType === 'L' && (!is_int($k) || $k != $index++)) {
  176. $dbType = 'M';
  177. }
  178. }
  179. }
  180. return [$dbType => $data];
  181. }
  182. // Handle binary values.
  183. if (is_resource($value) || $value instanceof StreamInterface) {
  184. $value = $this->binary($value);
  185. }
  186. if ($value instanceof BinaryValue) {
  187. return ['B' => (string) $value];
  188. }
  189. // Handle invalid values.
  190. return $this->handleInvalid('encountered unexpected value');
  191. }
  192. /**
  193. * Unmarshal a document (item) from a DynamoDB operation result into a JSON
  194. * document string.
  195. *
  196. * @param array $data Item/document from a DynamoDB result.
  197. * @param int $jsonEncodeFlags Flags to use with `json_encode()`.
  198. *
  199. * @return string
  200. */
  201. public function unmarshalJson(array $data, $jsonEncodeFlags = 0)
  202. {
  203. return json_encode(
  204. $this->unmarshalValue(['M' => $data], true),
  205. $jsonEncodeFlags
  206. );
  207. }
  208. /**
  209. * Unmarshal an item from a DynamoDB operation result into a native PHP
  210. * array. If you set $mapAsObject to true, then a stdClass value will be
  211. * returned instead.
  212. *
  213. * @param array $data Item from a DynamoDB result.
  214. * @param bool $mapAsObject Whether maps should be represented as stdClass.
  215. *
  216. * @return array|\stdClass
  217. */
  218. public function unmarshalItem(array $data, $mapAsObject = false)
  219. {
  220. return $this->unmarshalValue(['M' => $data], $mapAsObject);
  221. }
  222. /**
  223. * Unmarshal a value from a DynamoDB operation result into a native PHP
  224. * value. Will return a scalar, array, or (if you set $mapAsObject to true)
  225. * stdClass value.
  226. *
  227. * @param array $value Value from a DynamoDB result.
  228. * @param bool $mapAsObject Whether maps should be represented as stdClass.
  229. *
  230. * @return mixed
  231. * @throws \UnexpectedValueException
  232. */
  233. public function unmarshalValue(array $value, $mapAsObject = false)
  234. {
  235. $type = key($value);
  236. $value = $value[$type];
  237. switch ($type) {
  238. case 'S':
  239. case 'BOOL':
  240. return $value;
  241. case 'NULL':
  242. return null;
  243. case 'N':
  244. if ($this->options['wrap_numbers']) {
  245. return new NumberValue($value);
  246. }
  247. // Use type coercion to unmarshal numbers to int/float.
  248. return $value + 0;
  249. case 'M':
  250. if ($mapAsObject) {
  251. $data = new \stdClass;
  252. foreach ($value as $k => $v) {
  253. $data->$k = $this->unmarshalValue($v, $mapAsObject);
  254. }
  255. return $data;
  256. }
  257. // NOBREAK: Unmarshal M the same way as L, for arrays.
  258. case 'L':
  259. foreach ($value as $k => $v) {
  260. $value[$k] = $this->unmarshalValue($v, $mapAsObject);
  261. }
  262. return $value;
  263. case 'B':
  264. return new BinaryValue($value);
  265. case 'SS':
  266. case 'NS':
  267. case 'BS':
  268. foreach ($value as $k => $v) {
  269. $value[$k] = $this->unmarshalValue([$type[0] => $v]);
  270. }
  271. return new SetValue($value);
  272. }
  273. throw new \UnexpectedValueException("Unexpected type: {$type}.");
  274. }
  275. /**
  276. * Handle invalid value based on marshaler configuration.
  277. *
  278. * @param string $message Error message
  279. *
  280. * @return array|null
  281. */
  282. private function handleInvalid($message)
  283. {
  284. if ($this->options['ignore_invalid']) {
  285. return null;
  286. }
  287. if ($this->options['nullify_invalid']) {
  288. return ['NULL' => true];
  289. }
  290. throw new \UnexpectedValueException("Marshaling error: {$message}.");
  291. }
  292. }