Skip to content

Commit

Permalink
Ability to specify default value for filters (#33)
Browse files Browse the repository at this point in the history
* Implemented default value functionality.

* Default value tests.

* Added examples.

* Updated changelog.

* Change constructor parameter to optional fluent method.

* Fix styling.
  • Loading branch information
ImJustToNy authored Apr 6, 2021
1 parent 4996706 commit 14fd8fd
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `nova-enum-field` will be documented in this file.

## Unreleased
- Added ability to specify default value for `EnumFilter` and `EnumBooleanFilter`

## 2.3.0 - 2021-01-25

- Drop support for Laravel `7.x`
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class Example extends Resource
// Or with optional filter name:
(new EnumFilter('user_type', UserType::class))
->name('Type of user'),

// With optional default value:
(new EnumFilter('user_type', UserType::class))
->default(UserType::Administrator),
];
}
}
Expand All @@ -140,9 +144,16 @@ class Example extends Resource

new EnumBooleanFilter('user_permissions', UserPermissions::class),

// Or with optional filter name:
// With optional filter name:
(new EnumBooleanFilter('user_type', UserType::class))
->name('Type of user'),

// With optional default values:
(new EnumBooleanFilter('user_type', UserType::class))
->default([
UserType::Administrator,
UserType::Moderator,
]),

// When filtering a FlaggedEnum, it will default to filtering
// by ANY flags, however you may wish to filter by ALL flags:
Expand Down
22 changes: 22 additions & 0 deletions src/EnumBooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class EnumBooleanFilter extends BooleanFilter

protected $class;

protected $default;

protected $flagged;

protected $scope = 'any';
Expand Down Expand Up @@ -90,4 +92,24 @@ public function options(Request $request)

return array_flip($this->class::asSelectArray());
}

public function default()
{
if (isset(func_get_args()[0])) {
$this->default = collect(is_array(func_get_args()[0]) ? func_get_args()[0] : [func_get_args()[0]])
->map(function ($value, $key) {
return is_subclass_of($value, \BenSampo\Enum\Enum::class) ? $value->value : $value;
})->all();

return $this;
}

if (is_null($this->default)) {
return parent::default();
}

return collect($this->default)->mapWithKeys(function ($option) {
return [$option => true];
})->all() + parent::default();
}
}
17 changes: 17 additions & 0 deletions src/EnumFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class EnumFilter extends Filter

protected $class;

protected $default;

protected $flagged;

public function __construct($column, $class)
Expand Down Expand Up @@ -49,4 +51,19 @@ public function options(Request $request)
{
return array_flip($this->class::asSelectArray());
}

public function default()
{
if (isset(func_get_args()[0])) {
$this->default = is_subclass_of(func_get_args()[0], \BenSampo\Enum\Enum::class) ? func_get_args()[0]->value : func_get_args()[0];

return $this;
}

if (is_null($this->default)) {
return parent::default();
}

return $this->default;
}
}
41 changes: 41 additions & 0 deletions tests/Filters/BooleanFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,45 @@ public function it_can_have_a_different_name()

$this->assertEquals('Different name', $this->filter->name());
}

/** @test */
public function it_accepts_optional_default_values()
{
$this->filter->default(IntegerEnum::Moderator);

$this->assertEquals([
IntegerEnum::Administrator => false,
IntegerEnum::Moderator => true,
IntegerEnum::Subscriber => false,
], $this->filter->jsonSerialize()['currentValue']);

$this->filter->default(IntegerEnum::Administrator());

$this->assertEquals([
IntegerEnum::Administrator => true,
IntegerEnum::Moderator => false,
IntegerEnum::Subscriber => false,
], $this->filter->jsonSerialize()['currentValue']);

$this->filter->default([
IntegerEnum::Subscriber,
IntegerEnum::Moderator(),
]);

$this->assertEquals([
IntegerEnum::Administrator => false,
IntegerEnum::Moderator => true,
IntegerEnum::Subscriber => true,
], $this->filter->jsonSerialize()['currentValue']);
}

/** @test */
public function it_has_no_default_value_by_default()
{
$this->assertEquals([
IntegerEnum::Administrator => false,
IntegerEnum::Moderator => false,
IntegerEnum::Subscriber => false,
], $this->filter->jsonSerialize()['currentValue']);
}
}
18 changes: 18 additions & 0 deletions tests/Filters/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ public function it_can_have_a_different_name()

$this->assertEquals('Different name', $this->filter->name());
}

/** @test */
public function it_accepts_an_optional_default_value()
{
$this->filter->default(IntegerEnum::Moderator);

$this->assertEquals(IntegerEnum::Moderator, $this->filter->jsonSerialize()['currentValue']);

$this->filter->default(IntegerEnum::Subscriber());

$this->assertEquals(IntegerEnum::Subscriber, $this->filter->jsonSerialize()['currentValue']);
}

/** @test */
public function it_has_no_default_value_by_default()
{
$this->assertEquals('', $this->filter->jsonSerialize()['currentValue']);
}
}

0 comments on commit 14fd8fd

Please sign in to comment.