<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;

class ExclusiveWith implements Rule, DataAwareRule
{
    /* Refuses a set value (true or a non-zero number) while another field is set as well. */
    private string $other;

    private array $data = [];

    public function __construct(string $_other)
    {
        $this->other = $_other;
    }

    public function setData(array $data)
    {
        $this->data = $data;

        return $this;
    }

    public function passes($attribute, $value)
    {

        return !(self::isSet($value) && self::isSet($this->data[$this->other] ?? null));

    }

    private static function isSet($_value): bool
    {
        return !in_array($_value, [ null, false, 0, '', '0' ], true);
    }

    public function message()
    {
        return "The :attribute cannot be set together with `$this->other`.";
    }
}
