-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCallback.php
More file actions
188 lines (168 loc) · 5.1 KB
/
Callback.php
File metadata and controls
188 lines (168 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
*/
namespace cebe\openapi\spec;
use cebe\openapi\DocumentContextInterface;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\json\JsonPointer;
use cebe\openapi\ReferenceContext;
use cebe\openapi\SpecObjectInterface;
/**
* A map of possible out-of band callbacks related to the parent operation.
*
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#callbackObject
*
*/
class Callback implements SpecObjectInterface, DocumentContextInterface
{
/**
* @var string|null
*/
private $_url;
/**
* @var PathItem
*/
private $_pathItem;
/**
* @var array
*/
private $_errors = [];
/**
* @var SpecObjectInterface|null
*/
private $_baseDocument;
/**
* @var JsonPointer|null
*/
private $_jsonPointer;
/**
* Create an object from spec data.
* @param array $data spec data read from YAML or JSON
* @throws TypeErrorException in case invalid data is supplied.
*/
public function __construct(array $data)
{
if (count($data) !== 1) {
$this->_errors[] = 'Callback object must have exactly one URL.';
return;
}
$this->_pathItem = new PathItem(reset($data));
$this->_url = key($data);
}
/**
* @return mixed returns the serializable data of this object for converting it
* to JSON or YAML.
*/
public function getSerializableData()
{
return (object) [$this->_url => ($this->_pathItem === null) ? null : $this->_pathItem->getSerializableData()];
}
/**
* @return string
*/
public function getUrl()
{
return $this->_url;
}
/**
* @param string $url
*/
public function setUrl(string $url): void
{
$this->_url = $url;
}
/**
* @return PathItem
*/
public function getRequest(): ?PathItem
{
return $this->_pathItem;
}
/**
* @param PathItem $request
*/
public function setRequest(?PathItem $request): void
{
$this->_pathItem = $request;
}
/**
* Validate object data according to OpenAPI spec.
* @return bool whether the loaded data is valid according to OpenAPI spec
* @see getErrors()
*/
public function validate(): bool
{
$pathItemValid = $this->_pathItem === null || $this->_pathItem->validate();
return $pathItemValid && empty($this->_errors);
}
/**
* @return string[] list of validation errors according to OpenAPI spec.
* @see validate()
*/
public function getErrors(): array
{
if (($pos = $this->getDocumentPosition()) !== null) {
$errors = array_map(function ($e) use ($pos) {
return "[{$pos}] $e";
}, $this->_errors);
} else {
$errors = $this->_errors;
}
$pathItemErrors = $this->_pathItem === null ? [] : $this->_pathItem->getErrors();
return array_merge($errors, $pathItemErrors);
}
/**
* Resolves all Reference Objects in this object and replaces them with their resolution.
* @throws UnresolvableReferenceException
*/
public function resolveReferences(?ReferenceContext $context = null)
{
if ($this->_pathItem !== null) {
$this->_pathItem->resolveReferences($context);
}
}
/**
* Set context for all Reference Objects in this object.
*/
public function setReferenceContext(ReferenceContext $context)
{
if ($this->_pathItem !== null) {
$this->_pathItem->setReferenceContext($context);
}
}
/**
* Provide context information to the object.
*
* Context information contains a reference to the base object where it is contained in
* as well as a JSON pointer to its position.
* @param SpecObjectInterface $baseDocument
* @param JsonPointer $jsonPointer
*/
public function setDocumentContext(SpecObjectInterface $baseDocument, JsonPointer $jsonPointer)
{
$this->_baseDocument = $baseDocument;
$this->_jsonPointer = $jsonPointer;
if ($this->_pathItem instanceof DocumentContextInterface) {
$this->_pathItem->setDocumentContext($baseDocument, $jsonPointer->append($this->_url));
}
}
/**
* @return SpecObjectInterface|null returns the base document where this object is located in.
* Returns `null` if no context information was provided by [[setDocumentContext]].
*/
public function getBaseDocument(): ?SpecObjectInterface
{
return $this->_baseDocument;
}
/**
* @return JsonPointer|null returns a JSON pointer describing the position of this object in the base document.
* Returns `null` if no context information was provided by [[setDocumentContext]].
*/
public function getDocumentPosition(): ?JsonPointer
{
return $this->_jsonPointer;
}
}