<?php

namespace App\Rules;

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

class CommaSeparatedIntegers implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        // Remove any leading/trailing whitespace
        $input = trim($value);

        // Check if the input matches the format "int,int,int..." or "int"
        if (! preg_match('/^(\d+,)*\d+$/', $input)) {
            $fail(__('Integers should be comma seperated'));
        }
    }
}
