|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Membrane\OpenAPIReader; |
| 6 | + |
| 7 | +use cebe\{openapi as Cebe, openapi\exceptions as CebeException, openapi\spec as CebeSpec}; |
| 8 | +use Closure; |
| 9 | +use Membrane\OpenAPIReader\Exception\{CannotRead, CannotSupport, InvalidOpenAPI}; |
| 10 | +use Membrane\OpenAPIReader\Factory\V30\FromCebe; |
| 11 | +use Symfony\Component\Yaml\Exception\ParseException; |
| 12 | +use TypeError; |
| 13 | + |
| 14 | +class CebeReader |
| 15 | +{ |
| 16 | + /** @param OpenAPIVersion[] $supportedVersions */ |
| 17 | + public function __construct( |
| 18 | + private readonly array $supportedVersions, |
| 19 | + ) { |
| 20 | + if (empty($this->supportedVersions)) { |
| 21 | + throw CannotSupport::noSupportedVersions(); |
| 22 | + } |
| 23 | + (fn (OpenAPIVersion ...$versions) => null)(...$this->supportedVersions); |
| 24 | + } |
| 25 | + |
| 26 | + public function readFromAbsoluteFilePath(string $absoluteFilePath, ?FileFormat $fileFormat = null): CebeSpec\OpenApi |
| 27 | + { |
| 28 | + file_exists($absoluteFilePath) ?: throw CannotRead::fileNotFound($absoluteFilePath); |
| 29 | + |
| 30 | + $fileFormat ??= FileFormat::fromFileExtension(pathinfo($absoluteFilePath, PATHINFO_EXTENSION)); |
| 31 | + |
| 32 | + try { |
| 33 | + $openAPI = $this->getCebeObject(match ($fileFormat) { |
| 34 | + FileFormat::Json => fn() => Cebe\Reader::readFromJsonFile($absoluteFilePath), |
| 35 | + FileFormat::Yaml => fn() => Cebe\Reader::readFromYamlFile($absoluteFilePath), |
| 36 | + default => throw CannotRead::unrecognizedFileFormat($absoluteFilePath) |
| 37 | + }); |
| 38 | + } catch (CebeException\UnresolvableReferenceException $e) { |
| 39 | + throw CannotRead::unresolvedReference($e); |
| 40 | + } |
| 41 | + |
| 42 | + $this->validate($openAPI); |
| 43 | + |
| 44 | + return $openAPI; |
| 45 | + } |
| 46 | + |
| 47 | + public function readFromString(string $openAPI, FileFormat $fileFormat): CebeSpec\OpenApi |
| 48 | + { |
| 49 | + if (preg_match('#\s*[\'\"]?\$ref[\'\"]?\s*:\s*[\'\"]?[^\s\'\"\#]#', $openAPI)) { |
| 50 | + throw CannotRead::cannotResolveExternalReferencesFromString(); |
| 51 | + } |
| 52 | + |
| 53 | + $openAPI = $this->getCebeObject(match ($fileFormat) { |
| 54 | + FileFormat::Json => fn() => Cebe\Reader::readFromJson($openAPI), |
| 55 | + FileFormat::Yaml => fn() => Cebe\Reader::readFromYaml($openAPI), |
| 56 | + }); |
| 57 | + |
| 58 | + try { |
| 59 | + $openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp')); |
| 60 | + } catch (CebeException\UnresolvableReferenceException $e) { |
| 61 | + throw CannotRead::unresolvedReference($e); |
| 62 | + } |
| 63 | + |
| 64 | + $this->validate($openAPI); |
| 65 | + |
| 66 | + return $openAPI; |
| 67 | + } |
| 68 | + |
| 69 | + /** @param Closure():CebeSpec\OpenApi $readOpenAPI */ |
| 70 | + private function getCebeObject(Closure $readOpenAPI): CebeSpec\OpenApi |
| 71 | + { |
| 72 | + try { |
| 73 | + return $readOpenAPI(); |
| 74 | + } catch (TypeError | CebeException\TypeErrorException | ParseException $e) { |
| 75 | + throw CannotRead::invalidFormatting($e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private function validate(CebeSpec\OpenApi $openAPI): void |
| 80 | + { |
| 81 | + $this->isVersionSupported($openAPI->openapi) ?: throw CannotSupport::unsupportedVersion($openAPI->openapi); |
| 82 | + |
| 83 | + /** Currently only 3.0 Validated Objects exist */ |
| 84 | + if (OpenAPIVersion::fromString($openAPI->openapi) === OpenAPIVersion::Version_3_0) { |
| 85 | + FromCebe::createOpenAPI($openAPI); |
| 86 | + } |
| 87 | + |
| 88 | + $openAPI->validate() ?: throw InvalidOpenAPI::failedCebeValidation(...$openAPI->getErrors()); |
| 89 | + } |
| 90 | + |
| 91 | + private function isVersionSupported(string $version): bool |
| 92 | + { |
| 93 | + return in_array(OpenAPIVersion::fromString($version), $this->supportedVersions, true); |
| 94 | + } |
| 95 | +} |
0 commit comments