Completed
Pull Request — master (#4)
by Todd
02:01
created

Schema::validateString()   F

Complexity

Conditions 21
Paths 613

Size

Total Lines 89
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 58
CRAP Score 21.0021

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 58
cts 59
cp 0.9831
rs 2.3015
c 0
b 0
f 0
cc 21
eloc 65
nc 613
nop 2
crap 21.0021

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Schema;
9
10
/**
11
 * A class for defining and validating data schemas.
12
 */
13
class Schema implements \JsonSerializable {
14
    /**
15
     * Trigger a notice when extraneous properties are encountered during validation.
16
     */
17
    const VALIDATE_EXTRA_PROPERTY_NOTICE = 0x1;
18
19
    /**
20
     * Throw a ValidationException when extraneous properties are encountered during validation.
21
     */
22
    const VALIDATE_EXTRA_PROPERTY_EXCEPTION = 0x2;
23
24
    /**
25
     * @var array All the known types.
26
     *
27
     * If this is ever given some sort of public access then remove the static.
28
     */
29
    private static $types = [
30
        'array' => ['a'],
31
        'object' => ['o'],
32
        'integer' => ['i', 'int'],
33
        'string' => ['s', 'str'],
34
        'number' => ['f', 'float'],
35
        'boolean' => ['b', 'bool'],
36
        'timestamp' => ['ts'],
37
        'datetime' => ['dt'],
38
        'null' => ['n']
39
    ];
40
41
    private $schema = [];
42
43
    /**
44
     * @var int A bitwise combination of the various **Schema::FLAG_*** constants.
45
     */
46
    private $flags = 0;
47
48
    /**
49
     * @var array An array of callbacks that will custom validate the schema.
50
     */
51
    private $validators = [];
52
53
    /**
54
     * @var string|Validation The name of the class or an instance that will be cloned.
55
     */
56
    private $validationClass = Validation::class;
57
58
59
    /// Methods ///
60
61
    /**
62
     * Initialize an instance of a new {@link Schema} class.
63
     *
64
     * @param array $schema The array schema to validate against.
65
     */
66 142
    public function __construct($schema = []) {
67 142
        $this->schema = $this->parse($schema);
68 142
    }
69
70
    /**
71
     * Grab the schema's current description.
72
     *
73
     * @return string
74
     */
75 1
    public function getDescription() {
76 1
        return isset($this->schema['description']) ? $this->schema['description'] : '';
77
    }
78
79
    /**
80
     * Set the description for the schema.
81
     *
82
     * @param string $description The new description.
83
     * @throws \InvalidArgumentException Throws an exception when the provided description is not a string.
84
     * @return Schema
85
     */
86 2
    public function setDescription($description) {
87 2
        if (is_string($description)) {
88 1
            $this->schema['description'] = $description;
89
        } else {
90 1
            throw new \InvalidArgumentException("The description is not a valid string.", 500);
91
        }
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * Return the validation flags.
98
     *
99
     * @return int Returns a bitwise combination of flags.
100
     */
101 1
    public function getFlags() {
102 1
        return $this->flags;
103
    }
104
105
    /**
106
     * Set the validation flags.
107
     *
108
     * @param int $flags One or more of the **Schema::FLAG_*** constants.
109
     * @return Schema Returns the current instance for fluent calls.
110
     */
111 8
    public function setFlags($flags) {
112 8
        if (!is_int($flags)) {
113 1
            throw new \InvalidArgumentException('Invalid flags.', 500);
114
        }
115 7
        $this->flags = $flags;
116
117 7
        return $this;
118
    }
119
120
    /**
121
     * Whether or not the schema has a flag (or combination of flags).
122
     *
123
     * @param int $flag One or more of the **Schema::VALIDATE_*** constants.
124
     * @return bool Returns **true** if all of the flags are set or **false** otherwise.
125
     */
126 8
    public function hasFlag($flag) {
127 8
        return ($this->flags & $flag) === $flag;
128
    }
129
130
    /**
131
     * Set a flag.
132
     *
133
     * @param int $flag One or more of the **Schema::VALIDATE_*** constants.
134
     * @param bool $value Either true or false.
135
     * @return $this
136
     */
137 1
    public function setFlag($flag, $value) {
138 1
        if ($value) {
139 1
            $this->flags = $this->flags | $flag;
140
        } else {
141 1
            $this->flags = $this->flags & ~$flag;
142
        }
143 1
        return $this;
144
    }
145
146
    /**
147
     * Merge a schema with this one.
148
     *
149
     * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance.
150
     */
151 2
    public function merge(Schema $schema) {
152
        $fn = function (array &$target, array $source) use (&$fn) {
153 2
            foreach ($source as $key => $val) {
154 2
                if (is_array($val) && array_key_exists($key, $target) && is_array($target[$key])) {
155 2
                    if (isset($val[0]) || isset($target[$key][0])) {
156
                        // This is a numeric array, so just do a merge.
157 1
                        $merged = array_merge($target[$key], $val);
158 1
                        if (is_string($merged[0])) {
159 1
                            $merged = array_keys(array_flip($merged));
160
                        }
161 1
                        $target[$key] = $merged;
162
                    } else {
163 2
                        $target[$key] = $fn($target[$key], $val);
164
                    }
165
                } else {
166 2
                    $target[$key] = $val;
167
                }
168
            }
169
170 2
            return $target;
171 2
        };
172
173 2
        $fn($this->schema, $schema->getSchemaArray());
174 2
    }
175
176
    /**
177
     * Returns the internal schema array.
178
     *
179
     * @return array
180
     * @see Schema::jsonSerialize()
181
     */
182 11
    public function getSchemaArray() {
183 11
        return $this->schema;
184
    }
185
186
    /**
187
     * Parse a schema in short form into a full schema array.
188
     *
189
     * @param array $arr The array to parse into a schema.
190
     * @return array The full schema array.
191
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
192
     */
193 142
    protected function parse(array $arr) {
194 142
        if (empty($arr)) {
195
            // An empty schema validates to anything.
196 6
            return [];
197 137
        } elseif (isset($arr['type'])) {
198
            // This is a long form schema and can be parsed as the root.
199 6
            return $this->parseNode($arr);
200
        } else {
201
            // Check for a root schema.
202 133
            $value = reset($arr);
203 133
            $key = key($arr);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
204 133
            if (is_int($key)) {
205 82
                $key = $value;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
206 82
                $value = null;
207
            }
208 133
            list ($name, $param) = $this->parseShortParam($key, $value);
0 ignored issues
show
Documentation introduced by
$value is of type null|false, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
209 133
            if (empty($name)) {
210 50
                return $this->parseNode($param, $value);
211
            }
212
        }
213
214
        // If we are here then this is n object schema.
215 85
        list($properties, $required) = $this->parseProperties($arr);
216
217
        $result = [
218 85
            'type' => 'object',
219 85
            'properties' => $properties,
220 85
            'required' => $required
221
        ];
222
223 85
        return array_filter($result);
224
    }
225
226
    /**
227
     * Parse a schema node.
228
     *
229
     * @param array $node The node to parse.
230
     * @param mixed $value Additional information from the node.
231
     * @return array Returns a JSON schema compatible node.
232
     */
233 137
    private function parseNode($node, $value = null) {
234 137
        if (is_array($value)) {
235
            // The value describes a bit more about the schema.
236 52
            switch ($node['type']) {
237 52
                case 'array':
238 6
                    if (isset($value['items'])) {
239
                        // The value includes array schema information.
240 1
                        $node = array_replace($node, $value);
241
                    } else {
242 5
                        $node['items'] = $this->parse($value);
243
                    }
244 6
                    break;
245 46
                case 'object':
246
                    // The value is a schema of the object.
247 9
                    if (isset($value['properties'])) {
248 1
                        list($node['properties']) = $this->parseProperties($value['properties']);
249
                    } else {
250 9
                        list($node['properties'], $required) = $this->parseProperties($value);
251 9
                        if (!empty($required)) {
252 9
                            $node['required'] = $required;
253
                        }
254
                    }
255 9
                    break;
256
                default:
257 38
                    $node = array_replace($node, $value);
258 52
                    break;
259
            }
260 104
        } elseif (is_string($value)) {
261 77
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
262 2
                $node['items'] = ['type' => $arrType];
263
            } elseif (!empty($value)) {
264 22
                $node['description'] = $value;
265
            }
266 32
        } elseif ($value === null) {
267
            // Parse child elements.
268 30
            if ($node['type'] === 'array' && isset($node['items'])) {
269
                // The value includes array schema information.
270
                $node['items'] = $this->parse($node['items']);
271 30
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
272 4
                list($node['properties']) = $this->parseProperties($node['properties']);
273
274
            }
275
        }
276
277
278 137
        return $node;
279
    }
280
281
    /**
282
     * Parse the schema for an object's properties.
283
     *
284
     * @param array $arr An object property schema.
285
     * @return array Returns a schema array suitable to be placed in the **properties** key of a schema.
286
     */
287 87
    private function parseProperties(array $arr) {
288 87
        $properties = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
289 87
        $requiredProperties = [];
290 87
        foreach ($arr as $key => $value) {
291
            // Fix a schema specified as just a value.
292 87
            if (is_int($key)) {
293 61
                if (is_string($value)) {
294 61
                    $key = $value;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
295 61
                    $value = '';
296
                } else {
297
                    throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500);
298
                }
299
            }
300
301
            // The parameter is defined in the key.
302 87
            list($name, $param, $required) = $this->parseShortParam($key, $value);
303
304 87
            $node = $this->parseNode($param, $value);
305
306 87
            $properties[$name] = $node;
307 87
            if ($required) {
308 45
                $requiredProperties[] = $name;
309
            }
310
        }
311 87
        return array($properties, $requiredProperties);
312
    }
313
314
    /**
315
     * Parse a short parameter string into a full array parameter.
316
     *
317
     * @param string $key The short parameter string to parse.
318
     * @param array $value An array of other information that might help resolve ambiguity.
319
     * @return array Returns an array in the form `[string name, array param, bool required]`.
320
     * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format.
321
     */
322 135
    public function parseShortParam($key, $value = []) {
323
        // Is the parameter optional?
324 135
        if (substr($key, -1) === '?') {
325 60
            $required = false;
326 60
            $key = substr($key, 0, -1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
327
        } else {
328 93
            $required = true;
329
        }
330
331
        // Check for a type.
332 135
        $parts = explode(':', $key);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
333 135
        $name = $parts[0];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
334 135
        $allowNull = false;
335 135
        if (!empty($parts[1])) {
336 132
            $types = explode('|', $parts[1]);
337 132
            foreach ($types as $alias) {
338 132
                $found = $this->getType($alias);
339 132
                if ($found === null) {
340
                    throw new \InvalidArgumentException("Unknown type '$alias'", 500);
341 132
                } elseif ($found === 'null') {
342 9
                    $allowNull = true;
343
                } else {
344 132
                    $type = $found;
345
                }
346
            }
347
        } else {
348 6
            $type = null;
349
        }
350
351 135
        if ($value instanceof Schema) {
352 2
            if ($type === 'array') {
353 1
                $param = ['type' => $type, 'items' => $value];
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
354
            } else {
355 1
                $param = $value;
356
            }
357 135
        } elseif (isset($value['type'])) {
358 4
            $param = $value;
359
360 4
            if (!empty($type) && $type !== $param['type']) {
361
                throw new \InvalidArgumentException("Type mismatch between $type and {$param['type']} for field $name.", 500);
362
            }
363
        } else {
364 133
            if (empty($type) && !empty($parts[1])) {
365
                throw new \InvalidArgumentException("Invalid type {$parts[1]} for field $name.", 500);
366
            }
367 133
            $param = ['type' => $type];
368
369
            // Parsed required strings have a minimum length of 1.
370 133
            if ($type === 'string' && !empty($name) && $required && (!isset($value['default']) || $value['default'] !== '')) {
371 28
                $param['minLength'] = 1;
372
            }
373
        }
374 135
        if ($allowNull) {
375 9
            $param['allowNull'] = true;
376
        }
377
378 135
        return [$name, $param, $required];
379
    }
380
381
    /**
382
     * Add a custom validator to to validate the schema.
383
     *
384
     * @param string $fieldname The name of the field to validate, if any.
385
     *
386
     * If you are adding a validator to a deeply nested field then separate the path with dots.
387
     * @param callable $callback The callback to validate with.
388
     * @return Schema Returns `$this` for fluent calls.
389
     */
390 2
    public function addValidator($fieldname, callable $callback) {
391 2
        $this->validators[$fieldname][] = $callback;
392 2
        return $this;
393
    }
394
395
    /**
396
     * Require one of a given set of fields in the schema.
397
     *
398
     * @param array $required The field names to require.
399
     * @param string $fieldname The name of the field to attach to.
400
     * @param int $count The count of required items.
401
     * @return Schema Returns `$this` for fluent calls.
402
     */
403 1
    public function requireOneOf(array $required, $fieldname = '', $count = 1) {
404 1
        $result = $this->addValidator(
405
            $fieldname,
406
            function ($data, ValidationField $field) use ($required, $count) {
407 1
                $hasCount = 0;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
408 1
                $flattened = [];
409
410 1
                foreach ($required as $name) {
411 1
                    $flattened = array_merge($flattened, (array)$name);
412
413 1
                    if (is_array($name)) {
414
                        // This is an array of required names. They all must match.
415 1
                        $hasCountInner = 0;
416 1
                        foreach ($name as $nameInner) {
417 1
                            if (isset($data[$nameInner]) && $data[$nameInner]) {
418 1
                                $hasCountInner++;
419
                            } else {
420 1
                                break;
421
                            }
422
                        }
423 1
                        if ($hasCountInner >= count($name)) {
424 1
                            $hasCount++;
425
                        }
426 1
                    } elseif (isset($data[$name]) && $data[$name]) {
427 1
                        $hasCount++;
428
                    }
429
430 1
                    if ($hasCount >= $count) {
431 1
                        return true;
432
                    }
433
                }
434
435 1
                if ($count === 1) {
436 1
                    $message = 'One of {required} are required.';
437
                } else {
438
                    $message = '{count} of {required} are required.';
439
                }
440
441 1
                $field->addError('missingField', [
442 1
                    'messageCode' => $message,
443 1
                    'required' => $required,
444 1
                    'count' => $count
445
                ]);
446 1
                return false;
447 1
            }
448
        );
449
450 1
        return $result;
451
    }
452
453
    /**
454
     * Validate data against the schema.
455
     *
456
     * @param mixed $data The data to validate.
457
     * @param bool $sparse Whether or not this is a sparse validation.
458
     * @return mixed Returns a cleaned version of the data.
459
     * @throws ValidationException Throws an exception when the data does not validate against the schema.
460
     */
461 116
    public function validate($data, $sparse = false) {
462 116
        $field = new ValidationField($this->createValidation(), $this->schema, '');
463
464 116
        $clean = $this->validateField($data, $field, $sparse);
465
466 114
        if (Invalid::isInvalid($clean) && $field->isValid()) {
467
            // This really shouldn't happen, but we want to protect against seeing the invalid object.
468
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
469
        }
470
471 114
        if (!$field->getValidation()->isValid()) {
472 63
            throw new ValidationException($field->getValidation());
473
        }
474
475 70
        return $clean;
476
    }
477
478
    /**
479
     * Validate data against the schema and return the result.
480
     *
481
     * @param mixed $data The data to validate.
482
     * @param bool $sparse Whether or not to do a sparse validation.
483
     * @return bool Returns true if the data is valid. False otherwise.
484
     */
485 33
    public function isValid($data, $sparse = false) {
486
        try {
487 33
            $this->validate($data, $sparse);
488 23
            return true;
489 24
        } catch (ValidationException $ex) {
490 24
            return false;
491
        }
492
    }
493
494
    /**
495
     * Validate a field.
496
     *
497
     * @param mixed $value The value to validate.
498
     * @param ValidationField $field A validation object to add errors to.
499
     * @param bool $sparse Whether or not this is a sparse validation.
500
     * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value
501
     * is completely invalid.
502
     */
503 116
    protected function validateField($value, ValidationField $field, $sparse = false) {
504 116
        $result = $value;
505 116
        if ($field->getField() instanceof Schema) {
506
            try {
507 1
                $result = $field->getField()->validate($value, $sparse);
508 1
            } catch (ValidationException $ex) {
509
                // The validation failed, so merge the validations together.
510 1
                $field->getValidation()->merge($ex->getValidation(), $field->getName());
511
            }
512 116
        } elseif ($value === null && $field->val('allowNull', false)) {
513 9
            $result = $value;
514
        } else {
515
            // Validate the field's type.
516 116
            $type = $field->getType();
517
            switch ($type) {
518 116
                case 'boolean':
519 21
                    $result = $this->validateBoolean($value, $field);
520 21
                    break;
521 103
                case 'integer':
522 22
                    $result = $this->validateInteger($value, $field);
523 22
                    break;
524 101
                case 'number':
525 9
                    $result = $this->validateNumber($value, $field);
526 9
                    break;
527 100
                case 'string':
528 51
                    $result = $this->validateString($value, $field);
529 51
                    break;
530 83
                case 'timestamp':
531 8
                    $result = $this->validateTimestamp($value, $field);
532 8
                    break;
533 82
                case 'datetime':
534 9
                    $result = $this->validateDatetime($value, $field);
535 9
                    break;
536 79
                case 'array':
537 12
                    $result = $this->validateArray($value, $field, $sparse);
538 12
                    break;
539 77
                case 'object':
540 76
                    $result = $this->validateObject($value, $field, $sparse);
541 74
                    break;
542 2
                case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $type of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
543
                    // No type was specified so we are valid.
544 2
                    $result = $value;
545 2
                    break;
546
                default:
547
                    throw new \InvalidArgumentException("Unrecognized type $type.", 500);
548
            }
549 116
            if (Invalid::isValid($result)) {
550 114
                $result = $this->validateEnum($result, $field);
551
            }
552
        }
553
554
        // Validate a custom field validator.
555 116
        if (Invalid::isValid($result)) {
556 114
            $this->callValidators($result, $field);
557
        }
558
559 116
        return $result;
560
    }
561
562
    /**
563
     * Validate an array.
564
     *
565
     * @param mixed $value The value to validate.
566
     * @param ValidationField $field The validation results to add.
567
     * @param bool $sparse Whether or not this is a sparse validation.
568
     * @return array|Invalid Returns an array or invalid if validation fails.
569
     */
570 12
    protected function validateArray($value, ValidationField $field, $sparse = false) {
571 12
        if (!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) {
572 7
            $field->addTypeError('array');
573 7
            return Invalid::value();
574
        } elseif (empty($value)) {
575 1
            return [];
576 6
        } elseif ($field->val('items') !== null) {
577 4
            $result = [];
578
579
            // Validate each of the types.
580 4
            $itemValidation = new ValidationField(
581 4
                $field->getValidation(),
582 4
                $field->val('items'),
583 4
                ''
584
            );
585
586 4
            foreach ($value as $i => &$item) {
587 4
                $itemValidation->setName($field->getName()."[{$i}]");
588 4
                $validItem = $this->validateField($item, $itemValidation, $sparse);
589 4
                if (Invalid::isValid($validItem)) {
590 4
                    $result[] = $validItem;
591
                }
592
            }
593
        } else {
594
            // Cast the items into a proper numeric array.
595 2
            $result = array_values($value);
596
        }
597
598 6
        return empty($result) ? Invalid::value() : $result;
599
    }
600
601
    /**
602
     * Validate a boolean value.
603
     *
604
     * @param mixed $value The value to validate.
605
     * @param ValidationField $field The validation results to add.
606
     * @return bool|Invalid Returns the cleaned value or invalid if validation fails.
607
     */
608 21
    protected function validateBoolean($value, ValidationField $field) {
609 21
        $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
610 21
        if ($value === null) {
611 5
            $field->addTypeError('boolean');
612 5
            return Invalid::value();
613
        }
614 17
        return $value;
615
    }
616
617
    /**
618
     * Validate a date time.
619
     *
620
     * @param mixed $value The value to validate.
621
     * @param ValidationField $field The validation results to add.
622
     * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid.
623
     */
624 13
    protected function validateDatetime($value, ValidationField $field) {
625 13
        if ($value instanceof \DateTimeInterface) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
626
            // do nothing, we're good
627 11
        } elseif (is_string($value) && $value !== '') {
628
            try {
629 7
                $dt = new \DateTimeImmutable($value);
630 5
                if ($dt) {
631 5
                    $value = $dt;
632
                } else {
633
                    $value = null;
634
                }
635 2
            } catch (\Exception $ex) {
636 2
                $value = Invalid::value();
637
            }
638 4
        } elseif (is_int($value) && $value > 0) {
639 1
            $value = new \DateTimeImmutable('@'.(string)round($value));
640
        } else {
641 3
            $value = Invalid::value();
642
        }
643
644 13
        if (Invalid::isInvalid($value)) {
645 5
            $field->addTypeError('datetime');
646
        }
647 13
        return $value;
648
    }
649
650
    /**
651
     * Validate a float.
652
     *
653
     * @param mixed $value The value to validate.
654
     * @param ValidationField $field The validation results to add.
655
     * @return float|Invalid Returns a number or **null** if validation fails.
656
     */
657 9
    protected function validateNumber($value, ValidationField $field) {
658 9
        $result = filter_var($value, FILTER_VALIDATE_FLOAT);
659 9
        if ($result === false) {
660 5
            $field->addTypeError('number');
661 5
            return Invalid::value();
662
        }
663 4
        return $result;
664
    }
665
666
    /**
667
     * Validate and integer.
668
     *
669
     * @param mixed $value The value to validate.
670
     * @param ValidationField $field The validation results to add.
671
     * @return int|Invalid Returns the cleaned value or **null** if validation fails.
672
     */
673 22
    protected function validateInteger($value, ValidationField $field) {
674 22
        $result = filter_var($value, FILTER_VALIDATE_INT);
675
676 22
        if ($result === false) {
677 9
            $field->addTypeError('integer');
678 9
            return Invalid::value();
679
        }
680 16
        return $result;
681
    }
682
683
    /**
684
     * Validate an object.
685
     *
686
     * @param mixed $value The value to validate.
687
     * @param ValidationField $field The validation results to add.
688
     * @param bool $sparse Whether or not this is a sparse validation.
689
     * @return object|Invalid Returns a clean object or **null** if validation fails.
0 ignored issues
show
Documentation introduced by
Should the return type not be Invalid|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
690
     */
691 76
    protected function validateObject($value, ValidationField $field, $sparse = false) {
692 76
        if (!is_array($value) || isset($value[0])) {
693 7
            $field->addTypeError('object');
694 7
            return Invalid::value();
695 76
        } elseif (is_array($field->val('properties'))) {
696
            // Validate the data against the internal schema.
697 75
            $value = $this->validateProperties($value, $field, $sparse);
698
        }
699 74
        return $value;
700
    }
701
702
    /**
703
     * Validate data against the schema and return the result.
704
     *
705
     * @param array $data The data to validate.
706
     * @param ValidationField $field This argument will be filled with the validation result.
707
     * @param bool $sparse Whether or not this is a sparse validation.
708
     * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types.
709
     * or invalid if there are no valid properties.
710
     */
711 75
    protected function validateProperties(array $data, ValidationField $field, $sparse = false) {
712 75
        $properties = $field->val('properties', []);
713 75
        $required = array_flip($field->val('required', []));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
714 75
        $keys = array_keys($data);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
715 75
        $keys = array_combine(array_map('strtolower', $keys), $keys);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
716
717 75
        $propertyField = new ValidationField($field->getValidation(), [], null);
718
719
        // Loop through the schema fields and validate each one.
720 75
        $clean = [];
721 75
        foreach ($properties as $propertyName => $property) {
722
            $propertyField
723 75
                ->setField($property)
724 75
                ->setName(ltrim($field->getName().".$propertyName", '.'));
725
726 75
            $lName = strtolower($propertyName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
727 75
            $isRequired = isset($required[$propertyName]);
728
729
            // First check for required fields.
730 75
            if (!array_key_exists($lName, $keys)) {
731 20
                if ($sparse) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
732
                    // Sparse validation can leave required fields out.
733 20
                } elseif ($propertyField->hasVal('default')) {
734 2
                    $clean[$propertyName] = $propertyField->val('default');
735 18
                } elseif ($isRequired) {
736 6
                    $propertyField->addError('missingField', ['messageCode' => '{field} is required.']);
737
                }
738
            } else {
739 73
                $clean[$propertyName] = $this->validateField($data[$keys[$lName]], $propertyField, $sparse);
740
            }
741
742 75
            unset($keys[$lName]);
743
        }
744
745
        // Look for extraneous properties.
746 75
        if (!empty($keys)) {
747 7
            if ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_NOTICE)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
748 2
                $msg = sprintf("%s has unexpected field(s): %s.", $field->getName() ?: 'value', implode(', ', $keys));
749 2
                trigger_error($msg, E_USER_NOTICE);
750
            }
751
752 5
            if ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_EXCEPTION)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
