Passed
Push — master ( fed690...6f2951 )
by Todd
28s
created

ValidationField   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 95.08%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 266
ccs 58
cts 61
cp 0.9508
rs 10
c 0
b 0
f 0
wmc 27

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addError() 0 3 1
A addTypeError() 0 14 4
A setField() 0 3 1
A merge() 0 3 1
A setName() 0 3 1
A getType() 0 2 1
A getOptions() 0 2 1
A getValidation() 0 2 1
A getErrorCount() 0 2 1
A val() 0 2 1
A isSparse() 0 2 1
A getOption() 0 2 1
A setSchemaPath() 0 3 1
A getField() 0 2 1
A getName() 0 2 1
A getSchemaPath() 0 2 1
A isResponse() 0 2 1
A isRequest() 0 2 1
A hasType() 0 2 1
A isValid() 0 2 1
A hasVal() 0 2 3
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2018 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Schema;
9
10
/**
11
 * A parameters class for field validation.
12
 *
13
 * This is an internal class and may change in the future.
14
 */
15
class ValidationField {
16
    /**
17
     * @var array|Schema
18
     */
19
    private $field;
20
21
    /**
22
     * @var Validation
23
     */
24
    private $validation;
25
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * @var string
38
     */
39
    private $schemaPath;
40
41
    /**
42
     * Construct a new {@link ValidationField} object.
43
     *
44
     * @param Validation $validation The validation object that contains errors.
45
     * @param array|Schema $field The field definition.
46
     * @param string $name The path to the field.
47
     * @param string $schemaPath The path to the field within the parent schema.
48
     *
49
     * - **sparse**: Whether or not this is a sparse validation.
50
     * @param array $options Validation options.
51
     */
52 216
    public function __construct(Validation $validation, $field, string $name, string $schemaPath, array $options = []) {
53 216
        $this->field = $field;
54 216
        $this->validation = $validation;
55 216
        $this->name = $name;
56 216
        $this->schemaPath = $schemaPath;
57 216
        $this->options = $options + ['sparse' => false];
58 216
    }
59
60
    /**
61
     * Add a validation error.
62
     *
63
     * @param string $error The message code.
64
     * @param array $options An array of additional information to add to the error entry or a numeric error code.
65
     * @return $this
66
     * @see Validation::addError()
67
     */
68 38
    public function addError(string $error, array $options = []) {
69 38
        $this->validation->addError($this->getName(), $error, $options);
70 38
        return $this;
71
    }
72
73
    /**
74
     * Add an invalid type error.
75
     *
76
     * @param mixed $value The erroneous value.
77
     * @param string $type The type that was checked.
78
     * @return $this
79
     */
80 42
    public function addTypeError($value, $type = '') {
81 42
        $type = $type ?: $this->getType();
82
83 42
        $this->validation->addError(
84 42
            $this->getName(),
85 42
            'type',
86
            [
87 42
                'type' => $type,
88 42
                'value' => is_scalar($value) ? $value : null,
89 42
                'messageCode' => is_scalar($value) ? "{value} is not a valid $type." : "The value is not a valid $type."
90
            ]
91
        );
92
93 42
        return $this;
94
    }
95
96
    /**
97
     * Check whether or not this field is has errors.
98
     *
99
     * @return bool Returns true if the field has no errors, false otherwise.
100
     */
101 87
    public function isValid() {
102 87
        return $this->getValidation()->isValidField($this->getName());
103
    }
104
105
    /**
106
     * Merge a validation object to this one.
107
     *
108
     * @param Validation $validation The validation object to merge.
109
     * @return $this
110
     */
111
    public function merge(Validation $validation) {
112
        $this->getValidation()->merge($validation, $this->getName());
113
        return $this;
114
    }
115
116
    /**
117
     * Get the field.
118
     *
119
     * @return array|Schema Returns the field.
120
     */
121 216
    public function getField() {
122 216
        return $this->field;
123
    }
124
125
    /**
126
     * Set the field.
127
     *
128
     * This method is only meant to be called from within the schema class.
129
     *
130
     * @param array|Schema $field The new field.
131
     * @return $this
132
     */
133 112
    public function setField($field) {
134 112
        $this->field = $field;
135 112
        return $this;
136
    }
137
138
    /**
139
     * Get the validation.
140
     *
141
     * @return Validation Returns the validation.
142
     */
143 216
    public function getValidation() {
144 216
        return $this->validation;
145
    }
146
147
    /**
148
     * Get the name.
149
     *
150
     * @return string Returns the name.
151
     */
152 177
    public function getName() {
153 177
        return $this->name;
154
    }
155
156
    /**
157
     * Set the name.
158
     *
159
     * This method is only meant to be called from within the schema class.
160
     *
161
     * @param string $name The new name.
162
     * @return $this
163
     */
164 127
    public function setName($name) {
165 127
        $this->name = ltrim($name, '/');
166 127
        return $this;
167
    }
168
169
    /**
170
     * Get the field type.
171
     *
172
     * @return string|string[] Returns a type string, array of type strings, or null if there isn't one.
173
     */
174 216
    public function getType() {
175 216
        return $this->field['type'] ?? '';
176
    }
177
178
    /**
179
     * Whether or not the field has a given type.
180
     *
181
     * @param string $type The single type to test.
182
     * @return bool Returns **true** if the field has the given type or **false** otherwise.
183
     */
184 32
    public function hasType($type) {
185 32
        return in_array($type, (array)$this->getType());
186
    }
187
188
    /**
189
     * Get a value fom the field.
190
     *
191
     * @param string $key The key to look at.
192
     * @param mixed $default The default to return if the key isn't found.
193
     * @return mixed Returns a value or the default.
194
     */
195 216
    public function val($key, $default = null) {
196 216
        return $this->field[$key] ?? $default;
197
    }
198
199
    /**
200
     * Whether or not the field has a value.
201
     *
202
     * @param string $key The key to look at.
203
     * @return bool Returns **true** if the field has a key or **false** otherwise.
204
     */
205 29
    public function hasVal($key) {
206 29
        return isset($this->field[$key]) || (is_array($this->field) && array_key_exists($key, $this->field));
207
    }
208
209
    /**
210
     * Get the error count for this field.
211
     */
212 63
    public function getErrorCount() {
213 63
        return $this->getValidation()->getErrorCount($this->getName());
214
    }
215
216
    /**
217
     * Whether or not we are validating a request.
218
     *
219
     * @return bool Returns **true** of we are validating a request or **false** otherwise.
220
     */
221 114
    public function isRequest(): bool {
222 114
        return $this->options['request'] ?? false;
223
    }
224
225
    /**
226
     * Whether or not we are validating a response.
227
     *
228
     * @return bool Returns **true** of we are validating a response or **false** otherwise.
229
     */
230 114
    public function isResponse(): bool {
231 114
        return $this->options['response'] ?? false;
232
    }
233
234
    /**
235
     * Whether or not this is a sparse validation..
236
     *
237
     * @return bool Returns **true** if this is a sparse validation or **false** otherwise.
238
     */
239 30
    public function isSparse() {
240 30
        return $this->getOption('sparse', false);
241
    }
242
243
    /**
244
     * Gets the options array.
245
     *
246
     * @return array Returns an options array.
247
     */
248 131
    public function getOptions(): array {
249 131
        return $this->options;
250
    }
251
252
    /**
253
     * Get an indivitual option.
254
     *
255
     * @param string $option The name of the option.
256
     * @param mixed $default The default value to return if the option doesn't exist.
257
     * @return mixed Returns the option or the default value.
258
     */
259 30
    public function getOption(string $option, $default = null) {
260 30
        return $this->options[$option] ?? $default;
261
    }
262
263
    /**
264
     * Get the schemaPath.
265
     *
266
     * @return string Returns the schemaPath.
267
     */
268 216
    public function getSchemaPath(): string {
269 216
        return $this->schemaPath;
270
    }
271
272
    /**
273
     * Set the schemaPath.
274
     *
275
     * @param string $schemaPath
276
     * @return $this
277
     */
278 112
    public function setSchemaPath(string $schemaPath) {
279 112
        $this->schemaPath = ltrim($schemaPath, '/');
280 112
        return $this;
281
    }
282
}
283