|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the OpenapiBundle package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Niels Nijens <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\Json; |
|
15
|
|
|
|
|
16
|
|
|
use JsonSerializable; |
|
17
|
|
|
use Nijens\OpenapiBundle\Json\Exception\InvalidArgumentException; |
|
18
|
|
|
use stdClass; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* JSON reference container. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Niels Nijens <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Reference implements JsonSerializable |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $pointer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var stdClass |
|
34
|
|
|
*/ |
|
35
|
|
|
private $jsonSchema; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var JsonPointer |
|
39
|
|
|
*/ |
|
40
|
|
|
private $jsonPointer; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructs a new {@see Reference} instance. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(string $pointer, stdClass $jsonSchema) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->pointer = $pointer; |
|
48
|
|
|
$this->jsonSchema = $jsonSchema; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the JSON pointer of this reference. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPointer(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->pointer; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the (external) JSON schema being referenced. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getJsonSchema(): stdClass |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->jsonSchema; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function has(string $property): bool |
|
68
|
|
|
{ |
|
69
|
|
|
$schema = $this->resolve(); |
|
70
|
|
|
|
|
71
|
|
|
return isset($schema->{$property}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array|string|int|float|bool|stdClass|null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get(string $property) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->has($property) === false) { |
|
80
|
|
|
throw new InvalidArgumentException(sprintf('Unknown property "%s".', $property)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$schema = $this->resolve(); |
|
84
|
|
|
|
|
85
|
|
|
return $schema->{$property}; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function resolve(): stdClass |
|
89
|
|
|
{ |
|
90
|
|
|
$jsonPointer = $this->createJsonPointer(); |
|
91
|
|
|
|
|
92
|
|
|
return $jsonPointer->get($this->getPointer()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function jsonSerialize(): array |
|
96
|
|
|
{ |
|
97
|
|
|
return [ |
|
98
|
|
|
'$ref' => $this->getPointer(), |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function __isset(string $property): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->has($property); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array|string|int|float|bool|stdClass|null |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __get(string $property) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->get($property); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function createJsonPointer(): JsonPointer |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->jsonPointer instanceof JsonPointer === false) { |
|
118
|
|
|
$this->jsonPointer = new JsonPointer($this->getJsonSchema()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->jsonPointer; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|