753 2
                $field->addError('invalid', [
754 2
                    'messageCode' => '{field} has {extra,plural,an unexpected field,unexpected fields}: {extra}.',
755 2
                    'extra' => array_values($keys),
756 2
                    'status' => 422
757
                ]);
758
            }
759
        }
760
761 73
        return $clean;
762
    }
763
764
    /**
765
     * Validate a string.
766
     *
767
     * @param mixed $value The value to validate.
768
     * @param ValidationField $field The validation results to add.
769
     * @return string|Invalid Returns the valid string or **null** if validation fails.
770
     */
771 51
    protected function validateString($value, ValidationField $field) {
772 51
        if (is_string($value) || is_numeric($value)) {
773 48
            $value = $result = (string)$value;
774
        } else {
775 6
            $field->addTypeError('string');
776 6
            return Invalid::value();
777
        }
778
779 48
        $errorCount = $field->getErrorCount();
0 ignored issues
show
Unused Code introduced by
$errorCount is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
780 48
        if (($minLength = $field->val('minLength', 0)) > 0 && mb_strlen($value) < $minLength) {
781 4
            if (!empty($field->getName()) && $minLength === 1) {
782 2
                $field->addError('missingField', ['messageCode' => '{field} is required.', 'status' => 422]);
783
            } else {
784 2
                $field->addError(
785 2
                    'minLength',
786
                    [
787 2
                        'messageCode' => '{field} should be at least {minLength} {minLength,plural,character} long.',
788 2
                        'minLength' => $minLength,
789 2
                        'status' => 422
790
                    ]
791
                );
792
            }
793
        }
794 48
        if (($maxLength = $field->val('maxLength', 0)) > 0 && mb_strlen($value) > $maxLength) {
795 1
            $field->addError(
796 1
                'maxLength',
797
                [
798 1
                    'messageCode' => '{field} is {overflow} {overflow,plural,characters} too long.',
799 1
                    'maxLength' => $maxLength,
800 1
                    'overflow' => mb_strlen($value) - $maxLength,
801 1
                    'status' => 422
802
                ]
803
            );
804
        }
805 48
        if ($pattern = $field->val('pattern')) {
806 4
            $regex = '`'.str_replace('`', preg_quote('`', '`'), $pattern).'`';
807
808 4
            if (!preg_match($regex, $value)) {
809 2
                $field->addError(
810 2
                    'invalid',
811
                    [
812 2
                        'messageCode' => '{field} is in the incorrect format.',
813
                        'status' => 422
814
                    ]
815
                );
816
            }
817
        }
818 48
        if ($format = $field->val('format')) {
819 15
            $type = $format;
820
            switch ($format) {
821 15
                case 'date-time':
822 4
                    $result = $this->validateDatetime($result, $field);
823 4
                    if ($result instanceof \DateTimeInterface) {
824 4
                        $result = $result->format(\DateTime::RFC3339);
825
                    }
826 4
                    break;
827 11
                case 'email':
828 1
                    $result = filter_var($result, FILTER_VALIDATE_EMAIL);
829 1
                    break;
830 10
                case 'ipv4':
831 1
                    $type = 'IPv4 address';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
832 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
833 1
                    break;
834 9
                case 'ipv6':
835 1
                    $type = 'IPv6 address';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
836 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
837 1
                    break;
838 8
                case 'ip':
839 1
                    $type = 'IP address';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
840 1
                    $result = filter_var($result, FILTER_VALIDATE_IP);
841 1
                    break;
842 7
                case 'uri':
843 7
                    $type = 'URI';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
844 7
                    $result = filter_var($result, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_SCHEME_REQUIRED);
845 7
                    break;
846
                default:
847
                    trigger_error("Unrecognized format '$format'.", E_USER_NOTICE);
848
            }
849 15
            if ($result === false) {
850 5
                $field->addTypeError($type);
851
            }
852
        }
853
854 48
        if ($field->isValid()) {
855 40
            return $result;
856
        } else {
857 12
            return Invalid::value();
858
        }
859
    }
