123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace Aws\DynamoDb;
- use Psr\Http\Message\StreamInterface;
- class Marshaler
- {
-
- private static $defaultOptions = [
- 'ignore_invalid' => false,
- 'nullify_invalid' => false,
- 'wrap_numbers' => false,
- ];
-
- private $options;
-
- public function __construct(array $options = [])
- {
- $this->options = $options + self::$defaultOptions;
- }
-
- public function binary($value)
- {
- return new BinaryValue($value);
- }
-
- public function number($value)
- {
- return new NumberValue($value);
- }
-
- public function set(array $values)
- {
- return new SetValue($values);
- }
-
- public function marshalJson($json)
- {
- $data = json_decode($json);
- if (!($data instanceof \stdClass)) {
- throw new \InvalidArgumentException(
- 'The JSON document must be valid and be an object at its root.'
- );
- }
- return current($this->marshalValue($data));
- }
-
- public function marshalItem($item)
- {
- return current($this->marshalValue($item));
- }
-
- public function marshalValue($value)
- {
- $type = gettype($value);
-
- if ($type === 'string') {
- return ['S' => $value];
- }
-
- if ($type === 'integer'
- || $type === 'double'
- || $value instanceof NumberValue
- ) {
- return ['N' => (string) $value];
- }
-
- if ($type === 'boolean') {
- return ['BOOL' => $value];
- }
-
- if ($type === 'NULL') {
- return ['NULL' => true];
- }
-
- if ($value instanceof SetValue) {
- if (count($value) === 0) {
- return $this->handleInvalid('empty sets are invalid');
- }
- $previousType = null;
- $data = [];
- foreach ($value as $v) {
- $marshaled = $this->marshalValue($v);
- $setType = key($marshaled);
- if (!$previousType) {
- $previousType = $setType;
- } elseif ($setType !== $previousType) {
- return $this->handleInvalid('sets must be uniform in type');
- }
- $data[] = current($marshaled);
- }
- return [$previousType . 'S' => array_values(array_unique($data))];
- }
-
- $dbType = 'L';
- if ($value instanceof \stdClass) {
- $type = 'array';
- $dbType = 'M';
- }
- if ($type === 'array' || $value instanceof \Traversable) {
- $data = [];
- $index = 0;
- foreach ($value as $k => $v) {
- if ($v = $this->marshalValue($v)) {
- $data[$k] = $v;
- if ($dbType === 'L' && (!is_int($k) || $k != $index++)) {
- $dbType = 'M';
- }
- }
- }
- return [$dbType => $data];
- }
-
- if (is_resource($value) || $value instanceof StreamInterface) {
- $value = $this->binary($value);
- }
- if ($value instanceof BinaryValue) {
- return ['B' => (string) $value];
- }
-
- return $this->handleInvalid('encountered unexpected value');
- }
-
- public function unmarshalJson(array $data, $jsonEncodeFlags = 0)
- {
- return json_encode(
- $this->unmarshalValue(['M' => $data], true),
- $jsonEncodeFlags
- );
- }
-
- public function unmarshalItem(array $data, $mapAsObject = false)
- {
- return $this->unmarshalValue(['M' => $data], $mapAsObject);
- }
-
- public function unmarshalValue(array $value, $mapAsObject = false)
- {
- $type = key($value);
- $value = $value[$type];
- switch ($type) {
- case 'S':
- case 'BOOL':
- return $value;
- case 'NULL':
- return null;
- case 'N':
- if ($this->options['wrap_numbers']) {
- return new NumberValue($value);
- }
-
- return $value + 0;
- case 'M':
- if ($mapAsObject) {
- $data = new \stdClass;
- foreach ($value as $k => $v) {
- $data->$k = $this->unmarshalValue($v, $mapAsObject);
- }
- return $data;
- }
-
- case 'L':
- foreach ($value as $k => $v) {
- $value[$k] = $this->unmarshalValue($v, $mapAsObject);
- }
- return $value;
- case 'B':
- return new BinaryValue($value);
- case 'SS':
- case 'NS':
- case 'BS':
- foreach ($value as $k => $v) {
- $value[$k] = $this->unmarshalValue([$type[0] => $v]);
- }
- return new SetValue($value);
- }
- throw new \UnexpectedValueException("Unexpected type: {$type}.");
- }
-
- private function handleInvalid($message)
- {
- if ($this->options['ignore_invalid']) {
- return null;
- }
- if ($this->options['nullify_invalid']) {
- return ['NULL' => true];
- }
- throw new \UnexpectedValueException("Marshaling error: {$message}.");
- }
- }
|