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