From 08aff9a542e292ccd0e7980a8f1a13e9ffcc9ae9 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Mon, 14 Oct 2024 13:40:26 +0200 Subject: [PATCH] Added FormConfigStamp (#367) --- config/listeners.php | 1 + src/Config/ConfigLoader.php | 5 +++++ src/Config/FormConfig.php | 9 +++++++++ src/EventListener/ProcessFormDataListener.php | 9 +++++++++ src/Parcel/Stamp/FormConfigStamp.php | 20 +++++++++++++++++++ src/Token/TokenCollection.php | 13 ++++++++++++ tests/Token/TokenCollectionTest.php | 17 ++++++++++++++++ 7 files changed, 74 insertions(+) create mode 100644 src/Config/FormConfig.php create mode 100644 src/Parcel/Stamp/FormConfigStamp.php diff --git a/config/listeners.php b/config/listeners.php index 46a099b2..d09a0fdb 100644 --- a/config/listeners.php +++ b/config/listeners.php @@ -107,6 +107,7 @@ ->args([ service(NotificationCenter::class), service(FileUploadNormalizer::class), + service(ConfigLoader::class), ]) ; diff --git a/src/Config/ConfigLoader.php b/src/Config/ConfigLoader.php index 21771366..425f8eee 100644 --- a/src/Config/ConfigLoader.php +++ b/src/Config/ConfigLoader.php @@ -38,6 +38,11 @@ public function loadModule(int $id): ModuleConfig|null return $this->loadConfig($id, 'tl_module', ModuleConfig::class); } + public function loadForm(int $id): FormConfig|null + { + return $this->loadConfig($id, 'tl_form', FormConfig::class); + } + /** * @return array */ diff --git a/src/Config/FormConfig.php b/src/Config/FormConfig.php new file mode 100644 index 00000000..f8255f5d --- /dev/null +++ b/src/Config/FormConfig.php @@ -0,0 +1,9 @@ +with(new BulkyItemsStamp($bulkyItemVouchers)); } + $formConfig = $this->configLoader->loadForm((int) $form->id); + + if (null !== $formConfig) { + $stamps = $stamps->with(new FormConfigStamp($formConfig)); + } + $this->notificationCenter->sendNotificationWithStamps((int) $formData['nc_notification'], $stamps); } } diff --git a/src/Parcel/Stamp/FormConfigStamp.php b/src/Parcel/Stamp/FormConfigStamp.php new file mode 100644 index 00000000..c0fc06dc --- /dev/null +++ b/src/Parcel/Stamp/FormConfigStamp.php @@ -0,0 +1,20 @@ +formConfig); + } + + public static function fromArray(array $data): self + { + return new self(FormConfig::fromArray($data)); + } +} diff --git a/src/Token/TokenCollection.php b/src/Token/TokenCollection.php index 0bc5fd68..b432436f 100644 --- a/src/Token/TokenCollection.php +++ b/src/Token/TokenCollection.php @@ -29,6 +29,19 @@ public static function fromSerializedArray(array $data): self return new self($tokens); } + public function replaceToken(Token $token): self + { + $existing = $this->getByName($token->getName()); + + if (null !== $existing) { + $this->remove($existing); + } + + $this->addToken($token); + + return $this; + } + /** * Provides a fluent interface alternative to add() with a type hint. */ diff --git a/tests/Token/TokenCollectionTest.php b/tests/Token/TokenCollectionTest.php index 9e16176b..6dd27aaf 100644 --- a/tests/Token/TokenCollectionTest.php +++ b/tests/Token/TokenCollectionTest.php @@ -61,6 +61,23 @@ public function testCollectionHandling(): void $this->assertNull($tokenCollection->getByName('form_i_do_not_exist')); } + public function testReplace(): void + { + $tokenCollection = new TokenCollection(); + $tokenCollection->addToken(Token::fromValue('test', 'foobar')); + $tokenCollection->addToken(Token::fromValue('test', 'foobar new')); + + $this->assertCount(2, $tokenCollection); + $this->assertSame('foobar', $tokenCollection->getByName('test')->getValue(), 'foobar'); + + $tokenCollection = new TokenCollection(); + $tokenCollection->replaceToken(Token::fromValue('test', 'foobar')); + $tokenCollection->replaceToken(Token::fromValue('test', 'foobar new')); + + $this->assertCount(1, $tokenCollection); + $this->assertSame('foobar new', $tokenCollection->getByName('test')->getValue(), 'foobar'); + } + public function testMerge(): void { $tokenCollectionA = new TokenCollection();