-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathValidatorBuilder.php
More file actions
222 lines (190 loc) · 6.59 KB
/
ValidatorBuilder.php
File metadata and controls
222 lines (190 loc) · 6.59 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace Osteel\OpenApi\Testing;
use InvalidArgumentException;
use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder;
use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
use Osteel\OpenApi\Testing\Cache\CacheAdapterInterface;
use Osteel\OpenApi\Testing\Cache\Psr16Adapter;
use RuntimeException;
/**
* 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
{
return match (true) {
self::isUrl($definition) => self::fromYamlUrl($definition),
is_file($definition) => self::fromYamlFile($definition),
default => self::fromYamlString($definition),
};
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition
*/
public static function fromJson(string $definition): ValidatorBuilderInterface
{
return match (true) {
self::isUrl($definition) => self::fromJsonUrl($definition),
is_file($definition) => self::fromJsonFile($definition),
default => self::fromJsonString($definition),
};
}
private static function isUrl(string $value): bool
{
if (! filter_var($value, FILTER_VALIDATE_URL)) {
return false;
}
$scheme = parse_url($value, PHP_URL_SCHEME);
return in_array($scheme, ['http', 'https'], true);
}
/**
* @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);
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition's URL
*
* @throws InvalidArgumentException if the URL is invalid
* @throws RuntimeException if the content of the URL cannot be read
*/
public static function fromYamlUrl(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromYaml', self::getUrlContent($definition));
}
/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition's URL
*
* @throws InvalidArgumentException if the URL is invalid
* @throws RuntimeException if the content of the URL cannot be read
*/
public static function fromJsonUrl(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromJson', self::getUrlContent($definition));
}
/**
* @throws InvalidArgumentException if the URL is invalid
* @throws RuntimeException if the content of the URL cannot be read
*/
private static function getUrlContent(string $url): string
{
self::isUrl($url) || throw new InvalidArgumentException(sprintf('Invalid URL: %s', $url));
if (($content = file_get_contents($url)) === false) {
throw new RuntimeException(sprintf('Failed to read URL %s', $url));
}
return $content;
}
/**
* 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),
);
}
}