. */ declare(strict_types=1); namespace FireflyIII\Rules; use FireflyIII\Models\Account; use Illuminate\Contracts\Validation\Rule; use Illuminate\Support\Collection; /** * Class UniqueIban */ class UniqueIban implements Rule { /** @var Account */ private $account; /** * Create a new rule instance. * * @return void */ public function __construct(?Account $account) { $this->account = $account; } /** * Get the validation error message. * * @return string */ public function message() { return trans('validation.unique_iban_for_user'); } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * * @return bool */ public function passes($attribute, $value) { if (!auth()->check()) { return true; // @codeCoverageIgnore } $query = auth()->user()->accounts(); if (!is_null($this->account)) { $query->where('accounts.id', '!=', $this->account->id); } /** @var Collection $accounts */ $accounts = $query->get(); /** @var Account $account */ foreach ($accounts as $account) { if ($account->iban === $value) { return false; } } return true; } }