860
861
    /**
862
     * Validate a unix timestamp.
863
     *
864
     * @param mixed $value The value to validate.
865
     * @param ValidationField $field The field being validated.
866
     * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate.
867
     */
868 8
    protected function validateTimestamp($value, ValidationField $field) {
869 8
        if (is_numeric($value) && $value > 0) {
870 2
            $result = (int)$value;
871 6
        } elseif (is_string($value) && $ts = strtotime($value)) {
872 1
            $result = $ts;
873
        } else {
874 5
            $field->addTypeError('timestamp');
875 5
            $result = Invalid::value();
876
        }
877 8
        return $result;
878
    }
879
880
    /**
881
     * Validate a null value.
882
     *
883
     * @param mixed $value The value to validate.
884
     * @param ValidationField $field The error collector for the field.
885
     * @return null|Invalid Returns **null** or invalid.
886
     */
887
    protected function validateNull($value, ValidationField $field) {
888
        if ($value === null) {
889
            return null;
890
        }
891
        $field->addError('invalid', ['messageCode' => '{field} should be null.', 'status' => 422]);
892
        return Invalid::value();
893
    }
894
895
    /**
896
     * Validate a value against an enum.
897
     *
898
     * @param mixed $value The value to test.
899
     * @param ValidationField $field The validation object for adding errors.
900
     * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise.
901
     */
