MessageConfigurationsPool.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\Message;
  7. class MessageConfigurationsPool
  8. {
  9. /**
  10. * @var array
  11. */
  12. private $configurationsMap;
  13. /**
  14. * Expected input:
  15. * ['message_identifier' => ['renderer' => '{renderer_code}', 'data' => []], ...]
  16. *
  17. * @param array $configurationsMap
  18. */
  19. public function __construct(
  20. array $configurationsMap = []
  21. ) {
  22. array_walk(
  23. $configurationsMap,
  24. function (array &$configuration) {
  25. if (!isset($configuration['renderer'])
  26. || !is_string($configuration['renderer'])
  27. ) {
  28. throw new \InvalidArgumentException('Renderer should be defined.');
  29. }
  30. if (isset($configuration['data'])
  31. && !is_array($configuration['data'])
  32. ) {
  33. throw new \InvalidArgumentException('Data should be of array type.');
  34. }
  35. if (!isset($configuration['data'])) {
  36. $configuration['data'] = [];
  37. }
  38. }
  39. );
  40. $this->configurationsMap = $configurationsMap;
  41. }
  42. /**
  43. * Returns message configuration as
  44. * ['message_identifier' => ['renderer' => '{renderer_code}', 'data' => []], ...]
  45. *
  46. * @param string $identifier
  47. * @return null|array
  48. */
  49. public function getMessageConfiguration($identifier)
  50. {
  51. return !isset($this->configurationsMap[$identifier])
  52. ? null
  53. : $this->configurationsMap[$identifier];
  54. }
  55. }