Passed
Pull Request — master (#53)
by Todd
02:43
created

ValidationField::escapeRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 199
    public function __construct(Validation $validation, $field, string $name, string $schemaPath, array $options = []) {
53 199
        $this->field = $field;
54 199
        $this->validation = $validation;
55 199
        $this->name = $name;
56 199
        $this->schemaPath = $schemaPath;
57 199
        $this->options = $options + ['sparse' => false];
58 199
    }
59
60
    /**
61
     * Add a validation error.
62
     *
63
     * @param string $error The message code.
64
     * @param int|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 31
    public function addError($error, $options = []) {
69 31
        $this->validation->addError($this->getName(), $error, $options);
70 31
        return $this;
71
    }
72
73
    /**
74
     * Add an invalid type error.
75
     *
76
     * @param string $type The type that was checked.
77
     * @return $this
78
     */
79 45
    public function addTypeError($type = '') {
80 45
        $type = $type ?: $this->getType();
81
82 45
        $this->validation->addError(
83 45
            $this->getName(),
84 45
            'invalid',
85
            [
86 45
                'type' => $type,
87 45
                'messageCode' => '{field} is not a valid {type}.',
88 45
                'status' => 422
89
            ]
90
        );
91
92 45
        return $this;
93
    }
94
95
    /**
96
     * Check whether or not this field is has errors.
97
     *
98
     * @return bool Returns true if the field has no errors, false otherwise.
99
     */
100 76
    public function isValid() {
101 76
        return $this->getValidation()->isValidField($this->getName());
102
    }
103
104
    /**
105
     * Merge a validation object to this one.
106
     *
107
     * @param Validation $validation The validation object to merge.
108
     * @return $this
109
     */
110
    public function merge(Validation $validation) {
111
        $this->getValidation()->merge($validation, $this->getName());
112
        return $this;
113
    }
114
115
    /**
116
     * Get the field.
117
     *
118
     * @return array|Schema Returns the field.
119
     */
120 199
    public function getField() {
121 199
        return $this->field;
122
    }
123
124
    /**
125
     * Set the field.
126
     *
127
     * This method is only meant to be called from within the schema class.
128
     *
129
     * @param array|Schema $field The new field.
130
     * @return $this
131
     */
132 102
    public function setField($field) {
133 102
        $this->field = $field;
134 102
        return $this;
135
    }
136
137
    /**
138
     * Get the validation.
139
     *
140
     * @return Validation Returns the validation.
141
     */
142 199
    public function getValidation() {
143 199
        return $this->validation;
144
    }
145
146
    /**
147
     * Get the name.
148
     *
149
     * @return string Returns the name.
150
     */
151 160
    public function getName() {
152 160
        return $this->name;
153
    }
154
155
    /**
156
     * Set the name.
157
     *
158
     * This method is only meant to be called from within the schema class.
159
     *
160
     * @param string $name The new name.
161
     * @return $this
162
     */
163 115
    public function setName($name) {
164 115
        $this->name = ltrim($name, '/');
165 115
        return $this;
166
    }
167
168
    /**
169
     * Get the field type.
170
     *
171
     * @return string|string[]|null Returns a type string, array of type strings, or null if there isn't one.
172
     */
173 199
    public function getType() {
174 199
        return $this->field['type'] ?? null;
175
    }
176
177
    /**
178
     * Whether or not the field has a given type.
179
     *
180
     * @param string $type The single type to test.
181
     * @return bool Returns **true** if the field has the given type or **false** otherwise.
182
     */
183 31
    public function hasType($type) {
184 31
        return in_array($type, (array)$this->getType());
185
    }
186
187
    /**
188
     * Get a value fom the field.
189
     *
190
     * @param string $key The key to look at.
191
     * @param mixed $default The default to return if the key isn't found.
192
     * @return mixed Returns a value or the default.
193
     */
194 199
    public function val($key, $default = null) {
195 199
        return $this->field[$key] ?? $default;
196
    }
197
198
    /**
199
     * Whether or not the field has a value.
200
     *
201
     * @param string $key The key to look at.
202
     * @return bool Returns **true** if the field has a key or **false** otherwise.
203
     */
204 27
    public function hasVal($key) {
205 27
        return array_key_exists($key, $this->field);
0 ignored issues
show
Bug introduced by
It seems like $this->field can also be of type Garden\Schema\Schema; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

205
        return array_key_exists($key, /** @scrutinizer ignore-type */ $this->field);
Loading history...
206
    }
207
208
    /**
209
     * Get the error count for this field.
210
     */
211 51
    public function getErrorCount() {
212 51
        return $this->getValidation()->getErrorCount($this->getName());
213
    }
214
215
    /**
216
     * Whether or not we are validating a request.
217
     *
218
     * @return bool Returns **true** of we are validating a request or **false** otherwise.
219
     */
220 103
    public function isRequest(): bool {
221 103
        return $this->options['request'] ?? false;
222
    }
223
224
    /**
225
     * Whether or not we are validating a response.
226
     *
227
     * @return bool Returns **true** of we are validating a response or **false** otherwise.
228
     */
229 103
    public function isResponse(): bool {
230 103
        return $this->options['response'] ?? false;
231
    }
232
233
    /**
234
     * Whether or not this is a sparse validation..
235
     *
236
     * @return bool Returns **true** if this is a sparse validation or **false** otherwise.
237
     */
238 28
    public function isSparse() {
239 28
        return $this->getOption('sparse', false);
240
    }
241
242
    /**
243
     * Gets the options array.
244
     *
245
     * @return array Returns an options array.
246
     */
247 119
    public function getOptions(): array {
248 119
        return $this->options;
249
    }
250
251
    /**
252
     * Get an indivitual option.
253
     *
254
     * @param string $option The name of the option.
255
     * @param mixed $default The default value to return if the option doesn't exist.
256
     * @return mixed Returns the option or the default value.
257
     */
258 28
    public function getOption(string $option, $default = null) {
259 28
        return $this->options[$option] ?? $default;
260
    }
261
262
    /**
263
     * Get the schemaPath.
264
     *
265
     * @return string Returns the schemaPath.
266
     */
267 199
    public function getSchemaPath(): string {
268 199
        return $this->schemaPath;
269
    }
270
271
    /**
272
     * Set the schemaPath.
273
     *
274
     * @param string $schemaPath
275
     * @return $this
276
     */
277 102
    public function setSchemaPath(string $schemaPath) {
278 102
        $this->schemaPath = ltrim($schemaPath, '/');
279 102
        return $this;
280
    }
281
282
    /**
283
     * Escape a JSON reference field.
284
     *
285
     * @param string $ref The reference to escape.
286
     * @return string Returns an escaped reference.
287
     */
288 102
    public static function escapeRef(string $ref): string {
289 102
        return str_replace(['~', '/'], ['~0', '~1'], $ref);
290
    }
291
}
292