InputModel::setInput()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 10
nc 6
nop 2
crap 6
1
<?php
2
3
namespace mindplay\kissform;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
7
/**
8
 * This model represents form state: input values and errors.
9
 */
10
class InputModel
11
{
12
    /**
13
     * @var array form input (maps of strings, possibly nested)
14
     */
15
    public $input;
16
17
    /**
18
     * @var string[] map where field name => error message
19
     */
20
    protected $errors;
21
22
    /**
23
     * @var bool true, if any validation has been performed
24
     */
25
    protected $validated = false;
26
27
    /**
28
     * @param array    $input  map where field name => input value(s)
29
     * @param string[] $errors map where field name => error message
30
     */
31 48
    public function __construct(array $input, array $errors)
32
    {
33 48
        $this->input = $input;
34 48
        $this->errors = $errors;
35 48
    }
36
37
    /**
38
     * @param InputModel|array|null $input  map where field name => input value(s)
39
     * @param string[]              $errors map where field name => error message
0 ignored issues
show
Documentation introduced by
Should the type for parameter $errors not be string[]|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     *
41
     * @return self
42
     */
43 48
    public static function create($input = null, $errors = null)
44
    {
45 48
        if ($input instanceof self) {
46 18
            return $input; // InputModel instance given
47
        }
48
49 48
        return new self($input ?: [], $errors ?: []);
50
    }
51
52
    /**
53
     * @param FieldInterface|string $field
54
     *
55
     * @return string|array|null value (or NULL, if no value exists in $input)
56
     */
57 40
    public function getInput($field)
58
    {
59 40
        $name = $field instanceof FieldInterface
60 40
            ? $field->getName()
61 40
            : (string) $field;
62
63 40
        if (!isset($this->input[$name]) || $this->input[$name] === '') {
64 28
            return null;
65
        }
66
67 34
        if (is_scalar($this->input[$name])) {
68 34
            return (string) $this->input[$name];
69
        }
70
71 1
        return $this->input[$name];
72
    }
73
74
    /**
75
     * @param FieldInterface|string $field
76
     * @param string|array|null     $value
77
     *
78
     * @return void
79
     */
80 15
    public function setInput($field, $value)
81
    {
82 15
        $name = $field instanceof FieldInterface
83 15
            ? $field->getName()
84 15
            : (string) $field;
85
86 15
        if ($value === null || $value === '' || $value === []) {
87 5
            unset($this->input[$name]);
88
        } else {
89 15
            $this->input[$name] = is_array($value)
90 1
                ? $value
91 14
                : (string) $value;
92
        }
93 15
    }
94
95
    /**
96
     * Get all accummulated error-messages, indexed by Field-name.
97
     *
98
     * @return string[] map where field-name => error message
99
     */
100 3
    public function getErrors()
101
    {
102 3
        return $this->errors;
103
    }
104
105
    /**
106
     * Get the error message for a given Field.
107
     *
108
     * @param FieldInterface|string $field
109
     *
110
     * @return string|string[]|null error-message (or NULL, if the given Field has no error)
111
     */
112 19
    public function getError($field)
113
    {
114 19
        return @$this->errors[$field instanceof FieldInterface ? $field->getName() : (string) $field];
115
    }
116
117
    /**
118
     * Set an error message for a given Field, if one is not already set for that
119
     * Field - we only care about the first error message for each Field, so add
120
     * error messages in order of importance.
121
     *
122
     * @param FieldInterface|string $field the field for which to set an error-message
123
     * @param string                $error error message
124
     *
125
     * @return void
126
     */
127 24
    public function setError($field, $error)
128
    {
129 24
        $name = $field instanceof FieldInterface
130 24
            ? $field->getName()
131 24
            : (string) $field;
132
133 24
        if (! isset($this->errors[$name])) {
134 24
            $this->errors[$name] = $error;
135
        }
136 24
    }
137
138
    /**
139
     * @param FieldInterface|string $field
140
     *
141
     * @return bool true, if the given Field has an error message
142
     *
143
     * @see $errors
144
     */
145 21
    public function hasError($field)
146
    {
147 21
        return isset($this->errors[$field instanceof FieldInterface ? $field->getName() : (string) $field]);
148
    }
149
150
    /**
151
     * Clear the current error message for a given Field
152
     *
153
     * @param FieldInterface|string $field Field to clear error message for
154
     *
155
     * @return void
156
     */
157 2
    public function clearError($field)
158
    {
159 2
        unset($this->errors[$field instanceof FieldInterface ? $field->getName(): (string) $field]);
160 2
    }
161
162
    /**
163
     * Check the model for errors - this does not take into account whether the
164
     * form has been validated or not.
165
     *
166
     * @return bool true, if the form contains any error(s)
167
     *
168
     * @see isValid()
169
     */
170 6
    public function hasErrors()
171
    {
172 6
        return count($this->errors) !== 0;
173
    }
174
175
    /**
176
     * Check if the model has been validated and contains no errors.
177
     *
178
     * @return bool true, if the form has been validated and contains no errors.
179
     *
180
     * @see hasErrors()
181
     */
182 3
    public function isValid()
183
    {
184 3
        return $this->validated && ! $this->hasErrors();
185
    }
186
187
    /**
188
     * Clears any accumulated error messages and marks the model as either
189
     * non-validated (default) or validated.
190
     *
191
     * @param bool $validated true, if the model has been validated
192
     *
193
     * @return void
194
     */
195 20
    public function clearErrors($validated = false)
196
    {
197 20
        $this->errors = [];
198
199 20
        $this->validated = $validated;
200 20
    }
201
}
202