This repository has been archived by the owner on May 12, 2018. It is now read-only.
/
Max.php
72 lines (62 loc) · 2 KB
/
Max.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace League\JsonGuard\Constraints;
use League\JsonGuard;
use League\JsonGuard\Assert;
use League\JsonGuard\ValidationError;
class Max implements ParentSchemaAwarePropertyConstraint
{
const KEYWORD = 'maximum';
const EXCLUSIVE_KEYWORD = 'exclusiveMaximum';
/**
* {@inheritdoc}
*/
public static function validate($value, $schema, $parameter, $pointer = null)
{
Assert::type($parameter, 'number', self::KEYWORD, $pointer);
if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) {
return self::validateExclusiveMax($value, $parameter, $pointer);
}
return self::validateMax($value, $parameter, $pointer);
}
/**
* @param mixed $value
* @param mixed $parameter
* @param string|null $pointer
*
* @return \League\JsonGuard\ValidationError|null
*/
public static function validateMax($value, $parameter, $pointer = null)
{
if (!is_numeric($value) ||
JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) {
return null;
}
return new ValidationError(
'Value {value} is not at most {max}',
self::KEYWORD,
$value,
$pointer,
['value' => $value, 'max' => $parameter]
);
}
/**
* @param mixed $value
* @param mixed $parameter
* @param string|null $pointer
*
* @return \League\JsonGuard\ValidationError|null
*/
public static function validateExclusiveMax($value, $parameter, $pointer = null)
{
if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) {
return null;
}
return new ValidationError(
'Value {value} is not less than {exclusive_max}',
self::EXCLUSIVE_KEYWORD,
$value,
$pointer,
['value' => $value, 'exclusive_max' => $parameter]
);
}
}