-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathValidator.php
165 lines (148 loc) · 4.44 KB
/
Validator.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 League\OpenAPIValidation\PSR7\Exception\ValidationFailed;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ResponseValidator;
use League\OpenAPIValidation\PSR7\RoutedServerRequestValidator;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
use Osteel\OpenApi\Testing\Exceptions\ValidationException;
use Psr\Http\Message\ResponseInterface;
final class Validator implements ValidatorInterface
{
public function __construct(
private RoutedServerRequestValidator $requestValidator,
private ResponseValidator $responseValidator,
private MessageAdapterInterface $adapter
) {
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param string $method the HTTP method
*
* @throws ValidationException
*/
public function validate(object $message, string $path, string $method): bool
{
$message = $this->adapter->convert($message);
$operation = $this->getOperationAddress($path, $method);
$validator = $message instanceof ResponseInterface ? $this->responseValidator : $this->requestValidator;
try {
$validator->validate($operation, $message);
} catch (ValidationFailed $exception) {
throw ValidationException::fromValidationFailed($exception);
}
return true;
}
/**
* Build and return an OperationAddress object from the path and method.
*
* @param string $path the OpenAPI path
* @param string $method the HTTP method
*/
private function getOperationAddress(string $path, string $method): OperationAddress
{
// Make sure the path begins with a forward slash.
$path = sprintf('/%s', ltrim($path, '/'));
return new OperationAddress($path, strtolower($method));
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function delete(object $message, string $path): bool
{
return $this->validate($message, $path, 'delete');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function get(object $message, string $path): bool
{
return $this->validate($message, $path, 'get');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function head(object $message, string $path): bool
{
return $this->validate($message, $path, 'head');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function options(object $message, string $path): bool
{
return $this->validate($message, $path, 'options');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function patch(object $message, string $path): bool
{
return $this->validate($message, $path, 'patch');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function post(object $message, string $path): bool
{
return $this->validate($message, $path, 'post');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function put(object $message, string $path): bool
{
return $this->validate($message, $path, 'put');
}
/**
* @inheritDoc
*
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function trace(object $message, string $path): bool
{
return $this->validate($message, $path, 'trace');
}
}