From b0bfa1b3d55449da4c5955cb9b2a3860075b4559 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 21 Jun 2022 18:06:23 +0300 Subject: [PATCH 01/13] adds FlashMessengerNamespace class Signed-off-by: Aleksandr --- src/View/Helper/FlashMessengerNamespace.php | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/View/Helper/FlashMessengerNamespace.php diff --git a/src/View/Helper/FlashMessengerNamespace.php b/src/View/Helper/FlashMessengerNamespace.php new file mode 100644 index 0000000..a8683e9 --- /dev/null +++ b/src/View/Helper/FlashMessengerNamespace.php @@ -0,0 +1,75 @@ +'; + private string $messageOpenFormat = '
  • '; + private string $messageSeparatorString = '
  • '; + + public function __construct(string $name, string $classes = '') + { + $this->name = $name; + $this->classes = $classes; + } + + public function getName(): string + { + return $this->name; + } + + public function getMessageCloseString(): string + { + return $this->messageCloseString; + } + + public function setMessageCloseString(string $messageCloseString): void + { + $this->messageCloseString = $messageCloseString; + } + + public function getMessageOpenFormat(): string + { + return $this->messageOpenFormat; + } + + public function setMessageOpenFormat(string $messageOpenFormat): self + { + $this->messageOpenFormat = $messageOpenFormat; + + return $this; + } + + public function getMessageSeparatorString(): string + { + return $this->messageSeparatorString; + } + + public function setMessageSeparatorString(string $messageSeparatorString): self + { + $this->messageSeparatorString = $messageSeparatorString; + + return $this; + } + + public function getClasses(): string + { + return $this->classes; + } +} From b6e83aef439ab83d134f3cb4efc7905b669da9b5 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 21 Jun 2022 18:07:37 +0300 Subject: [PATCH 02/13] adopts FlashMessenger to be ready to use FlashMessengerNamespace class --- src/View/Helper/FlashMessenger.php | 104 +++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index 052440f..f80096b 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -89,6 +89,21 @@ class FlashMessenger extends AbstractHelper */ protected $pluginFlashMessenger; + /** + * @var array + */ + protected $namespaces = array(); + + /** + * @param array $namespaces + */ + public function __construct(array $namespaces = array()) + { + foreach ($namespaces as $namespace) { + $this->namespaces[$namespace->getName()] = $namespace; + } + } + /** * Returns the flash messenger plugin controller * @@ -169,12 +184,7 @@ protected function renderMessages( // Prepare classes for opening tag if (empty($classes)) { - if (isset($this->classMessages[$namespace])) { - $classes = $this->classMessages[$namespace]; - } else { - $classes = $this->classMessages['default']; - } - $classes = [$classes]; + $classes = [$this->getClasses($namespace)]; } $autoEscape ??= $this->autoEscape; @@ -208,12 +218,12 @@ function ($item) use (&$messagesToPrint, $escapeHtml, $autoEscape, $translator, } // Generate markup - $markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"'); + $markup = sprintf($this->getMessageOpenFormat($namespace), ' class="' . implode(' ', $classes) . '"'); $markup .= implode( - sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'), + sprintf($this->getMessageSeparatorString($namespace), ' class="' . implode(' ', $classes) . '"'), $messagesToPrint ); - $markup .= $this->getMessageCloseString(); + $markup .= $this->getMessageCloseString($namespace); return $markup; } @@ -245,9 +255,14 @@ public function getAutoEscape() * @param string $messageCloseString * @return FlashMessenger */ - public function setMessageCloseString($messageCloseString) + public function setMessageCloseString($messageCloseString, string $namespace = 'default') { - $this->messageCloseString = (string) $messageCloseString; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + $objNamespace->setMessageCloseString((string) $messageCloseString); + } else { + $this->messageCloseString = (string) $messageCloseString; + } return $this; } @@ -256,9 +271,14 @@ public function setMessageCloseString($messageCloseString) * * @return string */ - public function getMessageCloseString() + public function getMessageCloseString(string $namespace = 'default') { - return $this->messageCloseString; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + return $objNamespace->getMessageCloseString(); + } else { + return $this->messageCloseString; + } } /** @@ -267,9 +287,14 @@ public function getMessageCloseString() * @param string $messageOpenFormat * @return FlashMessenger */ - public function setMessageOpenFormat($messageOpenFormat) + public function setMessageOpenFormat($messageOpenFormat, string $namespace = 'default') { - $this->messageOpenFormat = (string) $messageOpenFormat; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + $objNamespace->setMessageOpenFormat((string) $messageOpenFormat); + } else { + $this->messageOpenFormat = (string) $messageOpenFormat; + } return $this; } @@ -278,9 +303,14 @@ public function setMessageOpenFormat($messageOpenFormat) * * @return string */ - public function getMessageOpenFormat() + public function getMessageOpenFormat(string $namespace = 'default') { - return $this->messageOpenFormat; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + return $objNamespace->getMessageOpenFormat(); + } else { + return $this->messageOpenFormat; + } } /** @@ -289,9 +319,14 @@ public function getMessageOpenFormat() * @param string $messageSeparatorString * @return FlashMessenger */ - public function setMessageSeparatorString($messageSeparatorString) + public function setMessageSeparatorString($messageSeparatorString, string $namespace = 'default') { - $this->messageSeparatorString = (string) $messageSeparatorString; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + $objNamespace->setMessageSeparatorString((string) $messageSeparatorString); + } else { + $this->messageSeparatorString = (string) $messageSeparatorString; + } return $this; } @@ -300,9 +335,14 @@ public function setMessageSeparatorString($messageSeparatorString) * * @return string */ - public function getMessageSeparatorString() + public function getMessageSeparatorString(string $namespace = 'default') { - return $this->messageSeparatorString; + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + return $objNamespace->getMessageSeparatorString(); + } else { + return $this->messageSeparatorString; + } } /** @@ -369,4 +409,26 @@ protected function getEscapeHtmlHelper() return $this->escapeHtmlHelper; } + + /** + * @param string $namespace + * @return FlashMessengerNamespace|null + */ + private function getNamespace(string $namespace) : ?FlashMessengerNamespace + { + return $this->namespaces[$namespace] ?? null; + } + + /** + * @return string + */ + private function getClasses(string $namespace): string + { + $objNamespace = $this->getNamespace($namespace); + if ($objNamespace !== null) { + return $objNamespace->getClasses(); + } else { + return $this->classMessages[$namespace]?? ''; + } + } } From 1e9564ffd1b651999639491b6485d35ecd5c49c0 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 21 Jun 2022 18:09:34 +0300 Subject: [PATCH 03/13] separates on two ways to create helper: old with 1-dimensional array config or actual in this version - multidimensional array config --- src/View/Helper/FlashMessengerFactory.php | 74 ++++++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 24dfdf6..f67385a 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -5,6 +5,7 @@ namespace Laminas\Mvc\Plugin\FlashMessenger\View\Helper; use Interop\Container\ContainerInterface; +use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger as PluginFlashMessenger; use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; @@ -25,26 +26,79 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = if (! method_exists($container, 'configure')) { $container = $container->getServiceLocator(); } - $helper = new FlashMessenger(); + $controllerPluginManager = $container->get('ControllerPluginManager'); $flashMessenger = $controllerPluginManager->get('flashmessenger'); + $config = $container->get('config'); + if ( + isset($config['view_helper_config']['flashmessenger']) && + is_array($config['view_helper_config']['flashmessenger']) + ) { + $isArrayOneDimensial = true; + foreach ($config['view_helper_config']['flashmessenger'] as $property) { + if (is_array($property)) { + $isArrayOneDimensial = false; + break; + } + } + + + if ($isArrayOneDimensial === true) { + return $this->createHelperWithOldConfig($flashMessenger, $config); + } else { + return $this->createHelperWithActualConfig($flashMessenger, $config); + } + } + + return new FlashMessenger(); + } + + private function createHelperWithOldConfig($flashMessenger, $config) + { + $helper = new FlashMessenger(); $helper->setPluginFlashMessenger($flashMessenger); - $config = $container->get('config'); - if (isset($config['view_helper_config']['flashmessenger'])) { - $configHelper = $config['view_helper_config']['flashmessenger']; - if (isset($configHelper['message_open_format'])) { - $helper->setMessageOpenFormat($configHelper['message_open_format']); + $configHelper = $config['view_helper_config']['flashmessenger']; + if (isset($configHelper['message_open_format'])) { + $helper + ->setMessageOpenFormat($configHelper['message_open_format']); + } + if (isset($configHelper['message_separator_string'])) { + $helper + ->setMessageSeparatorString($configHelper['message_separator_string']); + } + if (isset($configHelper['message_close_string'])) { + $helper + ->setMessageCloseString($configHelper['message_close_string']); + } + + return $helper; + } + + private function createHelperWithActualConfig($flashMessenger, $config) + { + $namespaces = []; + $configHelper = $config['view_helper_config']['flashmessenger']; + foreach ($configHelper as $configNamespace => $arrProperties) { + $namespace = new FlashMessengerNamespace($configNamespace, isset($arrProperties['classes']) ? (string) $arrProperties['classes'] : ''); + if (isset($arrProperties['message_open_format'])) { + $namespace + ->setMessageOpenFormat((string) $arrProperties['message_open_format']); } - if (isset($configHelper['message_separator_string'])) { - $helper->setMessageSeparatorString($configHelper['message_separator_string']); + if (isset($arrProperties['message_separator_string'])) { + $namespace + ->setMessageSeparatorString((string) $arrProperties['message_separator_string']); } - if (isset($configHelper['message_close_string'])) { - $helper->setMessageCloseString($configHelper['message_close_string']); + if (isset($arrProperties['message_close_string'])) { + $namespace + ->setMessageCloseString((string) $arrProperties['message_close_string']); } + $namespaces[$namespace->getName()] = $namespace; } + $helper = new FlashMessenger($namespaces); + $helper->setPluginFlashMessenger($flashMessenger); return $helper; } From 75597b84bc1b016be4df16b4e85337867f9d323d Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 21 Jun 2022 18:10:01 +0300 Subject: [PATCH 04/13] adds test Signed-off-by: Aleksandr --- test/View/Helper/FlashMessengerTest.php | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php index 15f1641..30747de 100644 --- a/test/View/Helper/FlashMessengerTest.php +++ b/test/View/Helper/FlashMessengerTest.php @@ -564,4 +564,53 @@ public function testRenderCurrentUsesCurrentAutoEscapeByDefault(): void $display = $this->helper->renderCurrent('default'); $this->assertSame($displayAssertion, $display); } + + /** + * @covers \Laminas\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testNamespacesMessageFormatIsDifferent(): void + { + $this->seedMessages(); + + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'default' => [ + 'message_open_format' => '
      ', + 'message_separator_string' => '', + 'message_close_string' => '
    ', + 'classes' => 'foo-baz foo-bar' + ], + 'info' => [ + 'message_open_format' => '
      ', + 'message_separator_string' => '', + 'message_close_string' => '
    ', + 'classes' => 'foo-bar foo-baz' + ], + ], + ], + ]; + $services = $this->createServiceManager($config); + $helper = $this->retrieveViewHelperFrom($services); + + $displayInfoAssertion = '
      ' + . '
    • foo
    • ' + . '
    • bar
    • ' + . '
    '; + $displayInfo = $helper->renderCurrent('default'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + + + $displayInfoAssertion = '
      ' + . '
    • bar-info
    • ' + . '
    '; + $displayInfo = $helper->renderCurrent('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + + $displayInfoAssertion = '
      ' + . '
    • bar-warning
    • ' + . '
    '; + $displayInfo = $helper->renderCurrent('warning'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } } From 4e0e2e92bb9a599212182f82010140d7b443ba08 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Tue, 21 Jun 2022 18:19:05 +0300 Subject: [PATCH 05/13] phpcs Signed-off-by: Aleksandr --- src/View/Helper/FlashMessenger.php | 22 ++++++--------------- src/View/Helper/FlashMessengerFactory.php | 20 +++++++++++++++---- src/View/Helper/FlashMessengerNamespace.php | 2 +- test/View/Helper/FlashMessengerTest.php | 7 +++---- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index f80096b..56c21b4 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -13,7 +13,6 @@ use function array_walk_recursive; use function assert; use function call_user_func_array; -use function get_class; use function gettype; use function implode; use function is_object; @@ -89,15 +88,13 @@ class FlashMessenger extends AbstractHelper */ protected $pluginFlashMessenger; - /** - * @var array - */ - protected $namespaces = array(); + /** @var array */ + protected $namespaces = []; /** * @param array $namespaces */ - public function __construct(array $namespaces = array()) + public function __construct(array $namespaces = []) { foreach ($namespaces as $namespace) { $this->namespaces[$namespace->getName()] = $namespace; @@ -361,7 +358,7 @@ public function setPluginFlashMessenger($pluginFlashMessenger) '%s expects a %s instance; received %s', __METHOD__, PluginFlashMessenger::class, - is_object($pluginFlashMessenger) ? get_class($pluginFlashMessenger) : gettype($pluginFlashMessenger) + is_object($pluginFlashMessenger) ? $pluginFlashMessenger::class : gettype($pluginFlashMessenger) )); } @@ -410,25 +407,18 @@ protected function getEscapeHtmlHelper() return $this->escapeHtmlHelper; } - /** - * @param string $namespace - * @return FlashMessengerNamespace|null - */ - private function getNamespace(string $namespace) : ?FlashMessengerNamespace + private function getNamespace(string $namespace): ?FlashMessengerNamespace { return $this->namespaces[$namespace] ?? null; } - /** - * @return string - */ private function getClasses(string $namespace): string { $objNamespace = $this->getNamespace($namespace); if ($objNamespace !== null) { return $objNamespace->getClasses(); } else { - return $this->classMessages[$namespace]?? ''; + return $this->classMessages[$namespace] ?? ''; } } } diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index f67385a..2b6546a 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -5,10 +5,10 @@ namespace Laminas\Mvc\Plugin\FlashMessenger\View\Helper; use Interop\Container\ContainerInterface; -use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger as PluginFlashMessenger; use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; +use function is_array; use function method_exists; class FlashMessengerFactory implements FactoryInterface @@ -30,7 +30,7 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = $controllerPluginManager = $container->get('ControllerPluginManager'); $flashMessenger = $controllerPluginManager->get('flashmessenger'); - $config = $container->get('config'); + $config = $container->get('config'); if ( isset($config['view_helper_config']['flashmessenger']) && is_array($config['view_helper_config']['flashmessenger']) @@ -43,7 +43,6 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = } } - if ($isArrayOneDimensial === true) { return $this->createHelperWithOldConfig($flashMessenger, $config); } else { @@ -54,6 +53,11 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = return new FlashMessenger(); } + /** + * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger + * @param array $config + * @return FlashMessenger + */ private function createHelperWithOldConfig($flashMessenger, $config) { $helper = new FlashMessenger(); @@ -76,12 +80,20 @@ private function createHelperWithOldConfig($flashMessenger, $config) return $helper; } + /** + * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger + * @param array $config + * @return FlashMessenger + */ private function createHelperWithActualConfig($flashMessenger, $config) { $namespaces = []; $configHelper = $config['view_helper_config']['flashmessenger']; foreach ($configHelper as $configNamespace => $arrProperties) { - $namespace = new FlashMessengerNamespace($configNamespace, isset($arrProperties['classes']) ? (string) $arrProperties['classes'] : ''); + $namespace = new FlashMessengerNamespace( + $configNamespace, + isset($arrProperties['classes']) ? (string) $arrProperties['classes'] : '' + ); if (isset($arrProperties['message_open_format'])) { $namespace ->setMessageOpenFormat((string) $arrProperties['message_open_format']); diff --git a/src/View/Helper/FlashMessengerNamespace.php b/src/View/Helper/FlashMessengerNamespace.php index a8683e9..8c4270c 100644 --- a/src/View/Helper/FlashMessengerNamespace.php +++ b/src/View/Helper/FlashMessengerNamespace.php @@ -25,7 +25,7 @@ class FlashMessengerNamespace public function __construct(string $name, string $classes = '') { - $this->name = $name; + $this->name = $name; $this->classes = $classes; } diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php index 30747de..b9500e1 100644 --- a/test/View/Helper/FlashMessengerTest.php +++ b/test/View/Helper/FlashMessengerTest.php @@ -579,13 +579,13 @@ public function testNamespacesMessageFormatIsDifferent(): void 'message_open_format' => '
      ', 'message_separator_string' => '', 'message_close_string' => '
    ', - 'classes' => 'foo-baz foo-bar' + 'classes' => 'foo-baz foo-bar', ], - 'info' => [ + 'info' => [ 'message_open_format' => '
      ', 'message_separator_string' => '', 'message_close_string' => '
    ', - 'classes' => 'foo-bar foo-baz' + 'classes' => 'foo-bar foo-baz', ], ], ], @@ -600,7 +600,6 @@ public function testNamespacesMessageFormatIsDifferent(): void $displayInfo = $helper->renderCurrent('default'); $this->assertEquals($displayInfoAssertion, $displayInfo); - $displayInfoAssertion = '
      ' . '
    • bar-info
    • ' . '
    '; From 8fcff08eb2d7da3ca766e7dda282d9b3c2782b3a Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Wed, 22 Jun 2022 18:02:31 +0300 Subject: [PATCH 06/13] adds fixes to pass the tests Signed-off-by: Aleksandr --- src/View/Helper/FlashMessenger.php | 15 ++++----- src/View/Helper/FlashMessengerFactory.php | 37 +++++++++++------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index 56c21b4..e974fde 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -13,6 +13,7 @@ use function array_walk_recursive; use function assert; use function call_user_func_array; +use function get_class; use function gettype; use function implode; use function is_object; @@ -256,9 +257,9 @@ public function setMessageCloseString($messageCloseString, string $namespace = ' { $objNamespace = $this->getNamespace($namespace); if ($objNamespace !== null) { - $objNamespace->setMessageCloseString((string) $messageCloseString); + $objNamespace->setMessageCloseString($messageCloseString); } else { - $this->messageCloseString = (string) $messageCloseString; + $this->messageCloseString = $messageCloseString; } return $this; } @@ -288,9 +289,9 @@ public function setMessageOpenFormat($messageOpenFormat, string $namespace = 'de { $objNamespace = $this->getNamespace($namespace); if ($objNamespace !== null) { - $objNamespace->setMessageOpenFormat((string) $messageOpenFormat); + $objNamespace->setMessageOpenFormat($messageOpenFormat); } else { - $this->messageOpenFormat = (string) $messageOpenFormat; + $this->messageOpenFormat = $messageOpenFormat; } return $this; } @@ -320,9 +321,9 @@ public function setMessageSeparatorString($messageSeparatorString, string $names { $objNamespace = $this->getNamespace($namespace); if ($objNamespace !== null) { - $objNamespace->setMessageSeparatorString((string) $messageSeparatorString); + $objNamespace->setMessageSeparatorString($messageSeparatorString); } else { - $this->messageSeparatorString = (string) $messageSeparatorString; + $this->messageSeparatorString = $messageSeparatorString; } return $this; } @@ -358,7 +359,7 @@ public function setPluginFlashMessenger($pluginFlashMessenger) '%s expects a %s instance; received %s', __METHOD__, PluginFlashMessenger::class, - is_object($pluginFlashMessenger) ? $pluginFlashMessenger::class : gettype($pluginFlashMessenger) + is_object($pluginFlashMessenger) ? get_class($pluginFlashMessenger) : gettype($pluginFlashMessenger) )); } diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 2b6546a..573ea15 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -28,15 +28,15 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = } $controllerPluginManager = $container->get('ControllerPluginManager'); - $flashMessenger = $controllerPluginManager->get('flashmessenger'); + /** @var \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger */ + $flashMessenger = $controllerPluginManager->get('flashmessenger'); $config = $container->get('config'); - if ( - isset($config['view_helper_config']['flashmessenger']) && - is_array($config['view_helper_config']['flashmessenger']) - ) { + if (isset($config['view_helper_config']['flashmessenger'])) { + $configHelper = (array) $config['view_helper_config']['flashmessenger']; $isArrayOneDimensial = true; - foreach ($config['view_helper_config']['flashmessenger'] as $property) { + /** @var string|array $property */ + foreach ($configHelper as $property) { if (is_array($property)) { $isArrayOneDimensial = false; break; @@ -44,9 +44,9 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = } if ($isArrayOneDimensial === true) { - return $this->createHelperWithOldConfig($flashMessenger, $config); + return $this->createHelperWithOldConfig($flashMessenger, $configHelper); } else { - return $this->createHelperWithActualConfig($flashMessenger, $config); + return $this->createHelperWithActualConfig($flashMessenger, $configHelper); } } @@ -55,26 +55,25 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = /** * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger - * @param array $config + * @param array $configHelper * @return FlashMessenger */ - private function createHelperWithOldConfig($flashMessenger, $config) + private function createHelperWithOldConfig($flashMessenger, $configHelper) { $helper = new FlashMessenger(); $helper->setPluginFlashMessenger($flashMessenger); - $configHelper = $config['view_helper_config']['flashmessenger']; if (isset($configHelper['message_open_format'])) { $helper - ->setMessageOpenFormat($configHelper['message_open_format']); + ->setMessageOpenFormat((string) $configHelper['message_open_format']); } if (isset($configHelper['message_separator_string'])) { $helper - ->setMessageSeparatorString($configHelper['message_separator_string']); + ->setMessageSeparatorString((string) $configHelper['message_separator_string']); } if (isset($configHelper['message_close_string'])) { $helper - ->setMessageCloseString($configHelper['message_close_string']); + ->setMessageCloseString((string) $configHelper['message_close_string']); } return $helper; @@ -82,16 +81,16 @@ private function createHelperWithOldConfig($flashMessenger, $config) /** * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger - * @param array $config + * @param array $configHelper * @return FlashMessenger */ - private function createHelperWithActualConfig($flashMessenger, $config) + private function createHelperWithActualConfig($flashMessenger, $configHelper) { - $namespaces = []; - $configHelper = $config['view_helper_config']['flashmessenger']; + $namespaces = []; + /** @var array $arrProperties */ foreach ($configHelper as $configNamespace => $arrProperties) { $namespace = new FlashMessengerNamespace( - $configNamespace, + (string) $configNamespace, isset($arrProperties['classes']) ? (string) $arrProperties['classes'] : '' ); if (isset($arrProperties['message_open_format'])) { From be145abc5794eeddab5d1259ce3ddbb0dc06ee66 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Thu, 23 Jun 2022 20:18:05 +0300 Subject: [PATCH 07/13] adds docblock and improves conditions --- src/View/Helper/FlashMessenger.php | 99 ++++++++++++++++++------------ 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index e974fde..2287b94 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -89,8 +89,14 @@ class FlashMessenger extends AbstractHelper */ protected $pluginFlashMessenger; - /** @var array */ - protected $namespaces = []; + /** + * All namespaces of FlashMessenger + * + * Keys of array are the namespace names + * + * @var array $namespaces + */ + private $namespaces = []; /** * @param array $namespaces @@ -253,14 +259,16 @@ public function getAutoEscape() * @param string $messageCloseString * @return FlashMessenger */ - public function setMessageCloseString($messageCloseString, string $namespace = 'default') + public function setMessageCloseString($messageCloseString, ?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - $objNamespace->setMessageCloseString($messageCloseString); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { $this->messageCloseString = $messageCloseString; + } else { + $namespace->setMessageCloseString($messageCloseString); } + return $this; } @@ -269,14 +277,15 @@ public function setMessageCloseString($messageCloseString, string $namespace = ' * * @return string */ - public function getMessageCloseString(string $namespace = 'default') + public function getMessageCloseString(?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - return $objNamespace->getMessageCloseString(); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { return $this->messageCloseString; } + + return $namespace->getMessageCloseString(); } /** @@ -285,13 +294,14 @@ public function getMessageCloseString(string $namespace = 'default') * @param string $messageOpenFormat * @return FlashMessenger */ - public function setMessageOpenFormat($messageOpenFormat, string $namespace = 'default') + public function setMessageOpenFormat($messageOpenFormat, ?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - $objNamespace->setMessageOpenFormat($messageOpenFormat); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { $this->messageOpenFormat = $messageOpenFormat; + } else { + $namespace->setMessageOpenFormat($messageOpenFormat); } return $this; } @@ -301,14 +311,15 @@ public function setMessageOpenFormat($messageOpenFormat, string $namespace = 'de * * @return string */ - public function getMessageOpenFormat(string $namespace = 'default') + public function getMessageOpenFormat(?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - return $objNamespace->getMessageOpenFormat(); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { return $this->messageOpenFormat; } + + return $namespace->getMessageOpenFormat(); } /** @@ -317,13 +328,14 @@ public function getMessageOpenFormat(string $namespace = 'default') * @param string $messageSeparatorString * @return FlashMessenger */ - public function setMessageSeparatorString($messageSeparatorString, string $namespace = 'default') + public function setMessageSeparatorString($messageSeparatorString, ?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - $objNamespace->setMessageSeparatorString($messageSeparatorString); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { $this->messageSeparatorString = $messageSeparatorString; + } else { + $namespace->setMessageSeparatorString($messageSeparatorString); } return $this; } @@ -333,14 +345,15 @@ public function setMessageSeparatorString($messageSeparatorString, string $names * * @return string */ - public function getMessageSeparatorString(string $namespace = 'default') + public function getMessageSeparatorString(?string $namespaceName = null) { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - return $objNamespace->getMessageSeparatorString(); - } else { + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { return $this->messageSeparatorString; } + + return $namespace->getMessageSeparatorString(); } /** @@ -413,13 +426,23 @@ private function getNamespace(string $namespace): ?FlashMessengerNamespace return $this->namespaces[$namespace] ?? null; } - private function getClasses(string $namespace): string + private function getClasses(string $namespaceName): string { - $objNamespace = $this->getNamespace($namespace); - if ($objNamespace !== null) { - return $objNamespace->getClasses(); - } else { - return $this->classMessages[$namespace] ?? ''; + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + return $this->classMessages[$namespaceName] ?? ''; } + + return $namespace->getClasses(); + } + + /** + * Returns default namespace name + * + * @return string + */ + private function getDefaultNamespaceName() : string + { + return \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::NAMESPACE_DEFAULT; } } From 3b51d9f14e4cbb57b1d555437894729cce02d594 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Thu, 23 Jun 2022 20:20:01 +0300 Subject: [PATCH 08/13] adds doc block and took out array dimensional checking code to another function --- src/View/Helper/FlashMessengerFactory.php | 40 ++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 573ea15..7949a7f 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -34,16 +34,10 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = $config = $container->get('config'); if (isset($config['view_helper_config']['flashmessenger'])) { $configHelper = (array) $config['view_helper_config']['flashmessenger']; - $isArrayOneDimensial = true; - /** @var string|array $property */ - foreach ($configHelper as $property) { - if (is_array($property)) { - $isArrayOneDimensial = false; - break; - } - } - if ($isArrayOneDimensial === true) { + $isArrayOneDimensional = $this->isArrayOneDimensional($configHelper); + + if ($isArrayOneDimensional === true) { return $this->createHelperWithOldConfig($flashMessenger, $configHelper); } else { return $this->createHelperWithActualConfig($flashMessenger, $configHelper); @@ -55,7 +49,9 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = /** * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger - * @param array $configHelper + * @param array $configHelper { + * message_open_format?:string, message_close_string?:string, message_separator_string?:string + * } * @return FlashMessenger */ private function createHelperWithOldConfig($flashMessenger, $configHelper) @@ -87,7 +83,11 @@ private function createHelperWithOldConfig($flashMessenger, $configHelper) private function createHelperWithActualConfig($flashMessenger, $configHelper) { $namespaces = []; - /** @var array $arrProperties */ + /** + * @var array $arrProperties { + * classes?:string, message_open_format?:string, message_close_string?:string, message_separator_string?:string + * } + */ foreach ($configHelper as $configNamespace => $arrProperties) { $namespace = new FlashMessengerNamespace( (string) $configNamespace, @@ -113,6 +113,24 @@ private function createHelperWithActualConfig($flashMessenger, $configHelper) return $helper; } + /** + * @param array $array + */ + private function isArrayOneDimensional(array $array): bool + { + $isArrayOneDimensional = true; + + /** @var string|array $property */ + foreach ($array as $property) { + if (is_array($property)) { + $isArrayOneDimensional = false; + break; + } + } + + return $isArrayOneDimensional; + } + /** * Create service (v2) * From 41880d35b8c491bba73cb66a8b2146403cbf19a2 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Thu, 23 Jun 2022 20:21:13 +0300 Subject: [PATCH 09/13] adds doc bloc. makes namespace class @internal Signed-off-by: Aleksandr --- src/View/Helper/FlashMessengerNamespace.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/View/Helper/FlashMessengerNamespace.php b/src/View/Helper/FlashMessengerNamespace.php index 8c4270c..c06346a 100644 --- a/src/View/Helper/FlashMessengerNamespace.php +++ b/src/View/Helper/FlashMessengerNamespace.php @@ -4,6 +4,11 @@ namespace Laminas\Mvc\Plugin\FlashMessenger\View\Helper; +/** + * Flashmessenger namespace + * + * @internal + */ class FlashMessengerNamespace { /** @@ -12,7 +17,7 @@ class FlashMessengerNamespace private string $name; /** - * Default attributes for the open format tag + * String of css classes to be attached */ private string $classes = ''; From 58b4007a9b1c4bddce8ddfefc6073590e14880a6 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Thu, 23 Jun 2022 20:54:13 +0300 Subject: [PATCH 10/13] adds test Signed-off-by: Aleksandr --- test/View/Helper/FlashMessengerTest.php | 47 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php index b9500e1..16ffcff 100644 --- a/test/View/Helper/FlashMessengerTest.php +++ b/test/View/Helper/FlashMessengerTest.php @@ -568,7 +568,7 @@ public function testRenderCurrentUsesCurrentAutoEscapeByDefault(): void /** * @covers \Laminas\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent */ - public function testNamespacesMessageFormatIsDifferent(): void + public function testNamespaceMessageFormatByConfig(): void { $this->seedMessages(); @@ -581,12 +581,6 @@ public function testNamespacesMessageFormatIsDifferent(): void 'message_close_string' => '
  • ', 'classes' => 'foo-baz foo-bar', ], - 'info' => [ - 'message_open_format' => '
      ', - 'message_separator_string' => '', - 'message_close_string' => '
    ', - 'classes' => 'foo-bar foo-baz', - ], ], ], ]; @@ -597,13 +591,7 @@ public function testNamespacesMessageFormatIsDifferent(): void . '
  • foo
  • ' . '
  • bar
  • ' . ''; - $displayInfo = $helper->renderCurrent('default'); - $this->assertEquals($displayInfoAssertion, $displayInfo); - - $displayInfoAssertion = '
      ' - . '
    • bar-info
    • ' - . '
    '; - $displayInfo = $helper->renderCurrent('info'); + $displayInfo = $helper->renderCurrent(); $this->assertEquals($displayInfoAssertion, $displayInfo); $displayInfoAssertion = '
      ' @@ -612,4 +600,35 @@ public function testNamespacesMessageFormatIsDifferent(): void $displayInfo = $helper->renderCurrent('warning'); $this->assertEquals($displayInfoAssertion, $displayInfo); } + + /** + * @covers \Laminas\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testCustomNamespaceMessageFormatByConfig(): void + { + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'foo-bar' => [ + 'message_open_format' => '
        ', + 'message_separator_string' => '', + 'message_close_string' => '
      ', + 'classes' => 'foo-bar foo-bar', + ], + ], + ], + ]; + $services = $this->createServiceManager($config); + $helper = $this->retrieveViewHelperFrom($services); + + $helper->getPluginFlashMessenger()->addMessage('foo', 'foo-bar'); + $helper->getPluginFlashMessenger()->addMessage('bar', 'foo-bar'); + + $displayAssertion = '
        ' + . '
      • foo
      • ' + . '
      • bar
      • ' + . '
      '; + $display = $helper->renderCurrent('foo-bar'); + $this->assertEquals($displayAssertion, $display); + } } From 5473d177ef172fe9fcbbdca737ca241e0d38e639 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Thu, 23 Jun 2022 20:55:10 +0300 Subject: [PATCH 11/13] csfix Signed-off-by: Aleksandr --- src/View/Helper/FlashMessenger.php | 18 ++++++++---------- src/View/Helper/FlashMessengerFactory.php | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index 2287b94..0be1ce4 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -262,7 +262,7 @@ public function getAutoEscape() public function setMessageCloseString($messageCloseString, ?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { $this->messageCloseString = $messageCloseString; } else { @@ -280,7 +280,7 @@ public function setMessageCloseString($messageCloseString, ?string $namespaceNam public function getMessageCloseString(?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { return $this->messageCloseString; } @@ -297,7 +297,7 @@ public function getMessageCloseString(?string $namespaceName = null) public function setMessageOpenFormat($messageOpenFormat, ?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { $this->messageOpenFormat = $messageOpenFormat; } else { @@ -314,7 +314,7 @@ public function setMessageOpenFormat($messageOpenFormat, ?string $namespaceName public function getMessageOpenFormat(?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { return $this->messageOpenFormat; } @@ -331,7 +331,7 @@ public function getMessageOpenFormat(?string $namespaceName = null) public function setMessageSeparatorString($messageSeparatorString, ?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { $this->messageSeparatorString = $messageSeparatorString; } else { @@ -348,7 +348,7 @@ public function setMessageSeparatorString($messageSeparatorString, ?string $name public function getMessageSeparatorString(?string $namespaceName = null) { $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); - $namespace = $this->getNamespace($namespaceName); + $namespace = $this->getNamespace($namespaceName); if ($namespace === null) { return $this->messageSeparatorString; } @@ -438,11 +438,9 @@ private function getClasses(string $namespaceName): string /** * Returns default namespace name - * - * @return string */ - private function getDefaultNamespaceName() : string + private function getDefaultNamespaceName(): string { - return \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::NAMESPACE_DEFAULT; + return PluginFlashMessenger::NAMESPACE_DEFAULT; } } diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 7949a7f..8bed9c4 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -33,7 +33,7 @@ public function __invoke(ContainerInterface $container, $name, ?array $options = $config = $container->get('config'); if (isset($config['view_helper_config']['flashmessenger'])) { - $configHelper = (array) $config['view_helper_config']['flashmessenger']; + $configHelper = (array) $config['view_helper_config']['flashmessenger']; $isArrayOneDimensional = $this->isArrayOneDimensional($configHelper); From 873e9b17ffd6092ce84deb9289281e4b77d91101 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Mon, 27 Jun 2022 17:50:14 +0300 Subject: [PATCH 12/13] adds native type definition Signed-off-by: Aleksandr --- src/View/Helper/FlashMessenger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index 0be1ce4..c861c77 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -96,7 +96,7 @@ class FlashMessenger extends AbstractHelper * * @var array $namespaces */ - private $namespaces = []; + private array $namespaces = []; /** * @param array $namespaces From b74a1c5e448937c9e099fe314ed24418947bdf54 Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Mon, 27 Jun 2022 17:51:10 +0300 Subject: [PATCH 13/13] improves check of array whether multidimensional Signed-off-by: Aleksandr --- src/View/Helper/FlashMessengerFactory.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 8bed9c4..2aed9cf 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -118,17 +118,14 @@ private function createHelperWithActualConfig($flashMessenger, $configHelper) */ private function isArrayOneDimensional(array $array): bool { - $isArrayOneDimensional = true; - - /** @var string|array $property */ + /** @var mixed $property */ foreach ($array as $property) { if (is_array($property)) { - $isArrayOneDimensional = false; - break; + return false; } } - return $isArrayOneDimensional; + return true; } /**