44
55namespace Osteel \OpenApi \Testing ;
66
7+ use Closure ;
78use InvalidArgumentException ;
89use League \OpenAPIValidation \PSR7 \ValidatorBuilder as BaseValidatorBuilder ;
910use Osteel \OpenApi \Testing \Adapters \MessageAdapterInterface ;
1617 */
1718final class ValidatorBuilder implements ValidatorBuilderInterface
1819{
20+ /** @var ?Closure():OpenApiSpecFactoryInterface */
21+ private static ?Closure $ openApiSpecFactoryResolver = null ;
22+
1923 /** @var class-string<MessageAdapterInterface> */
2024 private string $ adapter = HttpFoundationAdapter::class;
2125
@@ -33,9 +37,7 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder)
3337 */
3438 public static function fromYaml (string $ definition ): ValidatorBuilderInterface
3539 {
36- $ method = is_file ($ definition ) ? 'fromYamlFile ' : 'fromYaml ' ;
37-
38- return self ::fromMethod ($ method , $ definition );
40+ return self ::fromYamlFile ($ definition );
3941 }
4042
4143 /**
@@ -45,9 +47,7 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface
4547 */
4648 public static function fromJson (string $ definition ): ValidatorBuilderInterface
4749 {
48- $ method = is_file ($ definition ) ? 'fromJsonFile ' : 'fromJson ' ;
49-
50- return self ::fromMethod ($ method , $ definition );
50+ return self ::fromJsonFile ($ definition );
5151 }
5252
5353 /**
@@ -57,7 +57,7 @@ public static function fromJson(string $definition): ValidatorBuilderInterface
5757 */
5858 public static function fromYamlFile (string $ definition ): ValidatorBuilderInterface
5959 {
60- return self ::fromMethod (' fromYamlFile ' , $ definition );
60+ return self ::fromMethod (self :: determineMethod ( $ definition , ' yaml ' ) , $ definition );
6161 }
6262
6363 /**
@@ -67,7 +67,7 @@ public static function fromYamlFile(string $definition): ValidatorBuilderInterfa
6767 */
6868 public static function fromJsonFile (string $ definition ): ValidatorBuilderInterface
6969 {
70- return self ::fromMethod (' fromJsonFile ' , $ definition );
70+ return self ::fromMethod (self :: determineMethod ( $ definition , ' json ' ) , $ definition );
7171 }
7272
7373 /**
@@ -98,11 +98,51 @@ public static function fromJsonString(string $definition): ValidatorBuilderInter
9898 */
9999 private static function fromMethod (string $ method , string $ definition ): ValidatorBuilderInterface
100100 {
101+ $ openApiFactory = self ::getOpenApiSpecFactory ();
102+
103+ $ definition = match ($ method ) {
104+ 'fromJsonSchema ' => $ openApiFactory ->readFromJsonFile ($ definition ),
105+ 'fromYamlSchema ' => $ openApiFactory ->readFromYamlFile ($ definition ),
106+ default => $ definition ,
107+ };
108+
109+ if (in_array ($ method , ['fromJsonSchema ' , 'fromYamlSchema ' ], true )) {
110+ $ method = 'fromSchema ' ;
111+ }
112+
101113 $ builder = (new BaseValidatorBuilder ())->{$ method }($ definition );
102114
103115 return new ValidatorBuilder ($ builder );
104116 }
105117
118+ private static function determineMethod (string $ definition , string $ format ): string
119+ {
120+ if (filter_var ($ definition , FILTER_VALIDATE_URL ) && in_array (parse_url ($ definition , PHP_URL_SCHEME ), ['http ' , 'https ' ], true )) {
121+ return sprintf ('from%sSchema ' , ucfirst ($ format ));
122+ }
123+
124+ if (is_file ($ definition )) {
125+ return sprintf ('from%sFile ' , ucfirst ($ format ));
126+ }
127+
128+ return sprintf ('from%s ' , ucfirst ($ format ));
129+ }
130+
131+ /** @param ?Closure():OpenApiSpecFactoryInterface $resolver */
132+ public static function setOpenApiSpecFactoryResolver (?Closure $ resolver = null ): void
133+ {
134+ self ::$ openApiSpecFactoryResolver = $ resolver ;
135+ }
136+
137+ public static function getOpenApiSpecFactory (): OpenApiSpecFactoryInterface
138+ {
139+ if (self ::$ openApiSpecFactoryResolver === null ) {
140+ return new OpenApiSpecFactory ();
141+ }
142+
143+ return (self ::$ openApiSpecFactoryResolver )();
144+ }
145+
106146 /** @inheritDoc */
107147 public function setCache (object $ cache ): ValidatorBuilderInterface
108148 {
0 commit comments