*/ class FormCardResponse implements Arrayable, JsonSerializable, Responsable { /** * @param array|null $redirect */ public function __construct( protected string $type = 'success', protected ?string $message = null, protected ?array $redirect = null, protected bool $refresh = false, protected int $status = 200, ) { } public static function success(?string $message = null): self { return new self('success', $message); } public static function message(string $message): self { return self::success($message); } public static function error(string $message, int $status = 200): self { return new self('error', $message, status: $status); } public static function redirect(string $url, bool $remote = false): self { return new self('redirect', redirect: [ 'url' => $url, 'remote' => $remote, ]); } /** * @param array $options */ public static function visit(string $path, array $options = []): self { return new self('redirect', redirect: [ 'url' => $path, 'remote' => false, 'options' => $options, ]); } public static function refresh(?string $message = null): self { return new self('refresh', $message, refresh: true); } /** * @param array $payload */ public static function fromArray(array $payload): self { return new self( type: (string) ($payload['type'] ?? 'success'), message: isset($payload['message']) ? (string) $payload['message'] : null, redirect: isset($payload['redirect']) && is_array($payload['redirect']) ? $payload['redirect'] : null, refresh: (bool) ($payload['refresh'] ?? false), status: (int) ($payload['status'] ?? 200), ); } public static function from(mixed $value): self { if ($value instanceof self) { return $value; } if (is_array($value)) { return self::fromArray($value); } if (is_string($value)) { return self::message($value); } if ($value === false) { return self::error(__('nova-form-card::messages.could_not_submit')); } return self::success(); } public function status(): int { return $this->status; } /** * @return array */ public function toArray(): array { return array_filter([ 'type' => $this->type, 'message' => $this->message, 'redirect' => $this->redirect, 'refresh' => $this->refresh, ], static fn (mixed $value): bool => $value !== null && $value !== false); } /** * @return array */ public function jsonSerialize(): array { return $this->toArray(); } public function toResponse($request) { return response()->json($this->toArray(), $this->status); } }