public function visitArray($data, Type $type, Context $context) { if (!is_array($data)) { throw new RuntimeException(sprintf('Expected array, but got %s: %s', gettype($data), json_encode($data))); } // If no further parameters were given, keys/values are just passed as is. if (!$type->hasParam(0)) { $this->setData($data); return $data; } switch ($type->countParams()) { case 1: // Array is a list. $listType = $type->getParam(0); $result = []; foreach ($data as $v) { $result[] = $context->accept($v, $listType); } $this->setData($result); return $result; case 2: // Array is a map. $keyType = $type->getParam(0); $entryType = $type->getParam(1); $result = []; foreach ($data as $k => $v) { $result[$context->accept($k, $keyType)] = $context->accept($v, $entryType); } $this->setData($result); return $result; default: throw new RuntimeException(sprintf('Array type cannot have more than 2 parameters, but got %s.', json_encode($type->getParams()))); } }
private function visitArray(VisitorInterface $visitor, $data, Type $type, $context) { if ($context instanceof SerializationContext && $type->hasParam(0) && !$type->hasParam(1)) { $data = array_values($data); } return $visitor->visitArray($data, $type, $context); }
/** * @return string * @param Type $type */ private function getFormat(Type $type) { return $type->hasParam(0) ? $type->getParam(0) : $this->defaultFormat; }