Passed
Pull Request — 4.x (#156)
by Hari
10:20
created

FailureCollection::addSubfieldFailures()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 *
6
 * This file is part of Aura for PHP.
7
 *
8
 * @license http://opensource.org/licenses/mit-license.php MIT
9
 *
10
 */
11
namespace Aura\Filter\Failure;
12
13
use ArrayObject;
14
use Aura\Filter_Interface\FailureInterface;
15
use Aura\Filter_Interface\FailureCollectionInterface;
16
17
/**
18
 *
19
 * A collection of Failure objects.
20
 *
21
 * @package Aura.Filter
22
 *
23
 */
24
class FailureCollection extends ArrayObject implements FailureCollectionInterface
25
{
26
    /**
27
     *
28
     * Constructor.
29
     *
30
     * @return self
31
     *
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct(array());
36
    }
37
38
    /**
39
     *
40
     * Is the failure collection empty?
41
     *
42
     *
43
     */
44
    public function isEmpty(): bool
45
    {
46
        return count($this) === 0;
47
    }
48
49
    /**
50
     *
51
     * Set a failure on a field, removing all previous failures.
52
     *
53
     * @param string $field The field that failed.
54
     *
55
     * @param string $message The failure message.
56
     *
57
     * @param array $args The arguments passed to the rule specification.
58
     *
59
     *
60
     */
61
    public function set(string $field, string $message, array $args = array()): FailureInterface
62
    {
63
        $failure = $this->newFailure($field, $message, $args);
64
        $this[$field] = array($failure);
65
        return $failure;
66
    }
67
68
    /**
69
     *
70
     * Adds an additional failure on a field.
71
     *
72
     * @param string $field The field that failed.
73
     *
74
     * @param string $message The failure message.
75
     *
76
     * @param array $args The arguments passed to the rule specification.
77
     *
78
     */
79
    public function add(string $field, string $message, array $args = array()): FailureInterface
80
    {
81
        $failure = $this->newFailure($field, $message, $args);
82
        $this[$field][] = $failure;
83
        return $failure;
84
    }
85
86
    /**
87
     *
88
     * Factory method to return a new Failure object.
89
     *
90
     * @param string $field The field that failed.
91
     *
92
     * @param string $message The failure message.
93
     *
94
     * @param array $args The arguments passed to the rule specification.
95
     *
96
     *
97
     */
98
    protected function newFailure(string $field, string $message, array $args = array()): FailureInterface
99
    {
100
        return new Failure($field, $message, $args);
101
    }
102
103
    /**
104
     *
105
     * Returns all failures for a field.
106
     *
107
     * @param string $field The field name.
108
     *
109
     *
110
     * @return mixed[]
111
     */
112
    public function forField(string $field): array
113
    {
114
        if (! isset($this[$field])) {
115
            return array();
116
        }
117
118
        return $this[$field];
119
    }
120
121
    /**
122
     *
123
     * Returns all failure messages for all fields.
124
     *
125
     *
126
     * @return mixed[][]
127
     */
128
    public function getMessages(): array
129
    {
130
        $messages = array();
131
        foreach ($this as $field => $failures) {
132
            $messages[$field] = $this->getMessagesForField($field);
133
        }
134
        return $messages;
135
    }
136
137
    /**
138
     *
139
     * Returns all failure messages for one field.
140
     *
141
     * @param string $field The field name.
142
     *
143
     */
144
    public function getMessagesForField(string $field): array
145
    {
146
        if (! isset($this[$field])) {
147
            return array();
148
        }
149
150
        $messages = array();
151
152
        if ($this[$field] instanceof FailureCollection) {
153
            $messages = $this[$field]->getMessages();
154
        } else {
155
            foreach ($this[$field] as $failure) {
156
                $messages[] = $failure->getMessage();
157
            }
158
        }
159
160
        return $messages;
161
    }
162
163
    /**
164
     *
165
     * Returns a single string of all failure messages for all fields.
166
     *
167
     * @param string $prefix Prefix each line with this string.
168
     *
169
     *
170
     */
171
    public function getMessagesAsString(string $prefix = ''): string
172
    {
173
        $string = '';
174
        foreach ($this as $field => $failures) {
175
            foreach ($failures as $failure) {
176
                $message = $failure->getMessage();
177
                $string .= "{$prefix}{$field}: {$message}" . PHP_EOL;
178
            }
179
        }
180
        return $string;
181
    }
182
183
    /**
184
     *
185
     * Returns a single string of all failure messages for one field.
186
     *
187
     * @param string $field The field name.
188
     *
189
     * @param string $prefix Prefix each line with this string.
190
     *
191
     *
192
     */
193
    public function getMessagesForFieldAsString(string $field, string $prefix = ''): string
194
    {
195
        $string = '';
196
        foreach ($this->forField($field) as $failure) {
197
            $message = $failure->getMessage();
198
            $string .= "{$prefix}{$message}" . PHP_EOL;
199
        }
200
        return $string;
201
    }
202
203
    public function addSubfieldFailures($field, $spec)
204
    {
205
        $failure_collection = new FailureCollection();
206
207
        foreach ($spec->getMessage() as $f => $messages) {
208
            foreach ($messages as $message) {
209
                $failure_collection->add($f, $message, $spec->getArgs());
210
            }
211
        }
212
213
        $this[$field] = $failure_collection;
214
    }
215
}
216