|
3 | 3 | namespace erasys\OpenApi\Spec\v3; |
4 | 4 |
|
5 | 5 | use ArrayAccess; |
| 6 | +use erasys\OpenApi\ExtensionProperty; |
6 | 7 | use erasys\OpenApi\RawValue; |
7 | 8 | use Illuminate\Contracts\Support\Arrayable; |
8 | 9 | use Illuminate\Contracts\Support\Jsonable; |
@@ -117,23 +118,47 @@ public function toArray() |
117 | 118 | private function exportValue($value) |
118 | 119 | { |
119 | 120 | if ($value instanceof RawValue) { |
| 121 | + // Unwrap value and return it raw, as it is. |
120 | 122 | return $value->value; |
121 | 123 | } |
122 | 124 |
|
123 | 125 | if ($value instanceof AbstractObject) { |
124 | 126 | return $value->toArray(); |
125 | 127 | } |
126 | 128 |
|
| 129 | + if ($value instanceof ExtensionProperty) { |
| 130 | + return [ |
| 131 | + $value->name => $this->exportValue($value->value), |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
127 | 135 | if (is_array($value)) { |
128 | 136 | $result = []; |
129 | 137 | foreach ($value as $k => $v) { |
130 | 138 | // Ignore null properties |
131 | 139 | if (is_null($v)) { |
132 | 140 | continue; |
133 | 141 | } |
134 | | - // Transform extension property names |
135 | | - if (preg_match('/^x[A-Z]/', $k)) { |
136 | | - $k = 'x-' . lcfirst(preg_replace('/^(x)/', '', $k)); |
| 142 | + // Take key and value from extension property definition |
| 143 | + if ($v instanceof ExtensionProperty) { |
| 144 | + $result[$v->name] = $this->exportValue($v->value); |
| 145 | + continue; |
| 146 | + } |
| 147 | + if (in_array($k, ['xml'])) { |
| 148 | + $result[$k] = $this->exportValue($v); |
| 149 | + continue; |
| 150 | + } |
| 151 | + // Transform extension property names using the 'x-dashes-format' |
| 152 | + if (preg_match('/^x[_]/', $k)) { |
| 153 | + $k = str_replace('_', '-', $k); |
| 154 | + $result[$k] = $this->exportValue($v); |
| 155 | + continue; |
| 156 | + } |
| 157 | + // Transform extension property names using the 'x-camelCaseFormat' |
| 158 | + if (preg_match('/^x[A-Za-z]/', $k)) { |
| 159 | + $k = 'x-' . lcfirst(preg_replace('/^(x)/', '', $k)); |
| 160 | + $result[$k] = $this->exportValue($v); |
| 161 | + continue; |
137 | 162 | } |
138 | 163 | // Transform reference property names |
139 | 164 | if ($k === 'ref') { |
|
0 commit comments