. */ declare(strict_types=1); namespace FireflyIII\Rules; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Contracts\Validation\Rule; /** * Class IsValidAttachmentModel */ class IsValidAttachmentModel implements Rule { /** @var string */ private $model; /** * IsValidAttachmentModel constructor. * * @param string $model */ public function __construct(string $model) { $this->model = $model; } /** * Get the validation error message. * * @return string */ public function message(): string { return (string)trans('validation.model_id_invalid'); } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * * @return bool * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function passes($attribute, $value): bool { if (!auth()->check()) { return false; } if (TransactionJournal::class === $this->model) { $repository = app(JournalRepositoryInterface::class); $user = auth()->user(); $repository->setUser($user); $result = $repository->findNull((int)$value); return null !== $result; } return false; } }