MessageViolationListException::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Werkspot\MessageBus\Bus\DeliveryChain\Middleware\Validation;
4
5
use ArrayIterator;
6
use Countable;
7
use IteratorAggregate;
8
use RuntimeException;
9
use Traversable;
10
11
class MessageViolationListException extends RuntimeException implements IteratorAggregate, Countable
12
{
13
    /**
14
     * @var MessageViolationException[]
15
     */
16
    private $violations;
17
18 4
    public function __construct()
19
    {
20 4
        $this->violations = [];
21 4
    }
22
23 3
    public function add(MessageViolationException $e): void
24
    {
25 3
        $this->violations[] = $e;
26
27 3
        $this->message .= $e->getMessage() . PHP_EOL;
28 3
    }
29
30
    /**
31
     * (PHP 5 &gt;= 5.0.0)<br/>
32
     * Retrieve an external iterator
33
     *
34
     * @see http://php.net/manual/en/iteratoraggregate.getiterator.php
35
     *
36
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
37
     * <b>Traversable</b>
38
     */
39 2
    public function getIterator(): Traversable
40
    {
41 2
        return new ArrayIterator($this->violations);
42
    }
43
44
    /**
45
     * (PHP 5 &gt;= 5.1.0)<br/>
46
     * Count elements of an object
47
     *
48
     * @see http://php.net/manual/en/countable.count.php
49
     *
50
     * @return int The custom count as an integer.
51
     * </p>
52
     * <p>
53
     * The return value is cast to an integer.
54
     */
55 3
    public function count(): int
56
    {
57 3
        return count($this->violations);
58
    }
59
60
    /**
61
     * @return array
62
     */
63 1
    public function getErrors(): array
64
    {
65 1
        $errors = [];
66
67 1
        foreach ($this->violations as $commandViolationException) {
68 1
            $fieldName = $commandViolationException->getField();
69
70 1
            if (preg_match('/(\w+)\[(\w+)\]/', $fieldName, $fieldNameMatch)) {
71 1
                $rootFieldName = $fieldNameMatch[1];
72 1
                $mainFieldName = $fieldNameMatch[2];
73
74 1
                $errors[$rootFieldName][$mainFieldName] = $commandViolationException->getError();
75 1
                continue;
76
            }
77
78 1
            if (isset($errors[$fieldName])) {
79
                // If we have multiple errors for the same field, merge them in an array
80 1
                if (!is_array($errors[$fieldName])) {
81 1
                    $errors[$fieldName] = [$errors[$fieldName]];
82
                }
83
84 1
                $errors[$fieldName][] = $commandViolationException->getError();
85 1
                continue;
86
            }
87
88 1
            $errors[$fieldName] = $commandViolationException->getError();
89
        }
90
91 1
        return $errors;
92
    }
93
}
94