1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the OpenapiBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Niels Nijens <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\ExceptionHandling\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default violation implementation as part of the {@see InvalidRequestBodyProblemException}. |
18
|
|
|
* |
19
|
|
|
* @author Niels Nijens <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Violation implements ViolationInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $constraint; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $message; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $propertyPath; |
37
|
|
|
|
38
|
|
|
public function __construct(string $constraint, string $message, ?string $propertyPath = null) |
39
|
|
|
{ |
40
|
|
|
$this->constraint = $constraint; |
41
|
|
|
$this->message = $message; |
42
|
|
|
$this->propertyPath = $propertyPath; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPropertyPath(): ?string |
46
|
|
|
{ |
47
|
|
|
return $this->propertyPath; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getMessage(): string |
51
|
|
|
{ |
52
|
|
|
return $this->message; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getConstraint(): string |
56
|
|
|
{ |
57
|
|
|
return $this->constraint; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function jsonSerialize(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'constraint' => $this->getConstraint(), |
64
|
|
|
'message' => $this->getMessage(), |
65
|
|
|
'property' => $this->getPropertyPath(), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function fromArray(array $violation): self |
70
|
|
|
{ |
71
|
|
|
return new static( |
72
|
|
|
$violation['constraint'] ?? '', |
73
|
|
|
$violation['message'] ?? '', |
74
|
|
|
$violation['property'] ?? $violation['propertyPath'] ?? null |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|