Ctrl K
ComunidadGuías…

app/Rules/ValidServerIp.php

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ValidServerIp implements ValidationRule
{
    /**
     * Accepts a valid IPv4 address, IPv6 address (optionally in brackets), or RFC 1123 hostname.
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (empty($value)) {
            return;
        }

        $trimmed = trim($value);

        if (filter_var($trimmed, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            return;
        }

        if (filter_var($trimmed, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            return;
        }

        // A bracketed IPv6 address ([2a01:4f8::1]); it is stored without brackets.
        if (str_starts_with($trimmed, '[') && str_ends_with($trimmed, ']')
            && filter_var(substr($trimmed, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            return;
        }

        // Delegate hostname validation to ValidHostname
        $hostnameRule = new ValidHostname;
        $failed = false;
        $hostnameRule->validate($attribute, $trimmed, function () use (&$failed) {
            $failed = true;
        });

        if ($failed) {
            $fail('The :attribute must be a valid IPv4 address, IPv6 address, or hostname.');
        }
    }
}