Skip to content

Commit

Permalink
add eval config to DcaFieldConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
ericges committed Nov 27, 2024
1 parent 58a52e7 commit 95374cc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/Dca/DcaFieldConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class DcaFieldConfiguration
protected bool $search = false;
protected bool $filter = false;
protected bool $sorting = false;
protected array $eval = [];

/**
* @param string $table
*/
public function __construct(private string $table)
{

}
public function __construct(private readonly string $table) {}

public function getTable(): string
{
Expand Down Expand Up @@ -77,4 +75,20 @@ public function setFilter(bool $filter): DcaFieldConfiguration
$this->filter = $filter;
return $this;
}

public function setEvalValue(string $key, mixed $value): DcaFieldConfiguration
{
$this->eval[$key] = $value;
return $this;
}

public function getEvalValue(string $key): mixed
{
return $this->eval[$key] ?? null;
}

public function getEval(): array
{
return $this->eval;
}
}
6 changes: 5 additions & 1 deletion src/EventListener/DcaField/AbstractDcaFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getModelInstance(string $table, int $id): ?Model
return $framework->getAdapter($modelClass)->findByPk($id);
}

protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration)
protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration): void
{
if ($configuration->isFilter()) {
$field['filter'] = true;
Expand All @@ -42,6 +42,10 @@ protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfigura
if ($configuration->getFlag() !== null) {
$field['flag'] = $configuration->getFlag();
}

if (!empty($configuration->getEval())) {
$field['eval'] = \array_merge($field['eval'] ?? [], $configuration->getEval());
}
}

public static function getSubscribedServices(): array
Expand Down
33 changes: 32 additions & 1 deletion tests/Dca/DcaFieldOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,40 @@

class DcaFieldOptionsTest extends TestCase
{
public function testGetTable()
public function testAllOptions()
{
$dcaFieldOptions = new DcaFieldConfiguration('test_table');
$this->assertEquals('test_table', $dcaFieldOptions->getTable());

$dcaFieldOptions
->setFlag(69)
->setExclude(true)
->setSearch(true)
->setFilter(true)
->setSorting(true)
->setEvalValue('test_eval_1', 'test_value_1')
->setEvalValue('test_eval_2', 'test_value_2')
;
$this->assertEquals(69, $dcaFieldOptions->getFlag());
$this->assertTrue($dcaFieldOptions->isExclude());
$this->assertTrue($dcaFieldOptions->isSearch());
$this->assertTrue($dcaFieldOptions->isFilter());
$this->assertTrue($dcaFieldOptions->isSorting());
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_2',
],
$dcaFieldOptions->getEval()
);

$dcaFieldOptions->setEvalValue('test_eval_2', 'test_value_new2');
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_new2',
],
$dcaFieldOptions->getEval()
);
}
}

0 comments on commit 95374cc

Please sign in to comment.