Issues (48)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Schema.php (14 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden;
9
10
11
use Garden\Exception\ValidationException;
12
13
/**
14
 * A class for defining and validating data schemas.
15
 */
16
class Schema implements \JsonSerializable {
17
    /// Properties ///
18
    protected $schema = [];
19
20
    protected static $types = [
21
//        '@' => 'file',
22
        'a' => 'array',
23
        'o' => 'object',
24
        '=' => 'base64',
25
        'i' => 'integer',
26
        's' => 'string',
27
        'f' => 'float',
28
        'b' => 'boolean',
29
        'ts' => 'timestamp',
30
        'dt' => 'datetime'
31
    ];
32
33
    /**
34
     * @var array An array of callbacks that will custom validate the schema.
35
     */
36
    protected $validators = [];
37
38
    /// Methods ///
39
40
    /**
41
     * Initialize an instance of a new {@link Schema} class.
42
     *
43
     * @param array $schema The array schema to validate against.
44
     */
45 74
    public function __construct($schema = []) {
46 74
        $this->schema = static::parseSchema($schema);
47 74
    }
48
49
    /**
50
     * Create a new schema and return it.
51
     *
52
     * @param array $schema The schema array.
53
     * @return Schema Returns the newly created and parsed schema.
54
     */
55 24
    public static function create($schema = []) {
56 24
        $new = new Schema($schema);
57 24
        return $new;
58
    }
59
60
    /**
61
     * Parse a schema in short form into a full schema array.
62
     *
63
     * @param array $arr The array to parse into a schema.
64
     * @return array The full schema array.
65
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
66
     */
67 74
    public static function parseSchema(array $arr) {
68 74
        $result = [];
69
70 74
        foreach ($arr as $key => $value) {
71 74
            if (is_int($key)) {
72 72
                if (is_string($value)) {
73
                    // This is a short param value.
74 72
                    $param = static::parseShortParam($value);
75 72
                    $name = $param['name'];
76 72
                    $result[$name] = $param;
77 72
                } else {
78
                    throw new \InvalidArgumentException("Schema at position $key is not a valid param.", 500);
79
                }
80 72
            } else {
81
                // The parameter is defined in the key.
82 16
                $param = static::parseShortParam($key, $value);
83 16
                $name = $param['name'];
84
85 16
                if (is_array($value)) {
86
                    // The value describes a bit more about the schema.
87 11
                    switch ($param['type']) {
88 11
                        case 'array':
89 4
                            if (isset($value['items'])) {
90
                                // The value includes array schema information.
91 1
                                $param = array_replace($param, $value);
92 4
                            } elseif (isset($value['type'])) {
93
                                // The value is a long-form schema.
94
                                $param['items'] = $value;
95
                            } else {
96
                                // The value is another shorthand schema.
97 3
                                $param['items'] = [
98 3
                                    'type' => 'object',
99 3
                                    'required' => true,
100 3
                                    'properties' => static::parseSchema($value)
101 3
                                ];
102
                            }
103 4
                            break;
104 7
                        case 'object':
105
                            // The value is a schema of the object.
106 5
                            if (isset($value['properties'])) {
107 1
                                $param['properties'] = static::parseSchema($value['properties']);
108 1
                            } else {
109 5
                                $param['properties'] = static::parseSchema($value);
110
                            }
111 5
                            break;
112 3
                        default:
113 3
                            $param = array_replace($param, $value);
114 3
                            break;
115 11
                    }
116 16
                } elseif (is_string($value)) {
117 7
                    if ($param['type'] === 'array') {
118
                        // Check to see if the value is the item type in the array.
119 2
                        if (isset(self::$types[$value])) {
120 2
                            $arrType = self::$types[$value];
121 2
                        } elseif (($index = array_search($value, self::$types)) !== false) {
122
                            $arrType = self::$types[$index];
123
                        }
124
125 2
                        if (isset($arrType)) {
126 2
                            $param['items'] = ['type' => $arrType, 'required' => true];
127 2
                        } else {
128 1
                            $param['description'] = $value;
129
                        }
130 2
                    } else {
131
                        // The value is the schema description.
132 5
                        $param['description'] = $value;
133
                    }
134 7
                }
135
136 16
                $result[$name] = $param;
137
            }
138 74
        }
139
140 74
        return $result;
141
    }
142
143
    /**
144
     * Parse a short parameter string into a full array parameter.
145
     *
146
     * @param string $str The short parameter string to parse.
147
     * @param array $other An array of other information that might help resolve ambiguity.
148
     * @return array Returns an array in the form [name, [param]].
149
     * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format.
150
     */
151 74
    public static function parseShortParam($str, $other = []) {
152
        // Is the parameter optional?
153 74
        if (str_ends($str, '?')) {
154 50
            $required = false;
155 50
            $str = substr($str, 0, -1);
156 50
        } else {
157 37
            $required = true;
158
        }
159
160
        // Check for a type.
161 74
        $parts = explode(':', $str);
162
163 74
        if (count($parts) === 1) {
164 6
            if (isset($other['type'])) {
165 2
                $type = $other['type'];
166 2
            } else {
167 5
                $type = 'string';
168
            }
169 6
            $name = $parts[0];
170 6
        } else {
171 74
            $name = $parts[1];
172
173 74
            if (isset(self::$types[$parts[0]])) {
174 74
                $type = self::$types[$parts[0]];
175 74
            } else {
176
                throw new \InvalidArgumentException("Invalid type {$parts[1]} for field $name.", 500);
177
            }
178
        }
179
180 74
        $result = ['name' => $name, 'type' => $type, 'required' => $required];
181
182 74
        return $result;
183
    }
184
185
    /**
186
     * Add a custom validator to to validate the schema.
187
     *
188
     * @param string $fieldname The name of the field to validate, if any.
189
     * @param callable $callback The callback to validate with.
190
     * @return Schema Returns `$this` for fluent calls.
191
     */
192 2
    public function addValidator($fieldname, callable $callback) {
193 2
        $this->validators[$fieldname][] = $callback;
194 2
        return $this;
195
    }
196
197
198
    /**
199
     * Require one of a given set of fields in the schema.
200
     *
201
     * @param array $fieldnames The field names to require.
202
     * @param int $count The count of required items.
203
     * @return Schema Returns `$this` for fluent calls.
204
     */
205 1
    public function requireOneOf(array $fieldnames, $count = 1) {
206
        $result = $this->addValidator('*', function ($data, Validation $validation) use ($fieldnames, $count) {
207 1
            $hasCount = 0;
208 1
            $flattened = [];
209
210 1
            foreach ($fieldnames as $name) {
211 1
                $flattened = array_merge($flattened, (array)$name);
212
213 1
                if (is_array($name)) {
214
                    // This is an array of required names. They all must match.
215 1
                    $hasCountInner = 0;
216 1
                    foreach ($name as $nameInner) {
217 1
                        if (isset($data[$nameInner]) && $data[$nameInner]) {
218 1
                            $hasCountInner++;
219 1
                        } else {
220 1
                            break;
221
                        }
222 1
                    }
223 1
                    if ($hasCountInner >= count($name)) {
224 1
                        $hasCount++;
225 1
                    }
226 1
                } elseif (isset($data[$name]) && $data[$name]) {
227 1
                    $hasCount++;
228 1
                }
229
230 1
                if ($hasCount >= $count) {
231 1
                    return true;
232
                }
233 1
            }
234
235 1
            $messageFields = array_map(function ($v) {
236 1
                if (is_array($v)) {
237 1
                    return '('.implode(', ', $v).')';
238
                }
239 1
                return $v;
240 1
            }, $fieldnames);
241
242 1
            if ($count === 1) {
243 1
                $message = sprintft('One of %s are required.', implode(', ', $messageFields));
244 1
            } else {
245 1
                $message = sprintft('%1$s of %2$s are required.', $count, implode(', ', $messageFields));
246
            }
247
248 1
            $validation->addError('missing_field', $flattened, [
249
                'message' => $message
250 1
            ]);
251 1
            return false;
252 1
        });
253
254 1
        return $result;
255
    }
256
257
    /**
258
     * Validate data against the schema.
259
     *
260
     * @param array &$data The data to validate.
261
     * @param Validation &$validation This argument will be filled with the validation result.
262
     * @return bool Returns true if the data is valid, false otherwise.
263
     * @throws ValidationException Throws an exception when the data does not validate against the schema.
264
     */
265 14
    public function validate(array &$data, Validation &$validation = null) {
266 14
        if (!$this->isValidInternal($data, $this->schema, $validation, '')) {
267
            if ($validation === null) {
268
                // Although this should never be null, scrutinizer complains that it might be.
269
                $validation = new Validation();
270
            }
271
272
            throw new ValidationException($validation);
273
        }
274 14
        return $this;
275
    }
276
277
    /**
278
     * Validate data against the schema and return the result.
279
     *
280
     * @param array &$data The data to validate.
281
     * @param Validation &$validation This argument will be filled with the validation result.
282
     * @return bool Returns true if the data is valid. False otherwise.
283
     */
284 55
    public function isValid(array &$data, Validation &$validation = null) {
285 55
        return $this->isValidInternal($data, $this->schema, $validation, '');
286
    }
287
288
    /**
289
     * Validate data against the schema and return the result.
290
     *
291
     * @param array &$data The data to validate.
292
     * @param array $schema The schema array to validate against.
293
     * @param Validation &$validation This argument will be filled with the validation result.
294
     * @param string $path The path to the current path for nested objects.
295
     * @return bool Returns true if the data is valid. False otherwise.
296
     */
297 68
    protected function isValidInternal(array &$data, array $schema, Validation &$validation = null, $path = '') {
298 68
        if ($validation === null) {
299 68
            $validation = new Validation();
300 68
        }
301
302
        // Loop through the schema fields and validate each one.
303 68
        foreach ($schema as $name => $field) {
304
            // Prepend the path the field label.
305 68
            if ($path) {
306 4
                $field['path'] = $path.array_select(['path', 'name'], $field);
307 4
            }
308
309 68
            if (array_key_exists($name, $data)) {
310 68
                $this->validateField($data[$name], $field, $validation);
311 68
            } elseif (val('required', $field)) {
312 4
                $validation->addError('missing_field', array_select(['path', 'name'], $field));
313 4
            }
314 68
        }
315
316
        // Validate the global validators.
317 68
        if ($path == '' && isset($this->validators['*'])) {
318 1
            foreach ($this->validators['*'] as $callback) {
319 1
                call_user_func($callback, $data, $validation);
320 1
            }
321 1
        }
322
323 68
        return $validation->isValid();
324
    }
325
326
    /**
327
     * Validate a field.
328
     *
329
     * @param mixed &$value The value to validate.
330
     * @param array $field Parameters on the field.
331
     * @param Validation $validation A validation object to add errors to.
332
     * @throws \InvalidArgumentException Throws an exception when there is something wrong in the {@link $params}.
333
     * @internal param string $fieldname The name of the field to validate.
334
     * @return bool Returns true if the field is valid, false otherwise.
335
     */
336 68
    protected function validateField(&$value, array $field, Validation $validation) {
337 68
        $path = array_select(['path', 'name'], $field);
338 68
        $type = val('type', $field, '');
339 68
        $valid = true;
340
341
        // Check required first.
342
        // A value that isn't passed should fail the required test, but short circuit the other ones.
343 68
        $validRequired = $this->validateRequired($value, $field, $validation);
344 68
        if ($validRequired !== null) {
345 20
            return $validRequired;
346
        }
347
348
        // Validate the field's type.
349 49
        $validType = true;
350
        switch ($type) {
351 49
            case 'boolean':
352 18
                $validType &= $this->validateBoolean($value, $field, $validation);
353 18
                break;
354 34
            case 'integer':
355 11
                $validType &= $this->validateInteger($value, $field, $validation);
356 11
                break;
357 31
            case 'float':
358 6
                $validType &= $this->validateFloat($value, $field, $validation);
359 6
                break;
360 28
            case 'string':
361 9
                $validType &= $this->validateString($value, $field, $validation);
362 9
                break;
363 25
            case 'timestamp':
364 5
                $validType &= $this->validateTimestamp($value, $field, $validation);
365 5
                break;
366 22
            case 'datetime':
367 3
                $validType &= $this->validateDatetime($value, $field, $validation);
368 3
                break;
369 20
            case 'base64':
370 6
                $validType &= $this->validateBase64($value, $field, $validation);
371 6
                break;
372 14
            case 'array':
373 8
                $validType &= $this->validateArray($value, $field, $validation);
374 8
                break;
375 8
            case 'object':
376 8
                $validType &= $this->validateObject($value, $field, $validation);
377 8
                break;
378
            case '':
379
                // No type was specified so we are valid.
380
                $validType = true;
381
                break;
382
            default:
383
                throw new \InvalidArgumentException("Unrecognized type $type.", 500);
384
        }
385 49
        if (!$validType) {
386 31
            $valid = false;
387 31
            $validation->addError(
388 31
                'invalid_type',
389 31
                $path,
390
                [
391 31
                    'type' => $type,
392 31
                    'message' => sprintft('%1$s is not a valid %2$s.', $path, $type),
393
                    'status' => 422
394 31
                ]
395 31
            );
396 31
        }
397
398
        // Validate a custom field validator.
399 49
        $validatorName = val('validatorName', $field, $path);
400 49
        if (isset($this->validators[$validatorName])) {
401 1
            foreach ($this->validators[$validatorName] as $callback) {
402 1
                call_user_func_array($callback, [&$value, $field, $validation]);
403 1
            }
404 1
        }
405
406 49
        return $valid;
407
    }
408
409
    /**
410
     * Validate an array.
411
     *
412
     * @param mixed &$value The value to validate.
413
     * @param array $field The field definition.
414
     * @param Validation $validation The validation results to add.
415
     * @return bool Returns true if {@link $value} is valid or false otherwise.
416
     */
417 8
    protected function validateArray(&$value, array $field, Validation $validation) {
418 8
        $validType = true;
419
420 8
        if (!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) {
421 5
            $validType = false;
422 5
        } else {
423
            // Cast the items into a proper numeric array.
424 4
            $value = array_values($value);
425
426 4
            if (isset($field['items'])) {
427
                // Validate each of the types.
428 3
                $path = array_select(['path', 'name'], $field);
429 3
                $itemField = $field['items'];
430 3
                $itemField['validatorName'] = array_select(['validatorName', 'path', 'name'], $field).'.items';
431 3
                foreach ($value as $i => &$item) {
432 3
                    $itemField['path'] = "$path.$i";
433 3
                    $this->validateField($item, $itemField, $validation);
434 3
                }
435 3
            }
436
        }
437 8
        return $validType;
438
    }
439
440
    /**
441
     * Validate a base64 string.
442
     *
443
     * @param mixed &$value The value to validate.
444
     * @param array $field The field definition.
445
     * @param Validation $validation The validation results to add.
446
     * @return bool Returns true if {@link $value} is valid or false otherwise.
447
     */
448 6
    protected function validateBase64(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
449 6
        if (!is_string($value)) {
450 3
            $validType = false;
451 3
        } else {
452 3
            if (!preg_match('`^[a-zA-Z0-9/+]*={0,2}$`', $value)) {
453 1
                $validType = false;
454 1
            } else {
455 2
                $decoded = @base64_decode($value);
456 2
                if ($decoded === false) {
457
                    $validType = false;
458
                } else {
459 2
                    $value = $decoded;
460 2
                    $validType = true;
461
                }
462
            }
463
        }
464 6
        return $validType;
465
    }
466
467
    /**
468
     * Validate a boolean value.
469
     *
470
     * @param mixed &$value The value to validate.
471
     * @param array $field The field definition.
472
     * @param Validation $validation The validation results to add.
473
     * @return bool Returns true if {@link $value} is valid or false otherwise.
474
     */
475 18
    protected function validateBoolean(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
476 18
        if (is_bool($value)) {
477 4
            $validType = true;
478 4
        } else {
479
            $bools = [
480 14
                '0' => false, 'false' => false, 'no' => false, 'off' => false,
481 14
                '1' => true,  'true'  => true, 'yes' => true,  'on'  => true
482 14
            ];
483 14
            if ((is_string($value) || is_numeric($value)) && isset($bools[$value])) {
484 11
                $value = $bools[$value];
485 11
                $validType = true;
486 11
            } else {
487 3
                $validType = false;
488
            }
489
        }
490 18
        return $validType;
491
    }
492
493
    /**
494
     * Validate a date time.
495
     *
496
     * @param mixed &$value The value to validate.
497
     * @param array $field The field definition.
498
     * @param Validation $validation The validation results to add.
499
     * @return bool Returns true if {@link $value} is valid or false otherwise.
500
     */
501 3
    protected function validateDatetime(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
502 3
        $validType = true;
503 3
        if ($value instanceof \DateTime) {
504 1
            $validType = true;
505 3
        } elseif (is_string($value)) {
506
            try {
507 1
                $dt = new \DateTime($value);
508
                if ($dt) {
509
                    $value = $dt;
510
                } else {
511
                    $validType = false;
512
                }
513 1
            } catch (\Exception $ex) {
514 1
                $validType = false;
515
            }
516 2
        } elseif (is_numeric($value) && $value > 0) {
517
            $value = new \DateTime('@'.(string)round($value));
518
            $validType = true;
519
        } else {
520 1
            $validType = false;
521
        }
522 3
        return $validType;
523
    }
524
525
    /**
526
     * Validate a float.
527
     *
528
     * @param mixed &$value The value to validate.
529
     * @param array $field The field definition.
530
     * @param Validation $validation The validation results to add.
531
     * @return bool Returns true if {@link $value} is valid or false otherwise.
532
     */
533 6 View Code Duplication
    protected function validateFloat(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
534 6
        if (is_float($value)) {
535 1
            $validType = true;
536 6
        } elseif (is_numeric($value)) {
537 2
            $value = (float)$value;
538 2
            $validType = true;
539 2
        } else {
540 3
            $validType = false;
541
        }
542 6
        return $validType;
543
    }
544
545
    /**
546
     * Validate and integer.
547
     *
548
     * @param mixed &$value The value to validate.
549
     * @param array $field The field definition.
550
     * @param Validation $validation The validation results to add.
551
     * @return bool Returns true if {@link $value} is valid or false otherwise.
552
     */
553 11 View Code Duplication
    protected function validateInteger(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
554 11
        if (is_int($value)) {
555 8
            $validType = true;
556 11
        } elseif (is_numeric($value)) {
557 2
            $value = (int)$value;
558 2
            $validType = true;
559 2
        } else {
560 6
            $validType = false;
561
        }
562 11
        return $validType;
563
    }
564
565
    /**
566
     * Validate an object.
567
     *
568
     * @param mixed &$value The value to validate.
569
     * @param array $field The field definition.
570
     * @param Validation $validation The validation results to add.
571
     * @return bool Returns true if {@link $value} is valid or false otherwise.
572
     */
573 8
    protected function validateObject(&$value, array $field, Validation $validation) {
574 8
        if (!is_array($value) || isset($value[0])) {
575 4
            return false;
576 4
        } elseif (isset($field['properties'])) {
577 4
            $path = array_select(['path', 'name'], $field);
578
            // Validate the data against the internal schema.
579 4
            $this->isValidInternal($value, $field['properties'], $validation, $path.'.');
580 4
        }
581 4
        return true;
582
    }
583
584
    /**
585
     * Validate a required field.
586
     *
587
     * @param mixed &$value The field value.
588
     * @param array $field The field definition.
589
     * @param Validation $validation A {@link Validation} object to collect errors.
590
     * @return bool|null Returns one of the following:
591
     * - null: The field is not required.
592
     * - true: The field is required and {@link $value} is not empty.
593
     * - false: The field is required and {@link $value} is empty.
594
     */
595 68
    protected function validateRequired(&$value, array $field, Validation $validation) {
596 68
        $required = val('required', $field, false);
597 68
        $type = $field['type'];
598
599 68
        if ($value === '' || $value === null) {
600 20
            if (!$required) {
601 9
                $value = null;
602 9
                return true;
603
            }
604
605
            switch ($type) {
606 11
                case 'boolean':
607 1
                    $value = false;
608 1
                    return true;
609 10
                case 'string':
610 2
                    if (val('minLength', $field, 1) == 0) {
611 1
                        $value = '';
612 1
                        return true;
613
                    }
614 1
            }
615 9
            $validation->addError('missing_field', $field);
616 9
            return false;
617
        }
618 49
        return null;
619
    }
620
621
    /**
622
     * Validate a string.
623
     *
624
     * @param mixed &$value The value to validate.
625
     * @param array $field The field definition.
626
     * @param Validation $validation The validation results to add.
627
     * @return bool Returns true if {@link $value} is valid or false otherwise.
628
     */
629 9 View Code Duplication
    protected function validateString(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
630 9
        if (is_string($value)) {
631 7
            $validType = true;
632 9
        } elseif (is_numeric($value)) {
633 1
            $value = (string)$value;
634 1
            $validType = true;
635 1
        } else {
636 2
            $validType = false;
637
        }
638 9
        return $validType;
639
    }
640
641
    /**
642
     * Validate a unix timestamp.
643
     *
644
     * @param mixed &$value The value to validate.
645
     * @param array $field The field definition.
646
     * @param Validation $validation The validation results to add.
647
     * @return bool Returns true if {@link $value} is valid or false otherwise.
648
     */
649 5
    protected function validateTimestamp(&$value, array $field, Validation $validation) {
0 ignored issues
show
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
650 5
        $validType = true;
651 5
        if (is_numeric($value)) {
652 1
            $value = (int)$value;
653 5
        } elseif (is_string($value) && $ts = strtotime($value)) {
654 1
            $value = $ts;
655 1
        } else {
656 3
            $validType = false;
657
        }
658 5
        return $validType;
659
    }
660
661
    /**
662
     * Specify data which should be serialized to JSON.
663
     *
664
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
665
     * @return mixed data which can be serialized by <b>json_encode</b>,
666
     * which is a value of any type other than a resource.
667
     */
668 7
    public function jsonSerialize() {
669 7
        return $this->schema;
670
    }
671
}
672