From 5aedd4b65c6ec3fb388c5285cdd68dbeca666698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 23 Nov 2023 14:40:59 +0100 Subject: [PATCH] computed fields --- src/Fields/BelongsToMany.php | 2 +- src/Fields/Boolean.php | 3 ++- src/Fields/Color.php | 4 +++- src/Fields/Date.php | 3 ++- src/Fields/Dropdown.php | 3 ++- src/Fields/Editor.php | 2 +- src/Fields/Email.php | 4 +++- src/Fields/Field.php | 32 ++++++++++++++++++++++---------- src/Fields/File.php | 2 +- src/Fields/HasMany.php | 2 +- src/Fields/Hidden.php | 4 +++- src/Fields/Meta.php | 2 +- src/Fields/Number.php | 2 +- src/Fields/Range.php | 4 +++- src/Fields/Relation.php | 2 +- src/Fields/Slug.php | 2 +- src/Fields/Text.php | 2 +- 17 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 2dee9f2e..325e9101 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -33,7 +33,7 @@ class BelongsToMany extends Relation /** * Create a new relation field instance. */ - public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) + public function __construct(string $label, Closure|string $modelAttribute = null, Closure|string $relation = null) { parent::__construct($label, $modelAttribute, $relation); diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php index 64637668..8cca35f6 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -2,6 +2,7 @@ namespace Cone\Root\Fields; +use Closure; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; @@ -15,7 +16,7 @@ class Boolean extends Field /** * Create a new file field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Color.php b/src/Fields/Color.php index 5a753447..f62382e3 100644 --- a/src/Fields/Color.php +++ b/src/Fields/Color.php @@ -2,12 +2,14 @@ namespace Cone\Root\Fields; +use Closure; + class Color extends Field { /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Date.php b/src/Fields/Date.php index d5f3a396..cef661c0 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -2,6 +2,7 @@ namespace Cone\Root\Fields; +use Closure; use DateTimeInterface; class Date extends Field @@ -24,7 +25,7 @@ class Date extends Field /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Dropdown.php b/src/Fields/Dropdown.php index e38adf6e..d72f4d84 100644 --- a/src/Fields/Dropdown.php +++ b/src/Fields/Dropdown.php @@ -2,6 +2,7 @@ namespace Cone\Root\Fields; +use Closure; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Support\Collection; @@ -17,7 +18,7 @@ class Dropdown extends Select /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Editor.php b/src/Fields/Editor.php index 92ed40b4..5522bac9 100644 --- a/src/Fields/Editor.php +++ b/src/Fields/Editor.php @@ -37,7 +37,7 @@ class Editor extends Field /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Email.php b/src/Fields/Email.php index 2426aa31..d2ea4053 100644 --- a/src/Fields/Email.php +++ b/src/Fields/Email.php @@ -2,12 +2,14 @@ namespace Cone\Root\Fields; +use Closure; + class Email extends Text { /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Field.php b/src/Fields/Field.php index f4126880..965beadb 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -115,17 +115,25 @@ abstract class Field implements Arrayable, JsonSerializable */ protected ?Closure $searchQueryResolver = null; + /** + * Determine if the field is computed. + */ + protected bool $computed = false; + /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { - $this->modelAttribute = $modelAttribute ?: Str::of($label)->lower()->snake()->value(); + $this->computed = $modelAttribute instanceof Closure; + + $this->modelAttribute = $this->computed ? Str::random() : ($modelAttribute ?: Str::of($label)->lower()->snake()->value()); $this->label($label); $this->name($this->modelAttribute); $this->id($this->modelAttribute); $this->setAttribute('class', 'form-control'); + $this->value($this->computed ? $modelAttribute : null); } /** @@ -269,11 +277,11 @@ public function sortable(bool|Closure $value = true): static */ public function isSortable(): bool { - if ($this->sortable instanceof Closure) { - return call_user_func($this->sortable); + if ($this->computed) { + return false; } - return $this->sortable; + return $this->sortable instanceof Closure ? call_user_func($this->sortable) : $this->sortable; } /** @@ -291,11 +299,11 @@ public function searchable(bool|Closure $value = true): static */ public function isSearchable(): bool { - if ($this->searchable instanceof Closure) { - return call_user_func($this->searchable); + if ($this->computed) { + return false; } - return $this->searchable; + return $this->searchable instanceof Closure ? call_user_func($this->searchable) : $this->searchable; } /** @@ -321,7 +329,7 @@ public function resolveSearchQuery(Request $request, Builder $query, mixed $valu /** * Set the value resolver. */ - public function value(Closure $callback): static + public function value(Closure $callback = null): static { $this->valueResolver = $callback; @@ -383,7 +391,7 @@ public function getValue(Model $model): mixed /** * Set the format resolver. */ - public function format(Closure $callback): static + public function format(Closure $callback = null): static { $this->formatResolver = $callback; @@ -551,6 +559,10 @@ public function toDisplay(Request $request, Model $model): array */ public function toInput(Request $request, Model $model): array { + if ($this->computed) { + return []; + } + return array_merge($this->toDisplay($request, $model), [ 'attrs' => $this->newAttributeBag()->class([ 'form-control--invalid' => $this->invalid($request), diff --git a/src/Fields/File.php b/src/Fields/File.php index df5dc102..b52d20a6 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -44,7 +44,7 @@ class File extends MorphToMany /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) + public function __construct(string $label, Closure|string $modelAttribute = null, Closure|string $relation = null) { parent::__construct($label, $modelAttribute, $relation); diff --git a/src/Fields/HasMany.php b/src/Fields/HasMany.php index 1ff45280..7a0186d7 100644 --- a/src/Fields/HasMany.php +++ b/src/Fields/HasMany.php @@ -11,7 +11,7 @@ class HasMany extends HasOneOrMany /** * Create a new relation field instance. */ - public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) + public function __construct(string $label, Closure|string $modelAttribute = null, Closure|string $relation = null) { parent::__construct($label, $modelAttribute, $relation); diff --git a/src/Fields/Hidden.php b/src/Fields/Hidden.php index 1391c754..5e454f5a 100644 --- a/src/Fields/Hidden.php +++ b/src/Fields/Hidden.php @@ -2,6 +2,8 @@ namespace Cone\Root\Fields; +use Closure; + class Hidden extends Field { /** @@ -12,7 +14,7 @@ class Hidden extends Field /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index c7310f93..b5944e87 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -18,7 +18,7 @@ class Meta extends MorphOne /** * Create a new relation field instance. */ - public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) + public function __construct(string $label, Closure|string $modelAttribute = null, Closure|string $relation = null) { $relation ??= function (Model $model): EloquentRelation { $related = $model->metaData()->make(); diff --git a/src/Fields/Number.php b/src/Fields/Number.php index 36080021..b2eacd2b 100644 --- a/src/Fields/Number.php +++ b/src/Fields/Number.php @@ -9,7 +9,7 @@ class Number extends Field /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Range.php b/src/Fields/Range.php index e9c492a3..a63983cf 100644 --- a/src/Fields/Range.php +++ b/src/Fields/Range.php @@ -2,6 +2,8 @@ namespace Cone\Root\Fields; +use Closure; + class Range extends Number { /** @@ -12,7 +14,7 @@ class Range extends Number /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 26b09512..9c5fe811 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -95,7 +95,7 @@ abstract class Relation extends Field implements Form /** * Create a new relation field instance. */ - public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) + public function __construct(string $label, Closure|string $modelAttribute = null, Closure|string $relation = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index ae9434bf..72d0c2f0 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -45,7 +45,7 @@ class Slug extends Text /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute); diff --git a/src/Fields/Text.php b/src/Fields/Text.php index b3c20771..3be29f1c 100644 --- a/src/Fields/Text.php +++ b/src/Fields/Text.php @@ -9,7 +9,7 @@ class Text extends Field /** * Create a new field instance. */ - public function __construct(string $label, string $modelAttribute = null) + public function __construct(string $label, Closure|string $modelAttribute = null) { parent::__construct($label, $modelAttribute);