/** * Serialize extref to JSON * * @param JsonSerializationVisitor $visitor Visitor * @param ExtReference $extReference Extref * @param array $type Type * @param Context $context Context * @return string|null */ public function serializeExtReferenceToJson(JsonSerializationVisitor $visitor, ExtReference $extReference, array $type, Context $context) { try { return $visitor->visitString($this->converter->getUrl($extReference), $type, $context); } catch (\InvalidArgumentException $e) { return $visitor->visitNull(null, $type, $context); } }
/** * Transforms an object (extref) to a string (url) * * @param ExtReference|null $extref extref object * @return string */ public function transform($extref) { if ($extref === null) { return ''; } try { return $this->converter->getUrl($extref); } catch (\InvalidArgumentException $e) { throw new TransformationFailedException(sprintf('Cannot transform extref "%s" to URL', json_encode($extref)), 0, $e); } }
/** * loops our structure recursively to find all $ref objects that need to be converted * either from that or to that.. * * @param mixed $input input structure * * @return array altered structure with replaced $ref objects */ public static function processDynExtRefs($input) { if ($input instanceof \stdClass) { if (!empty(get_object_vars($input))) { $input = self::processDynExtRefs(get_object_vars($input)); } return $input; } $externalRefFieldName = '$ref'; $internalRefFieldName = 'ref'; if (is_array($input)) { foreach ($input as $key => $value) { if ($key === $internalRefFieldName) { if (is_array($value) && isset($value['$ref']) && isset($value['$id'])) { $extRef = ExtReference::create($value['$ref'], $value['$id']); $input[$externalRefFieldName] = self::$extRefConverter->getUrl($extRef); unset($input[$internalRefFieldName]); } } elseif ($key === $externalRefFieldName) { $extRef = self::$extRefConverter->getExtReference($value); $input[$internalRefFieldName] = $extRef->jsonSerialize(); unset($input[$externalRefFieldName]); } else { if (is_array($value)) { $value = self::processDynExtRefs($value); } $input[$key] = $value; } } } return $input; }