|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Osteel\OpenApi\Testing; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use League\OpenAPIValidation\PSR7\ValidatorBuilder; |
| 9 | +use Osteel\OpenApi\Testing\HttpFoundation\HttpFoundationRequestAdapter; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class creates RequestValidator objects based on OpenAPI definitions. |
| 13 | + */ |
| 14 | +class RequestValidatorBuilder |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var ValidatorBuilder |
| 18 | + */ |
| 19 | + protected $validatorBuilder; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $adapter = HttpFoundationRequestAdapter::class; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor. |
| 28 | + * |
| 29 | + * @param ValidatorBuilder $builder |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct(ValidatorBuilder $builder) |
| 33 | + { |
| 34 | + $this->validatorBuilder = $builder; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a RequestValidator object based on a YAML OpenAPI definition. |
| 39 | + * |
| 40 | + * @param string $definition The OpenAPI definition. |
| 41 | + * @return RequestValidatorBuilder |
| 42 | + */ |
| 43 | + public static function fromYaml(string $definition): RequestValidatorBuilder |
| 44 | + { |
| 45 | + $method = is_file($definition) ? 'fromYamlFile' : 'fromYaml'; |
| 46 | + |
| 47 | + return self::fromMethod($method, $definition); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a RequestValidator object based on a JSON OpenAPI definition. |
| 52 | + * |
| 53 | + * @param string $definition The OpenAPI definition. |
| 54 | + * @return RequestValidatorBuilder |
| 55 | + */ |
| 56 | + public static function fromJson(string $definition): RequestValidatorBuilder |
| 57 | + { |
| 58 | + $method = is_file($definition) ? 'fromJsonFile' : 'fromJson'; |
| 59 | + |
| 60 | + return self::fromMethod($method, $definition); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Create a RequestValidator object based on an OpenAPI definition. |
| 65 | + * |
| 66 | + * @param string $method The ValidatorBuilder object's method to use. |
| 67 | + * @param string $definition The OpenAPI definition. |
| 68 | + * @return RequestValidatorBuilder |
| 69 | + */ |
| 70 | + private static function fromMethod(string $method, string $definition): RequestValidatorBuilder |
| 71 | + { |
| 72 | + $builder = (new ValidatorBuilder())->$method($definition); |
| 73 | + |
| 74 | + return new RequestValidatorBuilder($builder); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return the RequestValidator object. |
| 79 | + * |
| 80 | + * @return RequestValidator |
| 81 | + */ |
| 82 | + public function getValidator(): RequestValidator |
| 83 | + { |
| 84 | + return new RequestValidator($this->validatorBuilder->getRequestValidator(), new $this->adapter()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Change the request adapter to be used. The provided class must |
| 89 | + * implement the \Osteel\OpenApi\Testing\RequestAdapter interface. |
| 90 | + * |
| 91 | + * @param string $class The adapter's class. |
| 92 | + * @return self |
| 93 | + * @throws InvalidArgumentException |
| 94 | + */ |
| 95 | + public function setAdapter(string $class): self |
| 96 | + { |
| 97 | + if (! is_subclass_of($class, RequestAdapter::class)) { |
| 98 | + throw new InvalidArgumentException(sprintf( |
| 99 | + 'Class %s does not implement the %s interface', |
| 100 | + $class, |
| 101 | + RequestAdapter::class |
| 102 | + )); |
| 103 | + } |
| 104 | + |
| 105 | + $this->adapter = $class; |
| 106 | + |
| 107 | + return $this; |
| 108 | + } |
| 109 | +} |
0 commit comments