902 114
    protected function validateEnum($value, ValidationField $field) {
903 114
        $enum = $field->val('enum');
904 114
        if (empty($enum)) {
905 113
            return $value;
906
        }
907
908 1
        if (!in_array($value, $enum, true)) {
909 1
            $field->addError(
910 1
                'invalid',
911
                [
912 1
                    'messageCode' => '{field} must be one of: {enum}.',
913 1
                    'enum' => $enum,
914 1
                    'status' => 422
915
                ]
916
            );
917 1
            return Invalid::value();
918
        }
919 1
        return $value;
920
    }
921
922
    /**
923
     * Call all of the validators attached to a field.
924
     *
925
     * @param mixed $value The field value being validated.
926
     * @param ValidationField $field The validation object to add errors.
927
     */
928 114
    protected function callValidators($value, ValidationField $field) {
929 114
        $valid = true;
930
931
        // Strip array references in the name except for the last one.
932 114
        $key = preg_replace(['`\[\d+\]$`', '`\[\d+\]`'], ['[]', ''], $field->getName());
933 114
        if (!empty($this->validators[$key])) {
934 2
            foreach ($this->validators[$key] as $validator) {
935 2
                $r = call_user_func($validator, $value, $field);
936
937 2
                if ($r === false || Invalid::isInvalid($r)) {
938 1
                    $valid = false;
939
                }
940
            }
941
        }
942
943
        // Add an error on the field if the validator hasn't done so.
944 114
        if (!$valid && $field->isValid()) {
945
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
946
        }
947 114
    }
