|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CHStudio\Raven\Bridge\LeagueOpenAPIValidation; |
|
4
|
|
|
|
|
5
|
|
|
use CHStudio\Raven\Bridge\LeagueOpenAPIValidation\Exception\InvalidOpenApiDefinitionException; |
|
6
|
|
|
use CHStudio\Raven\Bridge\LeagueOpenAPIValidation\Exception\ValidationExceptionMapper; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use League\OpenAPIValidation\PSR7\ValidatorBuilder; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
|
|
11
|
|
|
class Factory |
|
12
|
|
|
{ |
|
13
|
|
|
private readonly ValidationExceptionMapper $mapper; |
|
14
|
|
|
|
|
15
|
5 |
|
public function __construct( |
|
16
|
|
|
private readonly ValidatorBuilder $validator, |
|
17
|
|
|
ValidationExceptionMapper $mapper = null |
|
18
|
|
|
) { |
|
19
|
5 |
|
$this->mapper = $mapper ?? new ValidationExceptionMapper(); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
2 |
|
public static function fromYamlFile(string $path): self |
|
23
|
|
|
{ |
|
24
|
2 |
|
if (!is_readable($path)) { |
|
25
|
1 |
|
throw new InvalidArgumentException( |
|
26
|
1 |
|
sprintf('Filename given isn\'t readable: %s', $path) |
|
27
|
1 |
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
return new self( |
|
31
|
1 |
|
(new ValidatorBuilder())->fromYamlFile($path) |
|
32
|
1 |
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public static function fromJsonFile(string $path): self |
|
36
|
|
|
{ |
|
37
|
2 |
|
if (!is_readable($path)) { |
|
38
|
1 |
|
throw new InvalidArgumentException( |
|
39
|
1 |
|
sprintf('Filename given isn\'t readable: %s', $path) |
|
40
|
1 |
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return new self( |
|
44
|
1 |
|
(new ValidatorBuilder())->fromJsonFile($path) |
|
45
|
1 |
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function getRequestValidator(): RequestValidator |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
4 |
|
return new RequestValidator($this->validator->getRequestValidator(), $this->mapper); |
|
52
|
1 |
|
} catch (Throwable $error) { |
|
53
|
1 |
|
throw new InvalidOpenApiDefinitionException($error); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
public function getResponseValidator(): ResponseValidator |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
4 |
|
return new ResponseValidator($this->validator->getResponseValidator(), $this->mapper); |
|
61
|
1 |
|
} catch (Throwable $error) { |
|
62
|
1 |
|
throw new InvalidOpenApiDefinitionException($error); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|