FailureCollection   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 155
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 3 1
A add() 0 6 1
A forField() 0 7 2
A getMessages() 0 8 2
A set() 0 6 1
A newFailure() 0 7 1
A getMessagesForFieldAsstring() 0 8 2
A getMessagesForField() 0 13 3
A getMessagesAsString() 0 11 3
1
<?php
2
3
namespace Mbright\Validation\Failure;
4
5
class FailureCollection extends \ArrayObject
6
{
7
    /**
8
     * Factory method to return a new Failure object.
9
     *
10
     * @param string $field The field that failed
11
     * @param string $message The failure message
12
     * @param string $ruleClass Class for the rule that failed
13
     * @param array $args The arguments passed to the rule specification
14
     *
15
     * @return ValidationFailure
16
     */
17 42
    protected function newFailure(
18
        string $field,
19
        string $message,
20
        ?string $ruleClass = null,
21
        array $args = []
22
    ): ValidationFailure {
23 42
        return new ValidationFailure($field, $message, $ruleClass, $args);
24
    }
25
26
    /**
27
     * Returns bool indicating if the array is empty.
28
     *
29
     * @return bool
30
     */
31 54
    public function isEmpty(): bool
32
    {
33 54
        return count($this) === 0;
34
    }
35
36
    /**
37
     * Set a failure on a field, removing all previous failures.
38
     *
39
     * @param string $field The field that failed
40
     * @param string $message The failure message
41
     * @param array $args The arguments passed to the rule specification
42
     *
43
     * @return ValidationFailure
44
     */
45 9
    public function set(string $field, string $message, array $args = []): ValidationFailure
46
    {
47 9
        $failure = $this->newFailure($field, $message, null, $args);
48 9
        $this[$field] = [$failure];
49
50 9
        return $failure;
51
    }
52
53
    /**
54
     * Adds an additional failure on a field.
55
     *
56
     * @param string $field The field that failed
57
     * @param string $message The failure message
58
     * @param string $ruleClass Class for the rule that failed
59
     * @param array $args The arguments passed to the rule specification
60
     *
61
     * @return ValidationFailure
62
     */
63 33
    public function add(string $field, string $message, string $ruleClass, array $args = []): ValidationFailure
64
    {
65 33
        $failure = $this->newFailure($field, $message, $ruleClass, $args);
66 33
        $this[$field][] = $failure;
67
68 33
        return $failure;
69
    }
70
71
    /**
72
     * Returns all failures for a field.
73
     *
74
     * @param string $field The field name
75
     *
76
     * @return array
77
     */
78 9
    public function forField(string $field): array
79
    {
80 9
        if (!isset($this[$field])) {
81 3
            return [];
82
        }
83
84 6
        return $this[$field];
85
    }
86
87
    /**
88
     * Returns all failure messages for all fields.
89
     *
90
     * @return array
91
     */
92 24
    public function getMessages(): array
93
    {
94 24
        $messages = [];
95 24
        foreach ($this as $field => $failures) {
96 24
            $messages[$field] = $this->getMessagesForField($field);
97
        }
98
99 24
        return $messages;
100
    }
101
102
    /**
103
     * Returns all failure messages for one field.
104
     *
105
     * @param string $field The field name
106
     *
107
     * @return array
108
     */
109 27
    public function getMessagesForField(string $field): array
110
    {
111 27
        if (!isset($this[$field])) {
112 3
            return [];
113
        }
114 24
        $messages = [];
115
116
        /** @var ValidationFailure $failure */
117 24
        foreach ($this[$field] as $failure) {
118 24
            $messages[] = $failure->getMessage();
119
        }
120
121 24
        return $messages;
122
    }
123
124
    /**
125
     * Returns a single string of all failure messages for all fields.
126
     *
127
     * @param string $prefix Prefix each line with this string
128
     *
129
     * @return string
130
     */
131 3
    public function getMessagesAsString($prefix = ''): string
132
    {
133 3
        $string = '';
134 3
        foreach ($this as $field => $failures) {
135
            /** @var ValidationFailure $failure */
136 3
            foreach ($failures as $failure) {
137 3
                $message = $failure->getMessage();
138 3
                $string .= "{$prefix}{$field}: {$message}" . PHP_EOL;
139
            }
140
        }
141 3
        return $string;
142
    }
143
144
    /**
145
     * Returns a single string of all failure messages for one field.
146
     *
147
     * @param string $field The field name
148
     * @param string $prefix Prefix each line with this string
149
     *
150
     * @return string
151
     */
152 3
    public function getMessagesForFieldAsstring($field, $prefix = ''): string
153
    {
154 3
        $string = '';
155 3
        foreach ($this->forField($field) as $failure) {
156 3
            $message = $failure->getMessage();
157 3
            $string .= "{$prefix}{$message}" . PHP_EOL;
158
        }
159 3
        return $string;
160
    }
161
}
162