948
949
    /**
950
     * Specify data which should be serialized to JSON.
951
     *
952
     * This method specifically returns data compatible with the JSON schema format.
953
     *
954
     * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource.
955
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
956
     * @link http://json-schema.org/
957
     */
958
    public function jsonSerialize() {
959 15
        $fix = function ($schema) use (&$fix) {
960 15
            if ($schema instanceof Schema) {
961 1
                return $schema->jsonSerialize();
962
            }
963
964 15
            if (!empty($schema['type'])) {
965
                // Swap datetime and timestamp to other types with formats.
966 14
                if ($schema['type'] === 'datetime') {
967 3
                    $schema['type'] = 'string';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
968 3
                    $schema['format'] = 'date-time';
969 13
                } elseif ($schema['type'] === 'timestamp') {
970 3
                    $schema['type'] = 'integer';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
971 3
                    $schema['format'] = 'timestamp';
972
                }
973
            }
974
975 15
            if (!empty($schema['items'])) {
976 4
                $schema['items'] = $fix($schema['items']);
977
            }
978 15
            if (!empty($schema['properties'])) {
979 11
                $properties = [];
980 11
                foreach ($schema['properties'] as $key => $property) {
981 11
                    $properties[$key] = $fix($property);
982
                }
983 11
                $schema['properties'] = $properties;
984
            }
985
986 15
            return $schema;
987 15
        };
988
989 15
        $result = $fix($this->schema);
990
991 15
        return $result;
992
    }
