code = $code; $this->message = $message; $this->context = $context; } /** * Create an OptInError from an error code with a default message. * * @param string $code The error code. * @param array $context Additional context data. * * @return self */ public static function fromCode( string $code, array $context = array() ): self { $messages = self::getDefaultMessages(); $message = $messages[ $code ] ?? __( 'An unknown error occurred.', 'double-opt-in' ); return new self( $code, $message, $context ); } /** * Get the error code. * * @return string */ public function getCode(): string { return $this->code; } /** * Get the human-readable error message. * * @return string */ public function getMessage(): string { return $this->message; } /** * Get additional context data. * * @return array */ public function getContext(): array { return $this->context; } /** * Get the default translatable messages for all error codes. * * @return array */ private static function getDefaultMessages(): array { return array( self::SUBMISSION_CANCELLED => __( 'The form submission has been cancelled.', 'double-opt-in' ), self::NO_RECIPIENT => __( 'No valid email address was found.', 'double-opt-in' ), self::RATE_LIMIT_IP => __( 'Too many requests. Please try again later.', 'double-opt-in' ), self::RATE_LIMIT_EMAIL => __( 'Too many requests for this email address. Please try again later.', 'double-opt-in' ), self::RECIPIENT_INVALID => __( 'The email address could not be verified.', 'double-opt-in' ), self::UNIQUE_EMAIL_DUPLICATE => __( 'This email address has already been used.', 'double-opt-in' ), self::SAVE_FAILED => __( 'An error occurred. Please try again.', 'double-opt-in' ), self::CONSENT_NOT_GIVEN => __( 'You must agree to the consent statement to continue.', 'double-opt-in' ), ); } }