Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GridBuilder] Add missing default value on filter #274

Merged
merged 2 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Bundle/Builder/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class Filter implements FilterInterface

private array $criteria = [];

private mixed $defaultValue = null;

private function __construct(string $name, string $type)
{
$this->name = $name;
Expand Down Expand Up @@ -153,6 +155,18 @@ public function setCriteria(array $criteria): FilterInterface
return $this;
}

public function getDefaultValue(): mixed
{
return $this->defaultValue;
}

public function setDefaultValue(mixed $defaultValue): FilterInterface
{
$this->defaultValue = $defaultValue;

return $this;
}

public function toArray(): array
{
$output = ['type' => $this->type];
Expand Down Expand Up @@ -181,6 +195,10 @@ public function toArray(): array
$output['criteria'] = $this->criteria;
}

if (null !== $this->defaultValue) {
$output['default_value'] = $this->defaultValue;
}

return $output;
}
}
4 changes: 4 additions & 0 deletions src/Bundle/Builder/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace Sylius\Bundle\GridBundle\Builder\Filter;

/**
* @method mixed getDefaultValue()
* @method self setDefaultValue(mixed $defaultValue)
*/
interface FilterInterface
{
public static function create(string $name, string $type): self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ public function it_builds_grid_with_filters(): void
Filter::create('language', 'string')
->setLabel(null),
)
->addFilter(
Filter::create('language', 'string')
->setLabel(null),
)
->addFilter(
Filter::create('archival', 'exists')
->setLabel(null)
->setDefaultValue(false),
)
;

$this->load([
Expand Down Expand Up @@ -204,6 +213,14 @@ public function it_builds_grid_with_filters(): void
'options' => [],
'form_options' => [],
],
'archival' => [
'type' => 'exists',
'default_value' => false,
'enabled' => true,
'position' => 100,
'options' => [],
'form_options' => [],
],
],
'actions' => [],
],
Expand Down
6 changes: 3 additions & 3 deletions src/Bundle/Tests/Functional/Maker/MakeGridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function getResourceClass(): string
}

EOF
;
;
}

private static function getPriceGridExpectedContent(): string
Expand Down Expand Up @@ -271,7 +271,7 @@ public function getResourceClass(): string
}

EOF
;
;
}

private static function getAdminUserGridExpectedContent(): string
Expand Down Expand Up @@ -344,6 +344,6 @@ public function getResourceClass(): string
}

EOF
;
;
}
}
12 changes: 12 additions & 0 deletions src/Bundle/spec/Builder/Filter/FilterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ function it_sets_criteria(): void

$this->getCriteria()->shouldReturn(['name' => 'test']);
}

function it_has_no_default_value_by_default(): void
{
$this->getDefaultValue()->shouldReturn(null);
}

function its_default_value_is_mutable(): void
{
$this->setDefaultValue(false);

$this->getDefaultValue()->shouldReturn(false);
}
}
1 change: 0 additions & 1 deletion src/Component/Definition/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function getCriteria()
/**
* @param mixed $criteria
*
*
* @psalm-suppress MissingReturnType
*/
public function setCriteria($criteria)
Expand Down