<?php

namespace App\Rules;

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

class LandlineNumber implements ValidationRule
{
    public function validate(
        string $attribute,
        mixed $value,
        Closure $fail
    ): void {
        if ($value === null || $value === '') {
            return;
        }

        $number = preg_replace('/[\s\-\(\)]/', '', $value);

        $isValid = preg_match(
            '/^(?:\+44|0)(?:1\d{8,9}|2\d{9}|3\d{9})$/',
            $number
        );

        if (! $isValid) {
            $fail(
                'The landline number must be a valid UK landline number.'
            );
        }
    }
}