44
55namespace Osteel \OpenApi \Testing ;
66
7- use cebe \openapi \Reader ;
8- use cebe \openapi \ReferenceContext ;
97use InvalidArgumentException ;
108use League \OpenAPIValidation \PSR7 \ValidatorBuilder as BaseValidatorBuilder ;
119use Osteel \OpenApi \Testing \Adapters \HttpFoundationAdapter ;
1210use Osteel \OpenApi \Testing \Adapters \MessageAdapterInterface ;
1311use Osteel \OpenApi \Testing \Cache \CacheAdapterInterface ;
1412use Osteel \OpenApi \Testing \Cache \Psr16Adapter ;
13+ use RuntimeException ;
1514
1615/**
1716 * This class creates Validator objects based on OpenAPI definitions.
@@ -35,9 +34,11 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder)
3534 */
3635 public static function fromYaml (string $ definition ): ValidatorBuilderInterface
3736 {
38- return self ::isUrl ($ definition ) || is_file ($ definition )
39- ? self ::fromYamlFile ($ definition )
40- : self ::fromYamlString ($ definition );
37+ return match (true ) {
38+ self ::isUrl ($ definition ) => self ::fromYamlUrl ($ definition ),
39+ is_file ($ definition ) => self ::fromYamlFile ($ definition ),
40+ default => self ::fromYamlString ($ definition ),
41+ };
4142 }
4243
4344 /**
@@ -47,9 +48,11 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface
4748 */
4849 public static function fromJson (string $ definition ): ValidatorBuilderInterface
4950 {
50- return self ::isUrl ($ definition ) || is_file ($ definition )
51- ? self ::fromJsonFile ($ definition )
52- : self ::fromJsonString ($ definition );
51+ return match (true ) {
52+ self ::isUrl ($ definition ) => self ::fromJsonUrl ($ definition ),
53+ is_file ($ definition ) => self ::fromJsonFile ($ definition ),
54+ default => self ::fromJsonString ($ definition ),
55+ };
5356 }
5457
5558 private static function isUrl (string $ value ): bool
@@ -70,7 +73,7 @@ private static function isUrl(string $value): bool
7073 */
7174 public static function fromYamlFile (string $ definition ): ValidatorBuilderInterface
7275 {
73- return self ::fromMethod ('readFromYamlFile ' , $ definition );
76+ return self ::fromMethod ('fromYamlFile ' , $ definition );
7477 }
7578
7679 /**
@@ -80,7 +83,7 @@ public static function fromYamlFile(string $definition): ValidatorBuilderInterfa
8083 */
8184 public static function fromJsonFile (string $ definition ): ValidatorBuilderInterface
8285 {
83- return self ::fromMethod ('readFromJsonFile ' , $ definition );
86+ return self ::fromMethod ('fromJsonFile ' , $ definition );
8487 }
8588
8689 /**
@@ -90,7 +93,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa
9093 */
9194 public static function fromYamlString (string $ definition ): ValidatorBuilderInterface
9295 {
93- return self ::fromMethod ('readFromYaml ' , $ definition, resolveReferences: true );
96+ return self ::fromMethod ('fromYaml ' , $ definition );
9497 }
9598
9699 /**
@@ -100,23 +103,59 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter
100103 */
101104 public static function fromJsonString (string $ definition ): ValidatorBuilderInterface
102105 {
103- return self ::fromMethod ('readFromJson ' , $ definition, resolveReferences: true );
106+ return self ::fromMethod ('fromJson ' , $ definition );
104107 }
105108
106109 /**
107- * Create a Validator object based on an OpenAPI definition.
110+ * @inheritDoc
108111 *
109- * @param string $method the ValidatorBuilder object's method to use
110- * @param string $definition the OpenAPI definition
111- * @param bool $resolveReferences whether to resolve references in the definition
112+ * @param string $definition the OpenAPI definition's URL
113+ *
114+ * @throws InvalidArgumentException if the URL is invalid
115+ * @throws RuntimeException if the content of the URL cannot be read
112116 */
113- private static function fromMethod (string $ method , string $ definition, bool $ resolveReferences = false ): ValidatorBuilderInterface
117+ public static function fromYamlUrl (string $ definition ): ValidatorBuilderInterface
114118 {
115- $ specObject = Reader::{$ method }($ definition );
119+ return self ::fromMethod ('fromYaml ' , self ::getUrlContent ($ definition ));
120+ }
116121
117- $ resolveReferences && $ specObject ->resolveReferences (new ReferenceContext ($ specObject , '/ ' ));
122+ /**
123+ * @inheritDoc
124+ *
125+ * @param string $definition the OpenAPI definition's URL
126+ *
127+ * @throws InvalidArgumentException if the URL is invalid
128+ * @throws RuntimeException if the content of the URL cannot be read
129+ */
130+ public static function fromJsonUrl (string $ definition ): ValidatorBuilderInterface
131+ {
132+ return self ::fromMethod ('fromJson ' , self ::getUrlContent ($ definition ));
133+ }
118134
119- $ builder = (new BaseValidatorBuilder ())->fromSchema ($ specObject );
135+ /**
136+ * @throws InvalidArgumentException if the URL is invalid
137+ * @throws RuntimeException if the content of the URL cannot be read
138+ */
139+ private static function getUrlContent (string $ url ): string
140+ {
141+ self ::isUrl ($ url ) || throw new InvalidArgumentException (sprintf ('Invalid URL: %s ' , $ url ));
142+
143+ if (($ content = file_get_contents ($ url )) === false ) {
144+ throw new RuntimeException (sprintf ('Failed to read URL %s ' , $ url ));
145+ }
146+
147+ return $ content ;
148+ }
149+
150+ /**
151+ * Create a Validator object based on an OpenAPI definition.
152+ *
153+ * @param string $method the ValidatorBuilder object's method to use
154+ * @param string $definition the OpenAPI definition
155+ */
156+ private static function fromMethod (string $ method , string $ definition ): ValidatorBuilderInterface
157+ {
158+ $ builder = (new BaseValidatorBuilder ())->{$ method }($ definition );
120159
121160 return new ValidatorBuilder ($ builder );
122161 }
0 commit comments