FailureCollection::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Failure;
10
11
use ArrayObject;
12
13
/**
14
 *
15
 * A collection of Failure objects.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
class FailureCollection extends ArrayObject
21
{
22
    /**
23
     *
24
     * Constructor.
25
     *
26
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     *
28
     */
29 13
    public function __construct()
30
    {
31 13
        parent::__construct(array());
32 13
    }
33
34
    /**
35
     *
36
     * Is the failure collection empty?
37
     *
38
     * @return bool
39
     *
40
     */
41 9
    public function isEmpty()
42
    {
43 9
        return count($this) === 0;
44
    }
45
46
    /**
47
     *
48
     * Set a failure on a field, removing all previous failures.
49
     *
50
     * @param string $field The field that failed.
51
     *
52
     * @param string $message The failure message.
53
     *
54
     * @param array $args The arguments passed to the rule specification.
55
     *
56
     * @return Failure
57
     *
58
     */
59 1
    public function set($field, $message, array $args = array())
60
    {
61 1
        $failure = $this->newFailure($field, $message, $args);
62 1
        $this[$field] = array($failure);
63 1
        return $failure;
64
    }
65
66
    /**
67
     *
68
     * Adds an additional failure on a field.
69
     *
70
     * @param string $field The field that failed.
71
     *
72
     * @param string $message The failure message.
73
     *
74
     * @param array $args The arguments passed to the rule specification.
75
     *
76
     * @return Failure
77
     *
78
     */
79 9
    public function add($field, $message, array $args = array())
80
    {
81 9
        $failure = $this->newFailure($field, $message, $args);
82 9
        $this[$field][] = $failure;
83 9
        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
     * @return Failure
97
     *
98
     */
99 9
    protected function newFailure($field, $message, array $args = array())
100
    {
101 9
        return new Failure($field, $message, $args);
102
    }
103
104
    /**
105
     *
106
     * Returns all failures for a field.
107
     *
108
     * @param string $field The field name.
109
     *
110
     * @return array
111
     *
112
     */
113 1
    public function forField($field)
114
    {
115 1
        if (! isset($this[$field])) {
116 1
            return array();
117
        }
118
119 1
        return $this[$field];
120
    }
121
122
    /**
123
     *
124
     * Returns all failure messages for all fields.
125
     *
126
     * @return array
127
     *
128
     */
129 7
    public function getMessages()
130
    {
131 7
        $messages = array();
132 7
        foreach ($this as $field => $failures) {
133 7
            $messages[$field] = $this->getMessagesForField($field);
134
        }
135 7
        return $messages;
136
    }
137
138
    /**
139
     *
140
     * Returns all failure messages for one field.
141
     *
142
     * @param string $field The field name.
143
     *
144
     * @return array
145
     *
146
     */
147 7
    public function getMessagesForField($field)
148
    {
149 7
        if (! isset($this[$field])) {
150 1
            return array();
151
        }
152
153 7
        $messages = array();
154 7
        foreach ($this[$field] as $failure) {
155 7
            $messages[] = $failure->getMessage();
156
        }
157 7
        return $messages;
158
    }
159
160
    /**
161
     *
162
     * Returns a single string of all failure messages for all fields.
163
     *
164
     * @param string $prefix Prefix each line with this string.
165
     *
166
     * @return string
167
     *
168
     */
169 1
    public function getMessagesAsString($prefix = '')
170
    {
171 1
        $string = '';
172 1
        foreach ($this as $field => $failures) {
173 1
            foreach ($failures as $failure) {
174 1
                $message = $failure->getMessage();
175 1
                $string .= "{$prefix}{$field}: {$message}" . PHP_EOL;
176
            }
177
        }
178 1
        return $string;
179
    }
180
181
    /**
182
     *
183
     * Returns a single string of all failure messages for one field.
184
     *
185
     * @param string $field The field name.
186
     *
187
     * @param string $prefix Prefix each line with this string.
188
     *
189
     * @return string
190
     *
191
     */
192 1
    public function getMessagesForFieldAsString($field, $prefix = '')
193
    {
194 1
        $string = '';
195 1
        foreach ($this->forField($field) as $failure) {
196 1
            $message = $failure->getMessage();
197 1
            $string .= "{$prefix}{$message}" . PHP_EOL;
198
        }
199 1
        return $string;
200
    }
201
}
202