Passed
Pull Request — master (#69)
by Todd
03:16
created

ValidationField::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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 236
    public function __construct(Validation $validation, $field, string $name, string $schemaPath, array $options = []) {
53 236
        $this->field = $field;
54 236
        $this->validation = $validation;
55 236
        $this->name = $name;
56 236
        $this->schemaPath = $schemaPath;
57 236
        $this->options = $options + ['sparse' => false];
58 236
    }
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 46
    public function addError(string $error, array $options = []) {
69 46
        $this->validation->addError($this->getName(), $error, $options);
70 46
        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 43
    public function addTypeError($value, $type = '') {
81 43
        $type = $type ?: $this->getType();
82
83 43
        if ($value === null) {
84
            // Give a different message for null values.
85 12
            $messageCode = "The value cannot be null.";
86
        } else {
87 37
            $messageCode = is_scalar($value) ? "{value} is not a valid $type." : "The value is not a valid $type.";
88
        }
89
90 43
        $this->validation->addError(
91 43
            $this->getName(),
92 43
            'type',
93
            [
94 43
                'type' => $type,
95 43
                'value' => is_scalar($value) ? $value : null,
96 43
                'messageCode' => $messageCode,
97
            ]
98
        );
99
100 43
        return $this;
101
    }
102
103
    /**
104
     * Check whether or not this field is has errors.
105
     *
106
     * @return bool Returns true if the field has no errors, false otherwise.
107
     */
108 102
    public function isValid() {
109 102
        return $this->getValidation()->isValidField($this->getName());
110
    }
111
112
    /**
113
     * Merge a validation object to this one.
114
     *
115
     * @param Validation $validation The validation object to merge.
116
     * @return $this
117
     */
118
    public function merge(Validation $validation) {
119
        $this->getValidation()->merge($validation, $this->getName());
120
        return $this;
121
    }
122
123
    /**
124
     * Get the field.
125
     *
126
     * @return array|Schema Returns the field.
127
     */
128 233
    public function getField() {
129 233
        return $this->field;
130
    }
131
132
    /**
133
     * Set the field.
134
     *
135
     * This method is only meant to be called from within the schema class.
136
     *
137
     * @param array|Schema $field The new field.
138
     * @return $this
139
     */
140 119
    public function setField($field) {
141 119
        $this->field = $field;
142 119
        return $this;
143
    }
144
145
    /**
146
     * Get the validation.
147
     *
148
     * @return Validation Returns the validation.
149
     */
150 236
    public function getValidation() {
151 236
        return $this->validation;
152
    }
153
154
    /**
155
     * Get the name.
156
     *
157
     * @return string Returns the name.
158
     */
159 194
    public function getName() {
160 194
        return $this->name;
161
    }
162
163
    /**
164
     * Set the name.
165
     *
166
     * This method is only meant to be called from within the schema class.
167
     *
168
     * @param string $name The new name.
169
     * @return $this
170
     */
171 134
    public function setName($name) {
172 134
        $this->name = ltrim($name, '/');
173 134
        return $this;
174
    }
175
176
    /**
177
     * Check if allOf is available
178
     *
179
     * @return bool
180
     */
181 225
    public function hasAllOf() {
182 225
        return isset($this->field['allOf']);
183
    }
184
185
    /**
186
     * Get allof tree
187
     *
188
     * @return array
189
     */
190 4
    public function getAllOf() {
191 4
        return $this->field['allOf'] ?? [];
192
    }
193
194
    /**
195
     * Get the field type.
196
     *
197
     * @return string|string[] Returns a type string, array of type strings, or null if there isn't one.
198
     */
199 224
    public function getType() {
200 224
        return $this->field['type'] ?? '';
201
    }
202
203
    /**
204
     * Whether or not the field has a given type.
205
     *
206
     * @param string $type The single type to test.
207
     * @return bool Returns **true** if the field has the given type or **false** otherwise.
208
     */
209 32
    public function hasType($type) {
210 32
        return in_array($type, (array)$this->getType());
211
    }
212
213
    /**
214
     * Get a value fom the field.
215
     *
216
     * @param string $key The key to look at.
217
     * @param mixed $default The default to return if the key isn't found.
218
     * @return mixed Returns a value or the default.
219
     */
220 236
    public function val($key, $default = null) {
221 236
        return $this->field[$key] ?? $default;
222
    }
223
224
    /**
225
     * Whether or not the field has a value.
226
     *
227
     * @param string $key The key to look at.
228
     * @return bool Returns **true** if the field has a key or **false** otherwise.
229
     */
230 36
    public function hasVal($key) {
231 36
        return isset($this->field[$key]) || (is_array($this->field) && array_key_exists($key, $this->field));
232
    }
233
234
    /**
235
     * Get the error count for this field.
236
     */
237 64
    public function getErrorCount() {
238 64
        return $this->getValidation()->getErrorCount($this->getName());
239
    }
240
241
    /**
242
     * Whether or not we are validating a request.
243
     *
244
     * @return bool Returns **true** of we are validating a request or **false** otherwise.
245
     */
246 121
    public function isRequest(): bool {
247 121
        return $this->options['request'] ?? false;
248
    }
249
250
    /**
251
     * Whether or not we are validating a response.
252
     *
253
     * @return bool Returns **true** of we are validating a response or **false** otherwise.
254
     */
255 121
    public function isResponse(): bool {
256 121
        return $this->options['response'] ?? false;
257
    }
258
259
    /**
260
     * Whether or not this is a sparse validation..
261
     *
262
     * @return bool Returns **true** if this is a sparse validation or **false** otherwise.
263
     */
264 37
    public function isSparse() {
265 37
        return $this->getOption('sparse', false);
266
    }
267
268
    /**
269
     * Gets the options array.
270
     *
271
     * @return array Returns an options array.
272
     */
273 142
    public function getOptions(): array {
274 142
        return $this->options;
275
    }
276
277
    /**
278
     * Get an indivitual option.
279
     *
280
     * @param string $option The name of the option.
281
     * @param mixed $default The default value to return if the option doesn't exist.
282
     * @return mixed Returns the option or the default value.
283
     */
284 37
    public function getOption(string $option, $default = null) {
285 37
        return $this->options[$option] ?? $default;
286
    }
287
288
    /**
289
     * Get the schemaPath.
290
     *
291
     * @return string Returns the schemaPath.
292
     */
293 236
    public function getSchemaPath(): string {
294 236
        return $this->schemaPath;
295
    }
296
297
    /**
298
     * Set the schemaPath.
299
     *
300
     * @param string $schemaPath
301
     * @return $this
302
     */
303 119
    public function setSchemaPath(string $schemaPath) {
304 119
        $this->schemaPath = ltrim($schemaPath, '/');
305 119
        return $this;
306
    }
307
}
308