|
7 | 7 |
|
8 | 8 | namespace cebe\openapi;
|
9 | 9 |
|
| 10 | +use cebe\openapi\exceptions\TypeErrorException; |
| 11 | +use cebe\openapi\exceptions\UnresolvableReferenceException; |
10 | 12 | use cebe\openapi\spec\OpenApi;
|
11 | 13 | use Symfony\Component\Yaml\Yaml;
|
12 | 14 |
|
13 | 15 | /**
|
14 |
| - * |
| 16 | + * Utility class to simplify reading JSON or YAML OpenAPI specs. |
15 | 17 | *
|
16 | 18 | */
|
17 | 19 | class Reader
|
18 | 20 | {
|
| 21 | + /** |
| 22 | + * Populate OpenAPI spec object from JSON data. |
| 23 | + * @param string $json the JSON string to decode. |
| 24 | + * @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]]. |
| 25 | + * The default is [[OpenApi]] which is the base type of a OpenAPI specification file. |
| 26 | + * You may choose a different type if you instantiate objects from sub sections of a specification. |
| 27 | + * @return SpecObjectInterface|OpenApi the OpenApi object instance. |
| 28 | + * @throws TypeErrorException in case invalid spec data is supplied. |
| 29 | + */ |
19 | 30 | public static function readFromJson(string $json, string $baseType = OpenApi::class): SpecObjectInterface
|
20 | 31 | {
|
21 | 32 | return new $baseType(json_decode($json, true));
|
22 | 33 | }
|
23 | 34 |
|
| 35 | + /** |
| 36 | + * Populate OpenAPI spec object from YAML data. |
| 37 | + * @param string $yaml the YAML string to decode. |
| 38 | + * @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]]. |
| 39 | + * The default is [[OpenApi]] which is the base type of a OpenAPI specification file. |
| 40 | + * You may choose a different type if you instantiate objects from sub sections of a specification. |
| 41 | + * @return SpecObjectInterface|OpenApi the OpenApi object instance. |
| 42 | + * @throws TypeErrorException in case invalid spec data is supplied. |
| 43 | + */ |
24 | 44 | public static function readFromYaml(string $yaml, string $baseType = OpenApi::class): SpecObjectInterface
|
25 | 45 | {
|
26 | 46 | return new $baseType(Yaml::parse($yaml));
|
27 | 47 | }
|
| 48 | + |
| 49 | + /** |
| 50 | + * Populate OpenAPI spec object from a JSON file. |
| 51 | + * @param string $fileName the file name of the file to be read. |
| 52 | + * If `$resolveReferences` is true (the default), this should be an absolute URL, a `file://` URI or |
| 53 | + * an absolute path to allow resolving relative path references. |
| 54 | + * @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]]. |
| 55 | + * The default is [[OpenApi]] which is the base type of a OpenAPI specification file. |
| 56 | + * You may choose a different type if you instantiate objects from sub sections of a specification. |
| 57 | + * @param bool $resolveReferences whether to automatically resolve references in the specification. |
| 58 | + * If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling |
| 59 | + * [[SpecObjectInterface::resolveReferences()]]. |
| 60 | + * @return SpecObjectInterface|OpenApi the OpenApi object instance. |
| 61 | + * @throws TypeErrorException in case invalid spec data is supplied. |
| 62 | + * @throws UnresolvableReferenceException in case references could not be resolved. |
| 63 | + */ |
| 64 | + public static function readFromJsonFile(string $fileName, string $baseType = OpenApi::class, $resolveReferences = true): SpecObjectInterface |
| 65 | + { |
| 66 | + $spec = static::readFromJson(file_get_contents($fileName), $baseType); |
| 67 | + if ($resolveReferences) { |
| 68 | + $spec->resolveReferences(new ReferenceContext($spec, $fileName)); |
| 69 | + } |
| 70 | + return $spec; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Populate OpenAPI spec object from YAML file. |
| 75 | + * @param string $fileName the file name of the file to be read. |
| 76 | + * If `$resolveReferences` is true (the default), this should be an absolute URL, a `file://` URI or |
| 77 | + * an absolute path to allow resolving relative path references. |
| 78 | + * @param string $baseType the base Type to instantiate. This must be an instance of [[SpecObjectInterface]]. |
| 79 | + * The default is [[OpenApi]] which is the base type of a OpenAPI specification file. |
| 80 | + * You may choose a different type if you instantiate objects from sub sections of a specification. |
| 81 | + * @param bool $resolveReferences whether to automatically resolve references in the specification. |
| 82 | + * If `true`, all [[Reference]] objects will be replaced with their referenced spec objects by calling |
| 83 | + * [[SpecObjectInterface::resolveReferences()]]. |
| 84 | + * @return SpecObjectInterface|OpenApi the OpenApi object instance. |
| 85 | + * @throws TypeErrorException in case invalid spec data is supplied. |
| 86 | + * @throws UnresolvableReferenceException in case references could not be resolved. |
| 87 | + */ |
| 88 | + public static function readFromYamlFile(string $fileName, string $baseType = OpenApi::class, $resolveReferences = true): SpecObjectInterface |
| 89 | + { |
| 90 | + $spec = static::readFromYaml(file_get_contents($fileName), $baseType); |
| 91 | + if ($resolveReferences) { |
| 92 | + $spec->resolveReferences(new ReferenceContext($spec, $fileName)); |
| 93 | + } |
| 94 | + return $spec; |
| 95 | + } |
28 | 96 | }
|
0 commit comments