-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxLengthConstraint.php
79 lines (67 loc) · 1.46 KB
/
MinMaxLengthConstraint.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
78
79
<?php
namespace Weew\Validator\Constraints;
use Weew\Validator\IConstraint;
use Weew\Validator\IValidationData;
/**
* Check if the value's length is in the given range.
*/
class MinMaxLengthConstraint implements IConstraint {
/**
* @var int
*/
protected $min;
/**
* @var int
*/
protected $max;
/**
* @var string
*/
protected $message;
/**
* MinMaxLengthConstraint constructor.
*
* @param $min
* @param $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_string($value)) {
$length = strlen($value);
return $length >= $this->min && $length <= $this->max;
}
return false;
}
/**
* @return string
*/
public function getMessage() {
if ($this->message !== null) {
return $this->message;
}
return s(
'Must be between "%s" and "%s" characters long.',
$this->min, $this->max
);
}
/**
* @return array
*/
public function getOptions() {
return [
'min' => $this->min,
'max' => $this->max,
];
}
}