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
| <?php
namespace MyApp\Rules;
use Illuminate\Contracts\Validation\Rule;
/**
* Verify that the size (length) is one of the given sizes.
*
* Usage example:
* $rules['serialnumber'] = 'sizeoneof:8,14,20';
*
* @author Christian Weiske <weiske@mogic.com>
*/
class SizeOneOf implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute Attribute name
* @param mixed $value Attribute value
* @param array $allowedSizes Array of sizes/lengths
* @param object $validator Validator object with all form data
*
* @return bool If it validates
*/
public function passes(
$attribute, $value, $allowedSizes = [], $validator = null
) {
if (!count($allowedSizes)) {
throw new \Exception(
'sizeoneof: No sizes defined'
);
}
if (is_array($value)) {
$size = count($value);
} else {
$size = strlen($value);
}
return in_array($size, $allowedSizes);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.sizeoneof');
}
/**
* Replace the ":values" parameter in the message
*
* @param string $message Translated message with parameter
* @param string $attribute Attribute name
* @param string $ruleName Name of rule
* @param array $parameters Parameters for rule
*
* @return string Message with replaced other and max
*/
public function replace($message, $attribute, $ruleName, $parameters)
{
return str_replace(
':values', implode(', ', $parameters), $message
);
}
}
|