Ejemplo n.º 1
0
 private function format(Shape $shape, $value)
 {
     switch ($shape['type']) {
         case 'structure':
             $data = [];
             foreach ($value as $k => $v) {
                 if ($v !== null && $shape->hasMember($k)) {
                     $valueShape = $shape->getMember($k);
                     $data[$valueShape['locationName'] ?: $k] = $this->format($valueShape, $v);
                 }
             }
             return $data;
         case 'list':
             $items = $shape->getMember();
             foreach ($value as &$v) {
                 $v = $this->format($items, $v);
             }
             return $value;
         case 'map':
             if (empty($value)) {
                 return new \stdClass();
             }
             $values = $shape->getValue();
             foreach ($value as &$v) {
                 $v = $this->format($values, $v);
             }
             return $value;
         case 'blob':
             return base64_encode($value);
         case 'timestamp':
             return TimestampShape::format($value, 'unixTimestamp');
         default:
             return $value;
     }
 }
Ejemplo n.º 2
0
 public function parse(Shape $shape, $value)
 {
     if ($value === null) {
         return $value;
     }
     switch ($shape['type']) {
         case 'structure':
             $target = [];
             foreach ($shape->getMembers() as $name => $member) {
                 $locationName = $member['locationName'] ?: $name;
                 if (isset($value[$locationName])) {
                     $target[$name] = $this->parse($member, $value[$locationName]);
                 }
             }
             return $target;
         case 'list':
             $member = $shape->getMember();
             $target = [];
             foreach ($value as $v) {
                 $target[] = $this->parse($member, $v);
             }
             return $target;
         case 'map':
             $values = $shape->getValue();
             $target = [];
             foreach ($value as $k => $v) {
                 $target[$k] = $this->parse($values, $v);
             }
             return $target;
         case 'timestamp':
             // The Unix epoch (or Unix time or POSIX time or Unix
             // timestamp) is the number of seconds that have elapsed since
             // January 1, 1970 (midnight UTC/GMT).
             return DateTimeResult::fromEpoch($value);
         case 'blob':
             return base64_decode($value);
         default:
             return $value;
     }
 }