993
994
    /**
995
     * Look up a type based on its alias.
996
     *
997
     * @param string $alias The type alias or type name to lookup.
998
     * @return mixed
999
     */
1000 132
    protected function getType($alias) {
1001 132
        if (isset(self::$types[$alias])) {
1002
            return $alias;
1003
        }
1004 132
        foreach (self::$types as $type => $aliases) {
1005 132
            if (in_array($alias, $aliases, true)) {
1006 132
                return $type;
1007
            }
1008
        }
1009 8
        return null;
1010
    }
1011
1012
    /**
1013
     * Get the class that's used to contain validation information.
1014
     *
1015
     * @return Validation|string Returns the validation class.
1016
     */
1017 116
    public function getValidationClass() {
1018 116
        return $this->validationClass;
1019
    }
1020
1021
    /**
1022
     * Set the class that's used to contain validation information.
1023
     *
1024
     * @param Validation|string $class Either the name of a class or a class that will be cloned.
1025
     * @return $this
1026
     */
1027 1
    public function setValidationClass($class) {
1028 1
        if (!is_a($class, Validation::class, true)) {
1029
            throw new \InvalidArgumentException("$class must be a subclass of ".Validation::class, 500);
1030
        }
1031
1032 1
        $this->validationClass = $class;
1033 1
        return $this;
1034
    }
1035
1036
    /**
1037
     * Create a new validation instance.
1038
     *
1039
     * @return Validation Returns a validation object.
1040
     */
1041 116
    protected function createValidation() {
1042 116
        $class = $this->getValidationClass();
1043
1044 116
        if ($class instanceof Validation) {
1045 1
            $result = clone $class;
1046
        } else {
1047 116
            $result = new $class;
1048
        }
1049 116
        return $result;
1050
    }
1051
}
1052