Completed
Push — main ( bdd72f...bbe282 )
by Niels
15s queued 12s
created

Violation::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 2
b 0
f 0
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