Passed
Push — master ( 0756fe...706dc7 )
by Arthur
14:47
created

ValidatorException::getViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Larapie\DataTransferObject\Exceptions;
4
5
use Symfony\Component\Validator\ConstraintViolationListInterface;
6
7
class ValidatorException extends \Symfony\Component\Validator\Exception\ValidatorException
8
{
9
10
    /**
11
     * @var ConstraintViolationListInterface[]
12
     */
13
    protected $violations;
14
15
    public function __construct($violations)
16
    {
17
        $this->violations = $violations;
18
        parent::__construct($this->buildMessage($violations));
19
    }
20
21
    protected function buildMessage($violations): string
22
    {
23
        $message = "";
24
        foreach ($violations as $propertyName => $propertyViolations) {
25
            if (!empty($propertyViolations))
26
                $message = $message . "Exception on property '" . $propertyName . "': ";
27
            foreach ($propertyViolations as $violation) {
28
                $message = $message . $violation->getMessage() . "";
29
            }
30
            $message = $message . "\n";
31
        }
32
        return $message;
33
    }
34
35
    public function propertyViolationExists(string $property, string $violationClass)
36
    {
37
        if (array_key_exists($property, $this->violations)) {
38
            foreach ($this->violations[$property] as $violation) {
39
                if ($violation instanceof $violationClass)
40
                    return true;
41
            }
42
        }
43
        return false;
44
    }
45
46
    public function getViolations(): array
47
    {
48
        return $this->violations;
49
    }
50
51
}