From 95374cca91f037609264b2b52f2c2d64efdfe2b4 Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Wed, 27 Nov 2024 13:46:06 +0100 Subject: [PATCH] add eval config to DcaFieldConfiguration --- src/Dca/DcaFieldConfiguration.php | 22 ++++++++++--- .../DcaField/AbstractDcaFieldListener.php | 6 +++- tests/Dca/DcaFieldOptionsTest.php | 33 ++++++++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Dca/DcaFieldConfiguration.php b/src/Dca/DcaFieldConfiguration.php index 3258d8e0..261a3cf9 100644 --- a/src/Dca/DcaFieldConfiguration.php +++ b/src/Dca/DcaFieldConfiguration.php @@ -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 { @@ -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; + } } \ No newline at end of file diff --git a/src/EventListener/DcaField/AbstractDcaFieldListener.php b/src/EventListener/DcaField/AbstractDcaFieldListener.php index ac3d4b0b..8f859d0f 100644 --- a/src/EventListener/DcaField/AbstractDcaFieldListener.php +++ b/src/EventListener/DcaField/AbstractDcaFieldListener.php @@ -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; @@ -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 diff --git a/tests/Dca/DcaFieldOptionsTest.php b/tests/Dca/DcaFieldOptionsTest.php index 2334a01d..400c1c98 100644 --- a/tests/Dca/DcaFieldOptionsTest.php +++ b/tests/Dca/DcaFieldOptionsTest.php @@ -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() + ); } } \ No newline at end of file