<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class IPv6_CIDR implements Rule
{
	private const MAX=128;

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
		if (str_contains($value,':') && 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_IPV6);
		}
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be a valid IPv6 CIDR range.';
    }
}