7
7
8
8
namespace cebe \openapi \spec ;
9
9
10
+ use cebe \openapi \ReferenceContext ;
10
11
use cebe \openapi \SpecBaseObject ;
12
+ use cebe \openapi \SpecObjectInterface ;
13
+ use cebe \openapi \json \JsonPointer ;
11
14
12
15
/**
13
16
* Describes the operations available on a single path.
33
36
*/
34
37
class PathItem extends SpecBaseObject
35
38
{
39
+ /**
40
+ * @var Reference|null
41
+ */
42
+ private $ _ref ;
43
+
44
+
36
45
/**
37
46
* @return array array of attributes available in this object.
38
47
*/
@@ -54,6 +63,37 @@ protected function attributes(): array
54
63
];
55
64
}
56
65
66
+ /**
67
+ * Create an object from spec data.
68
+ * @param array $data spec data read from YAML or JSON
69
+ * @throws TypeErrorException in case invalid data is supplied.
70
+ */
71
+ public function __construct (array $ data )
72
+ {
73
+ if (isset ($ data ['$ref ' ])) {
74
+ // Allows for an external definition of this path item.
75
+ // $ref in a Path Item Object is not a Reference.
76
+ // https://github.com/OAI/OpenAPI-Specification/issues/1038
77
+ $ this ->_ref = new Reference (['$ref ' => $ data ['$ref ' ]], PathItem::class);
78
+ unset($ data ['$ref ' ]);
79
+ }
80
+
81
+ parent ::__construct ($ data );
82
+ }
83
+
84
+ /**
85
+ * @return mixed returns the serializable data of this object for converting it
86
+ * to JSON or YAML.
87
+ */
88
+ public function getSerializableData ()
89
+ {
90
+ $ data = parent ::getSerializableData ();
91
+ if ($ this ->_ref instanceof Reference) {
92
+ $ data ->{'$ref ' } = $ this ->_ref ->getReference ();
93
+ }
94
+ return $ data ;
95
+ }
96
+
57
97
/**
58
98
* Perform validation on this object, check data against OpenAPI Specification rules.
59
99
*/
@@ -76,4 +116,67 @@ public function getOperations()
76
116
}
77
117
return $ operations ;
78
118
}
119
+
120
+ /**
121
+ * Allows for an external definition of this path item. The referenced structure MUST be in the format of a
122
+ * PathItem Object. The properties of the referenced structure are merged with the local Path Item Object.
123
+ * If the same property exists in both, the referenced structure and the local one, this is a conflict.
124
+ * In this case the behavior is *undefined*.
125
+ * @return Reference|null
126
+ */
127
+ public function getReference (): ?Reference
128
+ {
129
+ return $ this ->_ref ;
130
+ }
131
+
132
+ /**
133
+ * Set context for all Reference Objects in this object.
134
+ */
135
+ public function setReferenceContext (ReferenceContext $ context )
136
+ {
137
+ if ($ this ->_ref instanceof Reference) {
138
+ $ this ->_ref ->setContext ($ context );
139
+ }
140
+ parent ::setReferenceContext ($ context );
141
+ }
142
+
143
+ /**
144
+ * Resolves all Reference Objects in this object and replaces them with their resolution.
145
+ * @throws exceptions\UnresolvableReferenceException in case resolving a reference fails.
146
+ */
147
+ public function resolveReferences (ReferenceContext $ context = null )
148
+ {
149
+ if ($ this ->_ref instanceof Reference) {
150
+ $ pathItem = $ this ->_ref ->resolve ($ context );
151
+ $ this ->_ref = null ;
152
+ // The properties of the referenced structure are merged with the local Path Item Object.
153
+ foreach (self ::attributes () as $ attribute => $ type ) {
154
+ if (!isset ($ pathItem ->$ attribute )) {
155
+ continue ;
156
+ }
157
+ // If the same property exists in both, the referenced structure and the local one, this is a conflict.
158
+ if (isset ($ this ->$ attribute ) && !empty ($ this ->$ attribute )) {
159
+ $ this ->addError ("Conflicting properties, property ' $ attribute' exists in local PathItem and also in the referenced one. " );
160
+ }
161
+ $ this ->$ attribute = $ pathItem ->$ attribute ;
162
+ }
163
+ }
164
+ parent ::resolveReferences ($ context );
165
+ }
166
+
167
+ /**
168
+ * Provide context information to the object.
169
+ *
170
+ * Context information contains a reference to the base object where it is contained in
171
+ * as well as a JSON pointer to its position.
172
+ * @param SpecObjectInterface $baseDocument
173
+ * @param JsonPointer $jsonPointer
174
+ */
175
+ public function setDocumentContext (SpecObjectInterface $ baseDocument , JsonPointer $ jsonPointer )
176
+ {
177
+ parent ::setDocumentContext ($ baseDocument , $ jsonPointer );
178
+ if ($ this ->_ref instanceof Reference) {
179
+ $ this ->_ref ->setDocumentContext ($ baseDocument , $ jsonPointer ->append ('$ref ' ));
180
+ }
181
+ }
79
182
}
0 commit comments