|
27 | 27 | */
|
28 | 28 | class OpenApi extends SpecBaseObject
|
29 | 29 | {
|
| 30 | + const VERSION_3_0 = '3.0.x'; |
| 31 | + const VERSION_3_1 = '3.1.x'; |
| 32 | + const VERSION_UNSUPPORTED = 'unsupported'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Pattern used to validate OpenAPI versions. |
| 36 | + */ |
| 37 | + const PATTERN_VERSION = '/^(3\.(0|1))\.\d+(-rc\d)?$/i'; |
| 38 | + |
30 | 39 | /**
|
31 | 40 | * @return array array of attributes available in this object.
|
32 | 41 | */
|
@@ -75,8 +84,31 @@ public function __get($name)
|
75 | 84 | public function performValidation()
|
76 | 85 | {
|
77 | 86 | $this->requireProperties(['openapi', 'info', 'paths']);
|
78 |
| - if (!empty($this->openapi) && !preg_match('/^3\.0\.\d+(-rc\d)?$/i', $this->openapi)) { |
| 87 | + if (!empty($this->openapi) && !preg_match(static::PATTERN_VERSION, $this->openapi)) { |
79 | 88 | $this->addError('Unsupported openapi version: ' . $this->openapi);
|
80 | 89 | }
|
81 | 90 | }
|
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the OpenAPI major version of the loaded OpenAPI description. |
| 94 | + * @return string This returns a value of one of the `VERSION_*`-constants. Currently supported versions are: |
| 95 | + * |
| 96 | + * - `VERSION_3_0 = '3.0.x'` |
| 97 | + * - `VERSION_3_1 = '3.1.x'` |
| 98 | + * |
| 99 | + * For unsupported version, this function will return `VERSION_UNSUPPORTED = 'unsupported'` |
| 100 | + */ |
| 101 | + public function getMajorVersion() |
| 102 | + { |
| 103 | + if (preg_match(static::PATTERN_VERSION, $this->openapi, $matches)) { |
| 104 | + switch ($matches[1]) { |
| 105 | + case '3.0': |
| 106 | + return static::VERSION_3_0; |
| 107 | + case '3.1': |
| 108 | + return static::VERSION_3_1; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return self::VERSION_UNSUPPORTED; |
| 113 | + } |
82 | 114 | }
|
0 commit comments