-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathLengthIn.php
More file actions
61 lines (52 loc) · 1.23 KB
/
LengthIn.php
File metadata and controls
61 lines (52 loc) · 1.23 KB
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
<?php
namespace Rakit\Validation\Rules;
use Rakit\Validation\Helper;
use Rakit\Validation\Rule;
/**
* Check if attribute has length in allowed values
*
*
*/
class LengthIn extends Rule
{
/** @var string */
protected $message = "The :attribute must have length only :allowed_values";
/** @var bool */
protected $strict = false;
/**
* Given $params and assign the $this->params
*
* @param array $params
* @return self
*/
public function fillParameters(array $params): Rule
{
if (count($params) == 1 && is_array($params[0])) {
$params = $params[0];
}
$this->params['allowed_values'] = $params;
return $this;
}
/**
* Set strict value
*
* @param bool $strict
* @return void
*/
public function strict(bool $strict = true)
{
$this->strict = $strict;
}
/**
* Check $value is existed
*
* @param mixed $value
* @return bool
*/
public function check($value): bool
{
$this->requireParameters(['allowed_values']);
$allowedValues = $this->parameter('allowed_values');
return in_array( strlen((string)$value), $allowedValues, $this->strict);
}
}