-
Notifications
You must be signed in to change notification settings - Fork 96
/
ResponseValidator.php
46 lines (39 loc) · 1.24 KB
/
ResponseValidator.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
<?php
declare(strict_types=1);
namespace League\OpenAPIValidation\PSR7;
use cebe\openapi\spec\OpenApi;
use League\OpenAPIValidation\PSR7\Exception\ValidationFailed;
use League\OpenAPIValidation\PSR7\Validators\BodyValidator\BodyValidator;
use League\OpenAPIValidation\PSR7\Validators\HeadersValidator;
use League\OpenAPIValidation\PSR7\Validators\ValidatorChain;
use Psr\Http\Message\ResponseInterface;
class ResponseValidator implements ReusableSchema
{
/** @var OpenApi */
protected $openApi;
/** @var MessageValidator */
protected $validator;
public function __construct(OpenApi $schema)
{
$this->openApi = $schema;
$finder = new SpecFinder($this->openApi);
$this->validator = new ValidatorChain(
new HeadersValidator($finder),
new BodyValidator($finder)
);
}
public function getSchema(): OpenApi
{
return $this->openApi;
}
/**
* @throws ValidationFailed
*/
public function validate(OperationAddress $opAddr, ResponseInterface $response): void
{
$this->validator->validate(
new ResponseAddress($opAddr->path(), $opAddr->method(), $response->getStatusCode()),
$response
);
}
}