1 | <?php |
||
13 | class Reference implements \JsonSerializable |
||
14 | { |
||
15 | /** |
||
16 | * A JSON object resulting from a json_decode call or a Closure. |
||
17 | * If a closure is used it should resolve to a JSON object when called. |
||
18 | * |
||
19 | * @var object|Closure |
||
20 | */ |
||
21 | private $schema; |
||
22 | |||
23 | /** |
||
24 | * A valid JSON reference. The reference should point to a location in $schema. |
||
25 | * @see https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $ref; |
||
30 | |||
31 | /** |
||
32 | * @param object|Closure $schema |
||
33 | * @param string $ref |
||
34 | */ |
||
35 | 62 | public function __construct($schema, $ref) |
|
36 | { |
||
37 | 62 | $this->schema = $schema; |
|
38 | 62 | $this->ref = $ref; |
|
39 | 62 | } |
|
40 | |||
41 | /** |
||
42 | * Specify data which should be serialized to JSON. |
||
43 | * Because a reference can be circular, references are always |
||
44 | * re-serialized as the reference property instead of attempting |
||
45 | * to inline the data. |
||
46 | * |
||
47 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
48 | * |
||
49 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
50 | * which is a value of any type other than a resource. |
||
51 | */ |
||
52 | 10 | public function jsonSerialize() |
|
53 | { |
||
54 | 10 | return ['$ref' => $this->ref]; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Resolve the reference and return the data. |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | 58 | public function resolve() |
|
63 | { |
||
64 | 58 | if ($this->schema instanceof Closure) { |
|
65 | 26 | return $this->resolveExternalReference(); |
|
66 | } |
||
67 | 44 | return $this->resolveInternalReference(); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Resolve an external reference and return the resolved schema. |
||
72 | * |
||
73 | * @return mixed The resolved schema |
||
74 | */ |
||
75 | 26 | private function resolveExternalReference() |
|
79 | |||
80 | /** |
||
81 | * Resolve an internal reference and return the resolved schema. |
||
82 | * |
||
83 | * @return mixed The resolved schema |
||
84 | */ |
||
85 | 44 | private function resolveInternalReference() |
|
91 | |||
92 | /** |
||
93 | * Proxies property access to the underlying schema. |
||
94 | * |
||
95 | * @param string $property |
||
96 | * |
||
97 | * @return mixed |
||
98 | * |
||
99 | * @throws \InvalidArgumentException |
||
100 | */ |
||
101 | 16 | public function __get($property) |
|
110 | } |
||
111 |