|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace cebe\openapi\json; |
| 4 | + |
| 5 | +use JsonSerializable; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents a JSON Reference (IETF draft-pbryan-zyp-json-ref-03) |
| 9 | + * |
| 10 | + * Includes the URI to another JSON document and the JSON Pointer as |
| 11 | + * the fragment section of the URI. |
| 12 | + * |
| 13 | + * @link https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 |
| 14 | + * @see JsonPointer |
| 15 | + */ |
| 16 | +final class JsonReference implements JsonSerializable |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + private $_uri = ''; |
| 22 | + /** |
| 23 | + * @var JsonPointer |
| 24 | + */ |
| 25 | + private $_pointer; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a JSON Reference instance from a JSON document. |
| 29 | + * @param string $json the JSON object, e.g. `{ "$ref": "http://example.com/example.json#/foo/bar" }`. |
| 30 | + * @return JsonReference |
| 31 | + * @throws MalformedJsonReferenceObjectException |
| 32 | + * @throws InvalidJsonPointerSyntaxException if an invalid JSON pointer string is passed as part of the fragment section. |
| 33 | + */ |
| 34 | + public static function createFromJson(string $json): JsonReference |
| 35 | + { |
| 36 | + $refObject = json_decode($json, true); |
| 37 | + if (!isset($refObject['$ref'])) { |
| 38 | + throw new MalformedJsonReferenceObjectException('JSON Reference Object must contain the "$ref" member.'); |
| 39 | + } |
| 40 | + return static::createFromReference($refObject['$ref']); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create a JSON Reference instance from an URI and a JSON Pointer. |
| 45 | + * If no JSON Pointer is given this will be interpreted as an empty string JSON pointer, which |
| 46 | + * references the whole document. |
| 47 | + * @param string $uri the URI to the document without a fragment part. |
| 48 | + * @param JsonPointer $jsonPointer |
| 49 | + * @return JsonReference |
| 50 | + */ |
| 51 | + public static function createFromUri(string $uri, ?JsonPointer $jsonPointer = null): JsonReference |
| 52 | + { |
| 53 | + $jsonReference = static::createFromReference($uri); |
| 54 | + $jsonReference->_pointer = $jsonPointer ?: new JsonPointer(''); |
| 55 | + return $jsonReference; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Create a JSON Reference instance from a reference URI. |
| 60 | + * @param string $referenceURI the JSON Reference URI, e.g. `"http://example.com/example.json#/foo/bar"`. |
| 61 | + * @return JsonReference |
| 62 | + * @throws InvalidJsonPointerSyntaxException if an invalid JSON pointer string is passed as part of the fragment section. |
| 63 | + */ |
| 64 | + public static function createFromReference(string $referenceURI): JsonReference |
| 65 | + { |
| 66 | + $jsonReference = new JsonReference(); |
| 67 | + if (strpos($referenceURI, '#') !== false) { |
| 68 | + list($uri, $fragment) = explode('#', $referenceURI, 2); |
| 69 | + $jsonReference->_uri = $uri; |
| 70 | + $jsonReference->_pointer = new JsonPointer(rawurldecode($fragment)); |
| 71 | + } else { |
| 72 | + $jsonReference->_uri = $referenceURI; |
| 73 | + $jsonReference->_pointer = new JsonPointer(''); |
| 74 | + } |
| 75 | + return $jsonReference; |
| 76 | + } |
| 77 | + |
| 78 | + private function __construct() {} |
| 79 | + |
| 80 | + public function __clone() |
| 81 | + { |
| 82 | + $this->_pointer = clone $this->_pointer; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * @return string returns the JSON Pointer. |
| 88 | + */ |
| 89 | + public function getJsonPointer(): JsonPointer |
| 90 | + { |
| 91 | + return $this->_pointer; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return string returns the URI of the referenced JSON document without the fragment (JSON Pointer) part. |
| 96 | + */ |
| 97 | + public function getDocumentUri(): string |
| 98 | + { |
| 99 | + return $this->_uri; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return string returns the JSON Pointer in URI format. |
| 104 | + */ |
| 105 | + public function getReference(): string |
| 106 | + { |
| 107 | + // https://tools.ietf.org/html/rfc6901#section-6 |
| 108 | + // A JSON Pointer can be represented in a URI fragment identifier by |
| 109 | + // encoding it into octets using UTF-8 [RFC3629], while percent-encoding |
| 110 | + // those characters not allowed by the fragment rule in [RFC3986]. |
| 111 | + // https://tools.ietf.org/html/rfc3986#page-25 |
| 112 | + // The characters slash ("/") and question mark ("?") are allowed to |
| 113 | + // represent data within the fragment identifier. |
| 114 | + // https://tools.ietf.org/html/rfc3986#section-2.4 |
| 115 | + // the "%7E" can be replaced by "~" without changing its interpretation. |
| 116 | + return $this->_uri . '#' . strtr(rawurlencode($this->_pointer->getPointer()), ['%2F' => '/', '%3F' => '?', '%7E' => '~']); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Specify data which should be serialized to JSON |
| 121 | + * @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
| 122 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
| 123 | + * which is a value of any type other than a resource. |
| 124 | + */ |
| 125 | + public function jsonSerialize() |
| 126 | + { |
| 127 | + return (object)['$ref' => $this->getReference()]; |
| 128 | + } |
| 129 | +} |
0 commit comments