diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index 052440f..c861c77 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -89,6 +89,25 @@ class FlashMessenger extends AbstractHelper */ protected $pluginFlashMessenger; + /** + * All namespaces of FlashMessenger + * + * Keys of array are the namespace names + * + * @var array $namespaces + */ + private array $namespaces = []; + + /** + * @param array $namespaces + */ + public function __construct(array $namespaces = []) + { + foreach ($namespaces as $namespace) { + $this->namespaces[$namespace->getName()] = $namespace; + } + } + /** * Returns the flash messenger plugin controller * @@ -169,12 +188,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 +222,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 +259,16 @@ public function getAutoEscape() * @param string $messageCloseString * @return FlashMessenger */ - public function setMessageCloseString($messageCloseString) + public function setMessageCloseString($messageCloseString, ?string $namespaceName = null) { - $this->messageCloseString = (string) $messageCloseString; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + $this->messageCloseString = $messageCloseString; + } else { + $namespace->setMessageCloseString($messageCloseString); + } + return $this; } @@ -256,9 +277,15 @@ public function setMessageCloseString($messageCloseString) * * @return string */ - public function getMessageCloseString() + public function getMessageCloseString(?string $namespaceName = null) { - return $this->messageCloseString; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + return $this->messageCloseString; + } + + return $namespace->getMessageCloseString(); } /** @@ -267,9 +294,15 @@ public function getMessageCloseString() * @param string $messageOpenFormat * @return FlashMessenger */ - public function setMessageOpenFormat($messageOpenFormat) + public function setMessageOpenFormat($messageOpenFormat, ?string $namespaceName = null) { - $this->messageOpenFormat = (string) $messageOpenFormat; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + $this->messageOpenFormat = $messageOpenFormat; + } else { + $namespace->setMessageOpenFormat($messageOpenFormat); + } return $this; } @@ -278,9 +311,15 @@ public function setMessageOpenFormat($messageOpenFormat) * * @return string */ - public function getMessageOpenFormat() + public function getMessageOpenFormat(?string $namespaceName = null) { - return $this->messageOpenFormat; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + return $this->messageOpenFormat; + } + + return $namespace->getMessageOpenFormat(); } /** @@ -289,9 +328,15 @@ public function getMessageOpenFormat() * @param string $messageSeparatorString * @return FlashMessenger */ - public function setMessageSeparatorString($messageSeparatorString) + public function setMessageSeparatorString($messageSeparatorString, ?string $namespaceName = null) { - $this->messageSeparatorString = (string) $messageSeparatorString; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + $this->messageSeparatorString = $messageSeparatorString; + } else { + $namespace->setMessageSeparatorString($messageSeparatorString); + } return $this; } @@ -300,9 +345,15 @@ public function setMessageSeparatorString($messageSeparatorString) * * @return string */ - public function getMessageSeparatorString() + public function getMessageSeparatorString(?string $namespaceName = null) { - return $this->messageSeparatorString; + $namespaceName = $namespaceName ?? $this->getDefaultNamespaceName(); + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + return $this->messageSeparatorString; + } + + return $namespace->getMessageSeparatorString(); } /** @@ -369,4 +420,27 @@ protected function getEscapeHtmlHelper() return $this->escapeHtmlHelper; } + + private function getNamespace(string $namespace): ?FlashMessengerNamespace + { + return $this->namespaces[$namespace] ?? null; + } + + private function getClasses(string $namespaceName): string + { + $namespace = $this->getNamespace($namespaceName); + if ($namespace === null) { + return $this->classMessages[$namespaceName] ?? ''; + } + + return $namespace->getClasses(); + } + + /** + * Returns default namespace name + */ + private function getDefaultNamespaceName(): string + { + return PluginFlashMessenger::NAMESPACE_DEFAULT; + } } diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 24dfdf6..2aed9cf 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -8,6 +8,7 @@ use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; +use function is_array; use function method_exists; class FlashMessengerFactory implements FactoryInterface @@ -25,29 +26,108 @@ 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'); - $helper->setPluginFlashMessenger($flashMessenger); + $controllerPluginManager = $container->get('ControllerPluginManager'); + /** @var \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger */ + $flashMessenger = $controllerPluginManager->get('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 = (array) $config['view_helper_config']['flashmessenger']; + + $isArrayOneDimensional = $this->isArrayOneDimensional($configHelper); + + if ($isArrayOneDimensional === true) { + return $this->createHelperWithOldConfig($flashMessenger, $configHelper); + } else { + return $this->createHelperWithActualConfig($flashMessenger, $configHelper); + } + } + + return new FlashMessenger(); + } + + /** + * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger + * @param array $configHelper { + * message_open_format?:string, message_close_string?:string, message_separator_string?:string + * } + * @return FlashMessenger + */ + private function createHelperWithOldConfig($flashMessenger, $configHelper) + { + $helper = new FlashMessenger(); + $helper->setPluginFlashMessenger($flashMessenger); + + if (isset($configHelper['message_open_format'])) { + $helper + ->setMessageOpenFormat((string) $configHelper['message_open_format']); + } + if (isset($configHelper['message_separator_string'])) { + $helper + ->setMessageSeparatorString((string) $configHelper['message_separator_string']); + } + if (isset($configHelper['message_close_string'])) { + $helper + ->setMessageCloseString((string) $configHelper['message_close_string']); + } + + return $helper; + } + + /** + * @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger + * @param array $configHelper + * @return FlashMessenger + */ + private function createHelperWithActualConfig($flashMessenger, $configHelper) + { + $namespaces = []; + /** + * @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, + 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; } + /** + * @param array $array + */ + private function isArrayOneDimensional(array $array): bool + { + /** @var mixed $property */ + foreach ($array as $property) { + if (is_array($property)) { + return false; + } + } + + return true; + } + /** * Create service (v2) * diff --git a/src/View/Helper/FlashMessengerNamespace.php b/src/View/Helper/FlashMessengerNamespace.php new file mode 100644 index 0000000..c06346a --- /dev/null +++ b/src/View/Helper/FlashMessengerNamespace.php @@ -0,0 +1,80 @@ +'; + 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; + } +} diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php index 15f1641..16ffcff 100644 --- a/test/View/Helper/FlashMessengerTest.php +++ b/test/View/Helper/FlashMessengerTest.php @@ -564,4 +564,71 @@ public function testRenderCurrentUsesCurrentAutoEscapeByDefault(): void $display = $this->helper->renderCurrent('default'); $this->assertSame($displayAssertion, $display); } + + /** + * @covers \Laminas\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testNamespaceMessageFormatByConfig(): void + { + $this->seedMessages(); + + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'default' => [ + 'message_open_format' => '
      ', + 'message_separator_string' => '', + 'message_close_string' => '
    ', + 'classes' => 'foo-baz foo-bar', + ], + ], + ], + ]; + $services = $this->createServiceManager($config); + $helper = $this->retrieveViewHelperFrom($services); + + $displayInfoAssertion = '
      ' + . '
    • foo
    • ' + . '
    • bar
    • ' + . '
    '; + $displayInfo = $helper->renderCurrent(); + $this->assertEquals($displayInfoAssertion, $displayInfo); + + $displayInfoAssertion = '
      ' + . '
    • bar-warning
    • ' + . '
    '; + $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); + } }