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,9 @@ 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 ::isUrl ($ definition )
77+ ? self ::fromMethod ('fromYamlUrl ' , $ definition )
78+ : self ::fromMethod ('fromYamlFile ' , $ definition );
7479 }
7580
7681 /**
@@ -80,7 +85,9 @@ public static function fromYamlFile(string $definition): ValidatorBuilderInterfa
8085 */
8186 public static function fromJsonFile (string $ definition ): ValidatorBuilderInterface
8287 {
83- return self ::fromMethod ('readFromJsonFile ' , $ definition );
88+ return self ::isUrl ($ definition )
89+ ? self ::fromMethod ('fromJsonUrl ' , $ definition )
90+ : self ::fromMethod ('fromJsonFile ' , $ definition );
8491 }
8592
8693 /**
@@ -90,7 +97,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa
9097 */
9198 public static function fromYamlString (string $ definition ): ValidatorBuilderInterface
9299 {
93- return self ::fromMethod ('readFromYaml ' , $ definition, resolveReferences: true );
100+ return self ::fromMethod ('fromYaml ' , $ definition );
94101 }
95102
96103 /**
@@ -100,23 +107,59 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter
100107 */
101108 public static function fromJsonString (string $ definition ): ValidatorBuilderInterface
102109 {
103- return self ::fromMethod ('readFromJson ' , $ definition, resolveReferences: true );
110+ return self ::fromMethod ('fromJson ' , $ definition );
104111 }
105112
106113 /**
107- * Create a Validator object based on an OpenAPI definition.
114+ * @inheritDoc
108115 *
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
116+ * @param string $definition the OpenAPI definition's URL
117+ *
118+ * @throws InvalidArgumentException if the URL is invalid
119+ * @throws RuntimeException if the content of the URL cannot be read
112120 */
113- private static function fromMethod (string $ method , string $ definition, bool $ resolveReferences = false ): ValidatorBuilderInterface
121+ public static function fromYamlUrl (string $ definition ): ValidatorBuilderInterface
114122 {
115- $ specObject = Reader::{$ method }($ definition );
123+ return self ::fromMethod ('fromYaml ' , self ::getUrlContent ($ definition ));
124+ }
116125
117- $ resolveReferences && $ specObject ->resolveReferences (new ReferenceContext ($ specObject , '/ ' ));
126+ /**
127+ * @inheritDoc
128+ *
129+ * @param string $definition the OpenAPI definition's URL
130+ *
131+ * @throws InvalidArgumentException if the URL is invalid
132+ * @throws RuntimeException if the content of the URL cannot be read
133+ */
134+ public static function fromJsonUrl (string $ definition ): ValidatorBuilderInterface
135+ {
136+ return self ::fromMethod ('fromJson ' , self ::getUrlContent ($ definition ));
137+ }
118138
119- $ builder = (new BaseValidatorBuilder ())->fromSchema ($ specObject );
139+ /**
140+ * @throws InvalidArgumentException if the URL is invalid
141+ * @throws RuntimeException if the content of the URL cannot be read
142+ */
143+ private static function getUrlContent (string $ url ): string
144+ {
145+ self ::isUrl ($ url ) || throw new InvalidArgumentException (sprintf ('Invalid URL: %s ' , $ url ));
146+
147+ if (($ content = file_get_contents ($ url )) === false ) {
148+ throw new RuntimeException (sprintf ('Failed to read URL %s ' , $ url ));
149+ }
150+
151+ return $ content ;
152+ }
153+
154+ /**
155+ * Create a Validator object based on an OpenAPI definition.
156+ *
157+ * @param string $method the ValidatorBuilder object's method to use
158+ * @param string $definition the OpenAPI definition
159+ */
160+ private static function fromMethod (string $ method , string $ definition ): ValidatorBuilderInterface
161+ {
162+ $ builder = (new BaseValidatorBuilder ())->{$ method }($ definition );
120163
121164 return new ValidatorBuilder ($ builder );
122165 }
0 commit comments