44 lines
833 B
PHP
44 lines
833 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Rules;
|
||
|
|
||
|
use Illuminate\Contracts\Validation\Rule;
|
||
|
|
||
|
class IPv4_CIDR implements Rule
|
||
|
{
|
||
|
private const MAX=32;
|
||
|
|
||
|
/**
|
||
|
* Determine if the validation rule passes.
|
||
|
*
|
||
|
* @param string $attribute
|
||
|
* @param mixed $value
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function passes($attribute, $value)
|
||
|
{
|
||
|
if (str_contains($value,'/')) {
|
||
|
list($value,$mask) = explode('/',$value);
|
||
|
|
||
|
$m = filter_var($mask,\FILTER_VALIDATE_INT,[
|
||
|
'options' => [
|
||
|
'min_range' => 0,
|
||
|
"max_range" => self::MAX,
|
||
|
]]
|
||
|
);
|
||
|
|
||
|
return $m && filter_var($value,\FILTER_VALIDATE_IP,\FILTER_FLAG_IPV4);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the validation error message.
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function message()
|
||
|
{
|
||
|
return 'The :attribute must be a valid IPv4 CIDR range.';
|
||
|
}
|
||
|
}
|