<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class LocationCoordinates implements Rule
{
    /**
     * Validate an "x,y" coordinate string where x and y are integers.
     */
    public function passes($attribute, $value): bool
    {
        if (! is_string($value)) {
            return false;
        }

        $parts = array_map('trim', explode(',', $value));

        if (count($parts) !== 2) {
            return false;
        }

        foreach ($parts as $part) {
            if (! preg_match('/^-?\d+$/', $part)) {
                return false;
            }
        }

        return true;
    }

    public function message(): string
    {
        return 'The :attribute must be in "<x>,<y>" format using integers.';
    }
}
