init
This commit is contained in:
52 files changed
+5299
No files matched your search
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidFormCardToken extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
use MeghdadFadaee\NovaFormCard\Support\SupportedField;
|
||||
|
||||
class UnsupportedFieldException extends RuntimeException
|
||||
{
|
||||
public static function forField(object $field): self
|
||||
{
|
||||
return new self(__('nova-form-card::messages.unsupported_field', [
|
||||
'field' => $field::class,
|
||||
'supported' => implode(', ', SupportedField::names()),
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard;
|
||||
|
||||
use Laravel\Nova\Card;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use MeghdadFadaee\NovaFormCard\Http\Responses\FormCardResponse;
|
||||
use MeghdadFadaee\NovaFormCard\Support\FormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\Support\NovaFieldSerializer;
|
||||
|
||||
abstract class FormCard extends Card
|
||||
{
|
||||
protected ?string $submitButtonText = null;
|
||||
|
||||
public function component(): string
|
||||
{
|
||||
return 'nova-form-card';
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return __(Str::headline(class_basename(static::class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Nova fields displayed by the card.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Fields\Field>
|
||||
*/
|
||||
public function fields(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
abstract public function handle(FormFields $fields, NovaRequest $request): mixed;
|
||||
|
||||
public function authorizedToSubmit(NovaRequest $request): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function submitButtonText(): string
|
||||
{
|
||||
return $this->submitButtonText ?? __('nova-form-card::messages.submit');
|
||||
}
|
||||
|
||||
public function withSubmitButtonText(string $text): static
|
||||
{
|
||||
$this->submitButtonText = $text;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$request = $this->currentNovaRequest();
|
||||
|
||||
app()->instance(NovaRequest::class, $request);
|
||||
|
||||
$this->withMeta([
|
||||
'formCard' => $this->serializeForFormCard($request),
|
||||
]);
|
||||
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function serializeForFormCard(NovaRequest $request): array
|
||||
{
|
||||
$prefix = trim((string) config('nova-form-card.route_prefix', 'nova-vendor/nova-form-card'), '/');
|
||||
$token = app(FormCardToken::class)->issue($this, $request);
|
||||
|
||||
return [
|
||||
'title' => $this->title(),
|
||||
'fields' => app(NovaFieldSerializer::class)->serialize($this->fields($request), $request),
|
||||
'endpoint' => "/{$prefix}/submit",
|
||||
'syncEndpoint' => "/{$prefix}/sync",
|
||||
'submitText' => $this->submitButtonText(),
|
||||
'canSubmit' => $this->authorizedToSubmit($request),
|
||||
'token' => $token,
|
||||
];
|
||||
}
|
||||
|
||||
protected function currentNovaRequest(): NovaRequest
|
||||
{
|
||||
if (app()->bound(NovaRequest::class) && app(NovaRequest::class) instanceof NovaRequest) {
|
||||
return app(NovaRequest::class);
|
||||
}
|
||||
|
||||
$request = request();
|
||||
|
||||
if ($request instanceof NovaRequest) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
return NovaRequest::createFrom($request);
|
||||
}
|
||||
|
||||
public static function message(string $message): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::message($message);
|
||||
}
|
||||
|
||||
public static function success(?string $message = null): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::success($message);
|
||||
}
|
||||
|
||||
public static function error(string $message): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::error($message);
|
||||
}
|
||||
|
||||
public static function danger(string $message): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::error($message);
|
||||
}
|
||||
|
||||
public static function redirect(string $url, bool $remote = false): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::redirect($url, $remote);
|
||||
}
|
||||
|
||||
public static function visit(string $path, array $options = []): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::visit($path, $options);
|
||||
}
|
||||
|
||||
public static function refresh(?string $message = null): FormCardResponse
|
||||
{
|
||||
return FormCardResponse::refresh($message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Nova\Events\ServingNova;
|
||||
use Laravel\Nova\Nova;
|
||||
|
||||
class FormCardServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__.'/../config/nova-form-card.php', 'nova-form-card');
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'nova-form-card');
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->publishes([
|
||||
__DIR__.'/../config/nova-form-card.php' => config_path('nova-form-card.php'),
|
||||
], 'nova-form-card-config');
|
||||
|
||||
$this->publishes([
|
||||
__DIR__.'/../resources/lang' => lang_path('vendor/nova-form-card'),
|
||||
], 'nova-form-card-translations');
|
||||
}
|
||||
|
||||
$this->app->booted(function (): void {
|
||||
$this->routes();
|
||||
});
|
||||
|
||||
Nova::serving(function (ServingNova $event): void {
|
||||
Nova::script('nova-form-card', __DIR__.'/../dist/js/card.js');
|
||||
Nova::style('nova-form-card', __DIR__.'/../dist/css/card.css');
|
||||
Nova::translations($this->frontendTranslationsPath());
|
||||
});
|
||||
}
|
||||
|
||||
protected function frontendTranslationsPath(): string
|
||||
{
|
||||
$locale = (string) $this->app->getLocale();
|
||||
|
||||
$paths = [
|
||||
lang_path("vendor/nova-form-card/{$locale}/card.json"),
|
||||
__DIR__."/../resources/lang/{$locale}/card.json",
|
||||
__DIR__.'/../resources/lang/en/card.json',
|
||||
];
|
||||
|
||||
foreach ($paths as $path) {
|
||||
if (is_file($path)) {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
return __DIR__.'/../resources/lang/en/card.json';
|
||||
}
|
||||
|
||||
protected function routes(): void
|
||||
{
|
||||
if ($this->app->routesAreCached()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Route::middleware(['nova'])
|
||||
->prefix(trim((string) config('nova-form-card.route_prefix', 'nova-vendor/nova-form-card'), '/'))
|
||||
->group(__DIR__.'/../routes/api.php');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard;
|
||||
|
||||
use Countable;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Nova\Fields\ActionFields;
|
||||
|
||||
/**
|
||||
* Nova-compatible container for values resolved from a submitted form card.
|
||||
*/
|
||||
class FormFields extends ActionFields implements Countable
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed>|\Illuminate\Support\Collection<string, mixed> $values
|
||||
*/
|
||||
public function __construct(array|Collection $values = [], ?Collection $callbacks = null)
|
||||
{
|
||||
parent::__construct(collect($values), $callbacks ?? collect());
|
||||
}
|
||||
|
||||
public function get($key, $default = null): mixed
|
||||
{
|
||||
if (! is_string($key)) {
|
||||
return parent::get($key, $default);
|
||||
}
|
||||
|
||||
$attributes = $this->getAttributes();
|
||||
|
||||
if (array_key_exists($key, $attributes)) {
|
||||
return $attributes[$key];
|
||||
}
|
||||
|
||||
$snake = Str::snake($key);
|
||||
|
||||
if (array_key_exists($snake, $attributes)) {
|
||||
return $attributes[$snake];
|
||||
}
|
||||
|
||||
return value($default);
|
||||
}
|
||||
|
||||
public function has($key): bool
|
||||
{
|
||||
if (! is_string($key)) {
|
||||
return parent::has($key);
|
||||
}
|
||||
|
||||
$attributes = $this->getAttributes();
|
||||
|
||||
return array_key_exists($key, $attributes)
|
||||
|| array_key_exists(Str::snake($key), $attributes);
|
||||
}
|
||||
|
||||
public function __get($key): mixed
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
public function __isset($key): bool
|
||||
{
|
||||
return $this->has($key);
|
||||
}
|
||||
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return is_string($offset) && $this->has($offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset): mixed
|
||||
{
|
||||
return is_string($offset) ? $this->get($offset) : null;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->getAttributes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Http\Controllers;
|
||||
|
||||
use Illuminate\Contracts\Support\Responsable;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use MeghdadFadaee\NovaFormCard\Exceptions\InvalidFormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\FormCard;
|
||||
use MeghdadFadaee\NovaFormCard\Http\Responses\FormCardResponse;
|
||||
use MeghdadFadaee\NovaFormCard\Support\FormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\Support\NovaFieldValidator;
|
||||
|
||||
class SubmitFormCardController
|
||||
{
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
FormCardToken $tokens,
|
||||
NovaFieldValidator $fields,
|
||||
): Response {
|
||||
$novaRequest = $this->novaRequest($request);
|
||||
|
||||
app()->instance(NovaRequest::class, $novaRequest);
|
||||
|
||||
$token = (string) $request->input('token', '');
|
||||
|
||||
try {
|
||||
$cardClass = $tokens->resolve($token, $novaRequest);
|
||||
} catch (InvalidFormCardToken) {
|
||||
abort(403, __('nova-form-card::messages.cannot_submit'));
|
||||
}
|
||||
|
||||
$card = app($cardClass);
|
||||
|
||||
if (! $card instanceof FormCard) {
|
||||
abort(403, __('nova-form-card::messages.cannot_submit'));
|
||||
}
|
||||
|
||||
if (! $card->authorizedToSubmit($novaRequest)) {
|
||||
abort(403, __('nova-form-card::messages.not_authorized_to_submit'));
|
||||
}
|
||||
|
||||
$payload = $this->fieldsPayload($request);
|
||||
|
||||
if ($payload === null) {
|
||||
return response()->json([
|
||||
'message' => __('nova-form-card::messages.fields_payload_must_be_object'),
|
||||
'errors' => [
|
||||
'fields' => [__('nova-form-card::messages.fields_payload_must_be_object')],
|
||||
],
|
||||
], 422);
|
||||
}
|
||||
|
||||
$submissionRequest = $fields->requestForPayload($novaRequest, $payload);
|
||||
|
||||
app()->instance(NovaRequest::class, $submissionRequest);
|
||||
|
||||
$cardFields = $card->fields($submissionRequest);
|
||||
|
||||
$validator = Validator::make(
|
||||
$payload,
|
||||
$fields->rules($cardFields, $submissionRequest),
|
||||
[],
|
||||
$fields->attributes($cardFields, $submissionRequest),
|
||||
);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'message' => __('nova-form-card::messages.invalid_data'),
|
||||
'errors' => $validator->errors()->toArray(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
$result = $card->handle(
|
||||
$fields->resolve($cardFields, $payload, $submissionRequest),
|
||||
$submissionRequest,
|
||||
);
|
||||
|
||||
if ($result instanceof Responsable) {
|
||||
return $result->toResponse($request);
|
||||
}
|
||||
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$response = FormCardResponse::from($result);
|
||||
|
||||
return response()->json($response->toArray(), $response->status());
|
||||
}
|
||||
|
||||
protected function novaRequest(Request $request): NovaRequest
|
||||
{
|
||||
if ($request instanceof NovaRequest) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
return NovaRequest::createFrom($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
protected function fieldsPayload(Request $request): ?array
|
||||
{
|
||||
$input = $request->input('fields', []);
|
||||
|
||||
if (! is_array($input)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$files = $request->file('fields', []);
|
||||
|
||||
if (! is_array($files)) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
return array_replace_recursive($input, $files);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Laravel\Nova\Fields\FieldCollection;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use MeghdadFadaee\NovaFormCard\Exceptions\InvalidFormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\FormCard;
|
||||
use MeghdadFadaee\NovaFormCard\Support\FormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\Support\NovaFieldSerializer;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SyncFormCardFieldController
|
||||
{
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
FormCardToken $tokens,
|
||||
NovaFieldSerializer $serializer,
|
||||
): JsonResponse {
|
||||
$novaRequest = $this->novaRequest($request);
|
||||
|
||||
app()->instance('request', $novaRequest);
|
||||
app()->instance(NovaRequest::class, $novaRequest);
|
||||
|
||||
try {
|
||||
$cardClass = $tokens->resolve((string) $request->query('token', ''), $novaRequest);
|
||||
} catch (InvalidFormCardToken) {
|
||||
abort(Response::HTTP_FORBIDDEN, __('nova-form-card::messages.cannot_submit'));
|
||||
}
|
||||
|
||||
$card = app($cardClass);
|
||||
|
||||
if (! $card instanceof FormCard || ! $card->authorizedToSubmit($novaRequest)) {
|
||||
abort(Response::HTTP_FORBIDDEN, __('nova-form-card::messages.not_authorized_to_submit'));
|
||||
}
|
||||
|
||||
$field = FieldCollection::make($card->fields($novaRequest))
|
||||
->authorized($novaRequest)
|
||||
->first(static function (object $field) use ($novaRequest): bool {
|
||||
return $novaRequest->query('field') === ($field->attribute ?? null)
|
||||
&& $novaRequest->query('component') === $field->dependentComponentKey();
|
||||
});
|
||||
|
||||
abort_if($field === null, Response::HTTP_NOT_FOUND);
|
||||
|
||||
$field->syncDependsOn($novaRequest);
|
||||
|
||||
return response()->json($serializer->serializeField($field, $novaRequest));
|
||||
}
|
||||
|
||||
protected function novaRequest(Request $request): NovaRequest
|
||||
{
|
||||
if ($request instanceof NovaRequest) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$novaRequest = NovaRequest::createFrom($request);
|
||||
$novaRequest->setUserResolver($request->getUserResolver());
|
||||
|
||||
return $novaRequest;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Support;
|
||||
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use JsonException;
|
||||
use RuntimeException;
|
||||
use MeghdadFadaee\NovaFormCard\Exceptions\InvalidFormCardToken;
|
||||
use MeghdadFadaee\NovaFormCard\FormCard;
|
||||
|
||||
class FormCardToken
|
||||
{
|
||||
public function issue(FormCard $card, NovaRequest $request): string
|
||||
{
|
||||
$payload = $this->encode([
|
||||
'class' => $card::class,
|
||||
'user_id' => $this->userIdentifier($request),
|
||||
'expires_at' => now()
|
||||
->addMinutes((int) config('nova-form-card.token_ttl_minutes', 120))
|
||||
->getTimestamp(),
|
||||
]);
|
||||
|
||||
return $payload.'.'.$this->signature($payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string<FormCard>
|
||||
*/
|
||||
public function resolve(string $token, NovaRequest $request): string
|
||||
{
|
||||
if ($token === '' || ! str_contains($token, '.')) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.missing'));
|
||||
}
|
||||
|
||||
[$payload, $signature] = explode('.', $token, 2);
|
||||
|
||||
if (! hash_equals($this->signature($payload), $signature)) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.invalid_signature'));
|
||||
}
|
||||
|
||||
$data = $this->decode($payload);
|
||||
|
||||
if (! isset($data['class']) || ! is_string($data['class'])) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.missing_card_class'));
|
||||
}
|
||||
|
||||
if (! isset($data['expires_at']) || (int) $data['expires_at'] < time()) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.expired'));
|
||||
}
|
||||
|
||||
if (($data['user_id'] ?? null) !== $this->userIdentifier($request)) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.wrong_user'));
|
||||
}
|
||||
|
||||
if (! is_subclass_of($data['class'], FormCard::class)) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.invalid_card_class'));
|
||||
}
|
||||
|
||||
return $data['class'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
protected function encode(array $payload): string
|
||||
{
|
||||
return rtrim(strtr(base64_encode(json_encode($payload, JSON_THROW_ON_ERROR)), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function decode(string $payload): array
|
||||
{
|
||||
$payload .= str_repeat('=', (4 - strlen($payload) % 4) % 4);
|
||||
$json = base64_decode(strtr($payload, '-_', '+/'), true);
|
||||
|
||||
if ($json === false) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.invalid_payload'));
|
||||
}
|
||||
|
||||
try {
|
||||
$decoded = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $exception) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.invalid_payload'), previous: $exception);
|
||||
}
|
||||
|
||||
if (! is_array($decoded)) {
|
||||
throw new InvalidFormCardToken(__('nova-form-card::messages.token.invalid_payload'));
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
protected function signature(string $payload): string
|
||||
{
|
||||
return hash_hmac('sha256', $payload, $this->key());
|
||||
}
|
||||
|
||||
protected function key(): string
|
||||
{
|
||||
$key = (string) config('app.key');
|
||||
|
||||
if ($key === '') {
|
||||
throw new RuntimeException(__('nova-form-card::messages.app_key_required'));
|
||||
}
|
||||
|
||||
if (str_starts_with($key, 'base64:')) {
|
||||
$decoded = base64_decode(substr($key, 7), true);
|
||||
|
||||
if ($decoded !== false) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
protected function userIdentifier(NovaRequest $request): ?string
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
if ($user === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string) $user->getAuthIdentifier();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Support;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use JsonSerializable;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use ReflectionClass;
|
||||
use ReflectionFunction;
|
||||
|
||||
class NovaFieldIntrospector
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $field): array
|
||||
{
|
||||
if ($field instanceof JsonSerializable) {
|
||||
$serialized = $field->jsonSerialize();
|
||||
|
||||
if (is_array($serialized)) {
|
||||
return $serialized;
|
||||
}
|
||||
}
|
||||
|
||||
if ($field instanceof Arrayable) {
|
||||
return $field->toArray();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function property(object $object, string $property, mixed $default = null): mixed
|
||||
{
|
||||
$class = new ReflectionClass($object);
|
||||
|
||||
while ($class) {
|
||||
if ($class->hasProperty($property)) {
|
||||
$reflection = $class->getProperty($property);
|
||||
$reflection->setAccessible(true);
|
||||
|
||||
return $reflection->getValue($object);
|
||||
}
|
||||
|
||||
$class = $class->getParentClass();
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function evaluate(mixed $value, NovaRequest $request, object $field): mixed
|
||||
{
|
||||
if (! $value instanceof \Closure) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$reflection = new ReflectionFunction($value);
|
||||
|
||||
return $reflection->getNumberOfParameters() === 0
|
||||
? $value->call($field)
|
||||
: $value->call($field, $request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Support;
|
||||
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Traversable;
|
||||
use MeghdadFadaee\NovaFormCard\Exceptions\UnsupportedFieldException;
|
||||
|
||||
class NovaFieldSerializer
|
||||
{
|
||||
public function __construct(
|
||||
protected ?NovaFieldIntrospector $introspector = null,
|
||||
protected ?NovaFieldValidator $validator = null,
|
||||
) {
|
||||
$this->introspector ??= new NovaFieldIntrospector();
|
||||
$this->validator ??= new NovaFieldValidator($this->introspector);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function serialize(array $fields, NovaRequest $request): array
|
||||
{
|
||||
return $this->validator
|
||||
->fieldsForSerialization($fields, $request)
|
||||
->map(fn (object $field): array => $this->serializeField($field, $request))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function serializeField(object $field, NovaRequest $request): array
|
||||
{
|
||||
$type = SupportedField::typeFor($field);
|
||||
|
||||
if ($type === null) {
|
||||
throw UnsupportedFieldException::forField($field);
|
||||
}
|
||||
|
||||
$this->prepareFieldForSerialization($field, $request);
|
||||
|
||||
$raw = $this->introspector->toArray($field);
|
||||
$attribute = $this->validator->attributeForField($field) ?? (string) ($raw['name'] ?? 'field');
|
||||
$value = $raw['value'] ?? $this->defaultValue($field, $request);
|
||||
|
||||
$raw['formCardType'] = $type;
|
||||
$raw['type'] = $this->fieldTypeForNovaComponent($type, $raw, $field);
|
||||
$raw['displayOnly'] = SupportedField::isDisplayOnly($field);
|
||||
$raw['name'] = (string) ($raw['name'] ?? $attribute);
|
||||
$raw['attribute'] = $attribute;
|
||||
$raw['value'] = $this->initialValue($type, $value);
|
||||
$raw['required'] = (bool) ($raw['required'] ?? false)
|
||||
|| $this->validator->fieldIsRequired($field, $request);
|
||||
$raw['readonly'] = (bool) ($raw['readonly'] ?? $this->resolvedProperty($field, 'readonlyCallback', $request, false));
|
||||
$raw['visible'] = (bool) ($raw['visible'] ?? true);
|
||||
$raw['withLabel'] = (bool) ($raw['withLabel'] ?? true);
|
||||
$raw['validationKey'] = (string) ($raw['validationKey'] ?? $attribute);
|
||||
$raw['uniqueKey'] = (string) ($raw['uniqueKey'] ?? "{$attribute}-form-card-field");
|
||||
|
||||
return $raw;
|
||||
}
|
||||
|
||||
protected function prepareFieldForSerialization(object $field, NovaRequest $request): void
|
||||
{
|
||||
if (! SupportedField::isDisplayOnly($field) || ! method_exists($field, 'resolveForDisplay')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field->resolveForDisplay($this->displayResourceFor($field, $request));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function displayResourceFor(object $field, NovaRequest $request): array
|
||||
{
|
||||
$resource = [];
|
||||
|
||||
foreach ($this->fieldsUsedForDisplayResource($field) as $displayField) {
|
||||
$attribute = $this->rawAttributeFor($displayField);
|
||||
|
||||
if ($attribute === null || $attribute === 'ComputedField') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$resource[$attribute] = $this->displayValueFor($displayField, $request);
|
||||
}
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, object>
|
||||
*/
|
||||
protected function fieldsUsedForDisplayResource(object $field): array
|
||||
{
|
||||
$fields = [$field];
|
||||
$lines = $this->introspector->property($field, 'lines');
|
||||
|
||||
if ($lines instanceof Traversable) {
|
||||
$lines = iterator_to_array($lines);
|
||||
}
|
||||
|
||||
if (is_array($lines)) {
|
||||
foreach ($lines as $line) {
|
||||
if (is_object($line)) {
|
||||
$fields[] = $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
protected function displayValueFor(object $field, NovaRequest $request): mixed
|
||||
{
|
||||
$value = $this->introspector->property($field, 'value');
|
||||
|
||||
return $value ?? $this->defaultValue($field, $request);
|
||||
}
|
||||
|
||||
protected function rawAttributeFor(object $field): ?string
|
||||
{
|
||||
$attribute = $this->introspector->property($field, 'attribute');
|
||||
|
||||
return is_string($attribute) && $attribute !== '' ? $attribute : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
protected function fieldTypeForNovaComponent(string $type, array $raw, object $field): string
|
||||
{
|
||||
if ($type === 'password-confirmation') {
|
||||
return 'password';
|
||||
}
|
||||
|
||||
if (SupportedField::isDisplayOnly($field) && is_string($raw['type'] ?? null)) {
|
||||
return $raw['type'];
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
protected function initialValue(string $type, mixed $value): mixed
|
||||
{
|
||||
return match ($type) {
|
||||
'boolean' => (bool) $value,
|
||||
'number' => $value,
|
||||
default => $value ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
protected function defaultValue(object $field, NovaRequest $request): mixed
|
||||
{
|
||||
if (method_exists($field, 'resolveDefaultValue')) {
|
||||
$value = $field->resolveDefaultValue($this->defaultResolvingRequest($request));
|
||||
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->resolvedProperty($field, 'defaultCallback', $request);
|
||||
}
|
||||
|
||||
protected function defaultResolvingRequest(NovaRequest $request): NovaRequest
|
||||
{
|
||||
$defaultRequest = NovaRequest::createFrom($request);
|
||||
$defaultRequest->setUserResolver($request->getUserResolver());
|
||||
$defaultRequest->merge([
|
||||
'editing' => 'true',
|
||||
'editMode' => 'create',
|
||||
]);
|
||||
|
||||
return $defaultRequest;
|
||||
}
|
||||
|
||||
protected function resolvedProperty(object $field, string $property, NovaRequest $request, mixed $default = null): mixed
|
||||
{
|
||||
$value = $this->introspector->property($field, $property, $default);
|
||||
|
||||
return $this->introspector->evaluate($value, $request, $field);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Support;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Nova\Fields\FieldCollection;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
use Laravel\Nova\Support\Fluent;
|
||||
use MeghdadFadaee\NovaFormCard\FormFields;
|
||||
use ReflectionFunction;
|
||||
|
||||
class NovaFieldValidator
|
||||
{
|
||||
public function __construct(protected ?NovaFieldIntrospector $introspector = null)
|
||||
{
|
||||
$this->introspector ??= new NovaFieldIntrospector();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
public function rules(array $fields, NovaRequest $request): array
|
||||
{
|
||||
$rules = [];
|
||||
|
||||
foreach ($this->fieldsForSubmission($fields, $request) as $field) {
|
||||
foreach ($this->ruleMapForField($field, $request) as $attribute => $fieldRules) {
|
||||
if (! is_string($attribute) || $attribute === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalized = $this->normalizeRules($fieldRules, $attribute, $request);
|
||||
|
||||
if ($normalized !== []) {
|
||||
$rules[$attribute] = array_merge($rules[$attribute] ?? [], $normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(array $fields, NovaRequest $request): array
|
||||
{
|
||||
$attributes = [];
|
||||
|
||||
foreach ($this->fieldsForSubmission($fields, $request) as $field) {
|
||||
if (method_exists($field, 'getValidationAttributeNames')) {
|
||||
$attributes = array_merge($attributes, $field->getValidationAttributeNames($request));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$attribute = $this->attributeForField($field);
|
||||
|
||||
if ($attribute === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes[$attribute] = (string) ($this->introspector->toArray($field)['name']
|
||||
?? $this->introspector->property($field, 'name', $attribute));
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function values(array $fields, array $payload, NovaRequest $request): array
|
||||
{
|
||||
return $this->resolve($fields, $payload, $request)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function resolve(array $fields, array $payload, NovaRequest $request): FormFields
|
||||
{
|
||||
$submissionRequest = $this->requestForPayload($request, $payload);
|
||||
$resolved = new Fluent;
|
||||
$callbacks = $this->fieldsForSubmission($fields, $submissionRequest)
|
||||
->mapWithKeys(fn (object $field): array => [
|
||||
$field->attribute => $field->fillForAction($submissionRequest, $resolved),
|
||||
])
|
||||
->filter(static fn (mixed $callback): bool => is_callable($callback));
|
||||
|
||||
return new FormFields($resolved->getAttributes(), $callbacks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
*/
|
||||
public function fieldsForSerialization(array $fields, NovaRequest $request): FieldCollection
|
||||
{
|
||||
$collection = FieldCollection::make($fields)
|
||||
->authorized($request);
|
||||
|
||||
if (method_exists($collection, 'applyDependsOnWithDefaultValues')) {
|
||||
$collection = $collection->applyDependsOnWithDefaultValues($request);
|
||||
}
|
||||
|
||||
return $collection->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, object> $fields
|
||||
*/
|
||||
public function fieldsForSubmission(array $fields, NovaRequest $request): FieldCollection
|
||||
{
|
||||
return FieldCollection::make($fields)
|
||||
->authorized($request)
|
||||
->applyDependsOn($request)
|
||||
->reject(static fn (object $field): bool => SupportedField::isDisplayOnly($field))
|
||||
->withoutReadonly($request)
|
||||
->withoutUnfillable()
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function requestForPayload(NovaRequest $request, array $payload): NovaRequest
|
||||
{
|
||||
$submissionRequest = NovaRequest::createFrom($request);
|
||||
$submissionRequest->setUserResolver($request->getUserResolver());
|
||||
$submissionRequest->merge($payload);
|
||||
$submissionRequest->merge([
|
||||
'editing' => 'true',
|
||||
'editMode' => 'create',
|
||||
]);
|
||||
|
||||
return $submissionRequest;
|
||||
}
|
||||
|
||||
public function attributeForField(object $field): ?string
|
||||
{
|
||||
$raw = $this->introspector->toArray($field);
|
||||
$attribute = $raw['attribute'] ?? $this->introspector->property($field, 'attribute');
|
||||
|
||||
return is_string($attribute) && $attribute !== '' ? $attribute : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function rulesForField(object $field, NovaRequest $request): array
|
||||
{
|
||||
$attribute = $this->attributeForField($field);
|
||||
|
||||
if ($attribute === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->normalizeRules(
|
||||
$this->ruleMapForField($field, $request)[$attribute] ?? [],
|
||||
$attribute,
|
||||
$request
|
||||
);
|
||||
}
|
||||
|
||||
public function fieldIsRequired(object $field, NovaRequest $request): bool
|
||||
{
|
||||
foreach ($this->rulesForField($field, $request) as $rule) {
|
||||
if (! is_string($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (explode('|', $rule) as $part) {
|
||||
if (strtolower(trim(strtok($part, ':'))) === 'required') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function ruleMapForField(object $field, NovaRequest $request): array
|
||||
{
|
||||
if (method_exists($field, 'getCreationRules')) {
|
||||
return $this->normalizeRuleMap($field->getCreationRules($request), $field, $request);
|
||||
}
|
||||
|
||||
if (method_exists($field, 'getRules')) {
|
||||
return $this->normalizeRuleMap($field->getRules($request), $field, $request);
|
||||
}
|
||||
|
||||
$attribute = $this->attributeForField($field);
|
||||
|
||||
if ($attribute === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
$attribute => $this->introspector->property($field, 'rules', []),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function normalizeRuleMap(mixed $rules, object $field, NovaRequest $request): array
|
||||
{
|
||||
$rules = $this->evaluateRuleCallback($rules, $request);
|
||||
|
||||
if ($rules instanceof Arrayable) {
|
||||
$rules = $rules->toArray();
|
||||
}
|
||||
|
||||
if ($rules instanceof Collection) {
|
||||
$rules = $rules->all();
|
||||
}
|
||||
|
||||
if ($rules instanceof \Traversable) {
|
||||
$rules = iterator_to_array($rules);
|
||||
}
|
||||
|
||||
$attribute = $this->attributeForField($field);
|
||||
|
||||
if (! is_array($rules)) {
|
||||
return $attribute === null ? [] : [$attribute => $rules];
|
||||
}
|
||||
|
||||
if (array_is_list($rules)) {
|
||||
return $attribute === null ? [] : [$attribute => $rules];
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
protected function normalizeRules(mixed $rules, string $attribute, NovaRequest $request): array
|
||||
{
|
||||
$rules = $this->evaluateRuleCallback($rules, $request);
|
||||
|
||||
if ($rules instanceof Arrayable) {
|
||||
$rules = $rules->toArray();
|
||||
}
|
||||
|
||||
if ($rules instanceof \Traversable) {
|
||||
$rules = iterator_to_array($rules);
|
||||
}
|
||||
|
||||
if (is_array($rules) && array_key_exists($attribute, $rules)) {
|
||||
$rules = $rules[$attribute];
|
||||
}
|
||||
|
||||
if ($rules === null || $rules === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (! is_array($rules)) {
|
||||
return [$rules];
|
||||
}
|
||||
|
||||
$normalized = [];
|
||||
|
||||
foreach ($rules as $rule) {
|
||||
$rule = $this->evaluateRuleCallback($rule, $request);
|
||||
|
||||
if ($rule instanceof Arrayable) {
|
||||
$rule = $rule->toArray();
|
||||
}
|
||||
|
||||
if (is_array($rule)) {
|
||||
array_push($normalized, ...$this->normalizeRules($rule, $attribute, $request));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rule !== null) {
|
||||
$normalized[] = $rule;
|
||||
}
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
protected function evaluateRuleCallback(mixed $rule, NovaRequest $request): mixed
|
||||
{
|
||||
if (! $rule instanceof Closure) {
|
||||
return $rule;
|
||||
}
|
||||
|
||||
$reflection = new ReflectionFunction($rule);
|
||||
|
||||
if ($reflection->getNumberOfParameters() <= 1) {
|
||||
return $rule($request);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\NovaFormCard\Support;
|
||||
|
||||
use Laravel\Nova\Fields\Audio;
|
||||
use Laravel\Nova\Fields\Avatar;
|
||||
use Laravel\Nova\Fields\Badge;
|
||||
use Laravel\Nova\Fields\Boolean;
|
||||
use Laravel\Nova\Fields\BooleanGroup;
|
||||
use Laravel\Nova\Fields\Code;
|
||||
use Laravel\Nova\Fields\Color;
|
||||
use Laravel\Nova\Fields\Country;
|
||||
use Laravel\Nova\Fields\Currency;
|
||||
use Laravel\Nova\Fields\Date;
|
||||
use Laravel\Nova\Fields\DateTime;
|
||||
use Laravel\Nova\Fields\Email;
|
||||
use Laravel\Nova\Fields\File;
|
||||
use Laravel\Nova\Fields\Gravatar;
|
||||
use Laravel\Nova\Fields\Heading;
|
||||
use Laravel\Nova\Fields\Hidden;
|
||||
use Laravel\Nova\Fields\ID;
|
||||
use Laravel\Nova\Fields\Image;
|
||||
use Laravel\Nova\Fields\KeyValue;
|
||||
use Laravel\Nova\Fields\Line;
|
||||
use Laravel\Nova\Fields\MultiSelect;
|
||||
use Laravel\Nova\Fields\Number;
|
||||
use Laravel\Nova\Fields\Password;
|
||||
use Laravel\Nova\Fields\PasswordConfirmation;
|
||||
use Laravel\Nova\Fields\Select;
|
||||
use Laravel\Nova\Fields\Slug;
|
||||
use Laravel\Nova\Fields\Sparkline;
|
||||
use Laravel\Nova\Fields\Stack;
|
||||
use Laravel\Nova\Fields\Status;
|
||||
use Laravel\Nova\Fields\Text;
|
||||
use Laravel\Nova\Fields\Textarea;
|
||||
use Laravel\Nova\Fields\Timezone;
|
||||
use Laravel\Nova\Fields\Token;
|
||||
use Laravel\Nova\Fields\UiAvatar;
|
||||
use Laravel\Nova\Fields\Unfillable;
|
||||
use Laravel\Nova\Fields\URL;
|
||||
|
||||
class SupportedField
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, string>
|
||||
*/
|
||||
protected const TYPES = [
|
||||
PasswordConfirmation::class => 'password-confirmation',
|
||||
KeyValue::class => 'key-value',
|
||||
BooleanGroup::class => 'boolean-group',
|
||||
MultiSelect::class => 'multi-select',
|
||||
DateTime::class => 'date-time',
|
||||
Currency::class => 'currency',
|
||||
Textarea::class => 'textarea',
|
||||
Timezone::class => 'timezone',
|
||||
Country::class => 'country',
|
||||
Sparkline::class => 'sparkline',
|
||||
Gravatar::class => 'gravatar',
|
||||
UiAvatar::class => 'ui-avatar',
|
||||
Heading::class => 'heading',
|
||||
Status::class => 'status',
|
||||
Badge::class => 'badge',
|
||||
Stack::class => 'stack',
|
||||
Line::class => 'line',
|
||||
Select::class => 'select',
|
||||
Boolean::class => 'boolean',
|
||||
Number::class => 'number',
|
||||
Date::class => 'date',
|
||||
Avatar::class => 'avatar',
|
||||
Image::class => 'image',
|
||||
Audio::class => 'audio',
|
||||
File::class => 'file',
|
||||
Password::class => 'password',
|
||||
Hidden::class => 'hidden',
|
||||
ID::class => 'id',
|
||||
Email::class => 'email',
|
||||
URL::class => 'url',
|
||||
Slug::class => 'slug',
|
||||
Token::class => 'token',
|
||||
Color::class => 'color',
|
||||
Code::class => 'code',
|
||||
Text::class => 'text',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<class-string, true>
|
||||
*/
|
||||
protected const DISPLAY_ONLY = [
|
||||
Badge::class => true,
|
||||
Gravatar::class => true,
|
||||
Heading::class => true,
|
||||
Line::class => true,
|
||||
Sparkline::class => true,
|
||||
Stack::class => true,
|
||||
Status::class => true,
|
||||
UiAvatar::class => true,
|
||||
];
|
||||
|
||||
public static function typeFor(object $field): ?string
|
||||
{
|
||||
if ($field instanceof Unfillable && ! self::isDisplayOnly($field)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (self::TYPES as $class => $type) {
|
||||
if ($field instanceof $class) {
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function isSupported(object $field): bool
|
||||
{
|
||||
return self::typeFor($field) !== null;
|
||||
}
|
||||
|
||||
public static function isDisplayOnly(object $field): bool
|
||||
{
|
||||
foreach (self::DISPLAY_ONLY as $class => $supported) {
|
||||
if ($field instanceof $class) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function names(): array
|
||||
{
|
||||
return [
|
||||
__('nova-form-card::messages.fields.text'),
|
||||
__('nova-form-card::messages.fields.textarea'),
|
||||
__('nova-form-card::messages.fields.select'),
|
||||
__('nova-form-card::messages.fields.boolean'),
|
||||
__('nova-form-card::messages.fields.number'),
|
||||
__('nova-form-card::messages.fields.date'),
|
||||
__('nova-form-card::messages.fields.file'),
|
||||
__('nova-form-card::messages.fields.image'),
|
||||
__('nova-form-card::messages.fields.audio'),
|
||||
__('nova-form-card::messages.fields.avatar'),
|
||||
__('nova-form-card::messages.fields.email'),
|
||||
__('nova-form-card::messages.fields.url'),
|
||||
__('nova-form-card::messages.fields.password'),
|
||||
__('nova-form-card::messages.fields.hidden'),
|
||||
__('nova-form-card::messages.fields.id'),
|
||||
__('nova-form-card::messages.fields.date_time'),
|
||||
__('nova-form-card::messages.fields.currency'),
|
||||
__('nova-form-card::messages.fields.color'),
|
||||
__('nova-form-card::messages.fields.code'),
|
||||
__('nova-form-card::messages.fields.country'),
|
||||
__('nova-form-card::messages.fields.timezone'),
|
||||
__('nova-form-card::messages.fields.multi_select'),
|
||||
__('nova-form-card::messages.fields.boolean_group'),
|
||||
__('nova-form-card::messages.fields.key_value'),
|
||||
__('nova-form-card::messages.fields.slug'),
|
||||
__('nova-form-card::messages.fields.token'),
|
||||
__('nova-form-card::messages.fields.badge'),
|
||||
__('nova-form-card::messages.fields.heading'),
|
||||
__('nova-form-card::messages.fields.line'),
|
||||
__('nova-form-card::messages.fields.stack'),
|
||||
__('nova-form-card::messages.fields.sparkline'),
|
||||
__('nova-form-card::messages.fields.status'),
|
||||
__('nova-form-card::messages.fields.gravatar'),
|
||||
__('nova-form-card::messages.fields.ui_avatar'),
|
||||
__('nova-form-card::messages.fields.password_confirmation'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user