This commit is contained in:
Meghdad committed 2026-07-10 04:20:13 +03:30
commit 8fe27cfa5e
52 files changed
+5299

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
<?php
namespace MeghdadFadaee\NovaFormCard\Http\Responses;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use JsonSerializable;
/**
* @implements Arrayable<string, mixed>
*/
class FormCardResponse implements Arrayable, JsonSerializable, Responsable
{
/**
* @param array<string, mixed>|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<string, mixed> $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<string, mixed> $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<string, mixed>
*/
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<string, mixed>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
public function toResponse($request)
{
return response()->json($this->toArray(), $this->status);
}
}