-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxConstraint.php
77 lines (66 loc) · 1.41 KB
/
MinMaxConstraint.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
73
74
75
76
77
<?php
namespace Weew\Validator\Constraints;
use Weew\Validator\IConstraint;
use Weew\Validator\IValidationData;
/**
* Check if the value is inside the given range.
*/
class MinMaxConstraint implements IConstraint {
/**
* @var int
*/
protected $min;
/**
* @var int
*/
protected $max;
/**
* @var string
*/
protected $message;
/**
* MinMaxConstraint constructor.
*
* @param int $min
* @param int $max
* @param string $message
*/
public function __construct($min, $max, $message = null) {
$this->min = $min;
$this->max = $max;
$this->message = $message;
}
/**
* @param $value
* @param IValidationData $data
*
* @return bool
*/
public function check($value, IValidationData $data = null) {
if (is_numeric($value)) {
return $value >= $this->min && $value <= $this->max;
}
return false;
}
/**
* @return string
*/
public function getMessage() {
if ($this->message !== null) {
return $this->message;
}
return s(
'Must have a value between "%s" and "%s".',
$this->min, $this->max
);
}
/**
* @return array
*/
public function getOptions() {
return [
'min' => $this->min,
'max' => $this->max,
];
}
}