|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors |
| 5 | + * @license https://github.com/cebe/php-openapi/blob/master/LICENSE |
| 6 | + */ |
| 7 | + |
| 8 | +namespace cebe\openapi\spec; |
| 9 | + |
| 10 | +use cebe\openapi\exceptions\TypeErrorException; |
| 11 | +use cebe\openapi\SpecBaseObject; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is the root document object of the OpenAPI document. |
| 15 | + * |
| 16 | + * @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#openapi-object |
| 17 | + * |
| 18 | + * @property string $openapi |
| 19 | + * @property Info $info |
| 20 | + * @property Server[] $servers |
| 21 | + * @property Paths|PathItem[] $paths |
| 22 | + * @property Components|null $components |
| 23 | + * @property WebHooks|null $webhooks |
| 24 | + * @property SecurityRequirement[] $security |
| 25 | + * @property Tag[] $tags |
| 26 | + * @property ExternalDocumentation|null $externalDocs |
| 27 | + * |
| 28 | + */ |
| 29 | +class OpenApi extends SpecBaseObject |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @return array array of attributes available in this object. |
| 33 | + */ |
| 34 | + protected function attributes(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + 'openapi' => Type::STRING, |
| 38 | + 'info' => Info::class, |
| 39 | + 'servers' => [Server::class], |
| 40 | + 'paths' => Paths::class, |
| 41 | + 'webhooks' => WebHooks::class, |
| 42 | + 'components' => Components::class, |
| 43 | + 'security' => [SecurityRequirement::class], |
| 44 | + 'tags' => [Tag::class], |
| 45 | + 'externalDocs' => ExternalDocumentation::class, |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return array array of attributes default values. |
| 51 | + */ |
| 52 | + protected function attributeDefaults(): array |
| 53 | + { |
| 54 | + return [ |
| 55 | + // Spec: If the servers property is not provided, or is an empty array, |
| 56 | + // the default value would be a Server Object with a url value of /. |
| 57 | + 'servers' => [ |
| 58 | + new Server(['url' => '/']) |
| 59 | + ], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + public function __get($name) |
| 64 | + { |
| 65 | + $ret = parent::__get($name); |
| 66 | + // Spec: If the servers property is not provided, or is an empty array, |
| 67 | + // the default value would be a Server Object with a url value of /. |
| 68 | + if ($name === 'servers' && $ret === []) { |
| 69 | + return $this->attributeDefaults()['servers']; |
| 70 | + } |
| 71 | + return $ret; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Perform validation on this object, check data against OpenAPI Specification rules. |
| 76 | + */ |
| 77 | + public function performValidation() |
| 78 | + { |
| 79 | + $this->requireProperties(['openapi', 'info'], ['paths', 'webhooks']); |
| 80 | + if (!empty($this->openapi) && !preg_match('/^3\.(0|1)\.\d+(-rc\d)?$/i', $this->openapi)) { |
| 81 | + $this->addError('Unsupported openapi version: ' . $this->openapi); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments