callback = $callback; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->failed = false; $this->callback->__invoke($attribute, $value, function ($attribute, $message = null) { $this->failed = true; return $this->pendingPotentiallyTranslatedString($attribute, $message); }); return ! $this->failed; } /** * Get the validation error messages. * * @return string */ public function message() { return $this->messages; } /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } /** * Create a pending potentially translated string. * * @param string $attribute * @param ?string $message * @return \Illuminate\Translation\PotentiallyTranslatedString */ protected function pendingPotentiallyTranslatedString($attribute, $message) { $destructor = $message === null ? fn ($message) => $this->messages[] = $message : fn ($message) => $this->messages[$attribute] = $message; return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString { /** * The callback to call when the object destructs. * * @var \Closure */ protected $destructor; /** * Create a new pending potentially translated string. * * @param string $message * @param \Illuminate\Contracts\Translation\Translator $translator * @param \Closure $destructor */ public function __construct($message, $translator, $destructor) { parent::__construct($message, $translator); $this->destructor = $destructor; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { ($this->destructor)($this->toString()); } }; } }