Skip to content

Commit

Permalink
computed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 23, 2023
1 parent bac111f commit 5aedd4b
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Fields;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/Fields/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Fields;

use Closure;
use DateTimeInterface;

class Date extends Field
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/Fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Fields;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/Fields/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 22 additions & 10 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/Fields/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cone\Root\Fields;

use Closure;

class Hidden extends Field
{
/**
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/Fields/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cone\Root\Fields;

use Closure;

class Range extends Number
{
/**
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5aedd4b

Please sign in to comment.