-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathValidatorBuilder.php
165 lines (139 loc) · 4.73 KB
/
ValidatorBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Osteel\OpenApi\Testing;
use InvalidArgumentException;
use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter;
use Osteel\OpenApi\Testing\Cache\CacheAdapterInterface;
use Osteel\OpenApi\Testing\Cache\Psr16Adapter;
/**
* This class creates Validator objects based on OpenAPI definitions.
*/
final class ValidatorBuilder implements ValidatorBuilderInterface
{
/** @var class-string<MessageAdapterInterface> */
private string $adapter = HttpFoundationAdapter::class;
/** @var class-string<CacheAdapterInterface> */
private string $cacheAdapter = Psr16Adapter::class;
public function __construct(private BaseValidatorBuilder $validatorBuilder)
{
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition
*/
public static function fromYaml(string $definition): ValidatorBuilderInterface
{
$method = is_file($definition) ? 'fromYamlFile' : 'fromYaml';
return self::fromMethod($method, $definition);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition
*/
public static function fromJson(string $definition): ValidatorBuilderInterface
{
$method = is_file($definition) ? 'fromJsonFile' : 'fromJson';
return self::fromMethod($method, $definition);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition's file
*/
public static function fromYamlFile(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromYamlFile', $definition);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition's file
*/
public static function fromJsonFile(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromJsonFile', $definition);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition as YAML text
*/
public static function fromYamlString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromYaml', $definition);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition as JSON text
*/
public static function fromJsonString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromJson', $definition);
}
/**
* Create a Validator object based on an OpenAPI definition.
*
* @param string $method the ValidatorBuilder object's method to use
* @param string $definition the OpenAPI definition
*/
private static function fromMethod(string $method, string $definition): ValidatorBuilderInterface
{
$builder = (new BaseValidatorBuilder())->{$method}($definition);
return new ValidatorBuilder($builder);
}
/** @inheritDoc */
public function setCache(object $cache): ValidatorBuilderInterface
{
$adapter = new $this->cacheAdapter();
$this->validatorBuilder->setCache($adapter->convert($cache));
return $this;
}
/** @inheritDoc */
public function getValidator(): ValidatorInterface
{
return new Validator(
$this->validatorBuilder->getRoutedRequestValidator(),
$this->validatorBuilder->getResponseValidator(),
new $this->adapter()
);
}
/**
* Change the adapter to use. The provided class must implement \Osteel\OpenApi\Testing\Adapters\AdapterInterface.
*
* @param string $class the adapter's class
*
* @throws InvalidArgumentException
*/
public function setMessageAdapter(string $class): ValidatorBuilder
{
if (is_subclass_of($class, MessageAdapterInterface::class)) {
$this->adapter = $class;
return $this;
}
throw new InvalidArgumentException(
sprintf('Class %s does not implement the %s interface', $class, MessageAdapterInterface::class),
);
}
/**
* Change the cache adapter to use. The provided class must implement \Osteel\OpenApi\Testing\Cache\AdapterInterface.
*
* @param string $class the cache adapter's class
*
* @throws InvalidArgumentException
*/
public function setCacheAdapter(string $class): ValidatorBuilder
{
if (is_subclass_of($class, CacheAdapterInterface::class)) {
$this->cacheAdapter = $class;
return $this;
}
throw new InvalidArgumentException(
sprintf('Class %s does not implement the %s interface', $class, CacheAdapterInterface::class),
);
}
}