Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessageList.php
Go to the documentation of this file.
1 <?php
8 
17 {
23  protected $_messageClasses;
24 
30  protected $_messages;
31 
36  public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $messages = [])
37  {
38  $this->_objectManager = $objectManager;
39  $this->_messageClasses = $messages;
40  }
41 
49  protected function _loadMessages()
50  {
51  if (!empty($this->_messages)) {
52  return;
53  }
54  foreach ($this->_messageClasses as $key => $messageClass) {
55  if (!$messageClass) {
56  throw new \InvalidArgumentException('Message class for message "' . $key . '" is not set');
57  }
58  $message = $this->_objectManager->get($messageClass);
59  if ($message instanceof \Magento\Framework\Notification\MessageInterface) {
60  $this->_messages[$message->getIdentity()] = $message;
61  } else {
62  throw new \UnexpectedValueException("Message class has to implement the message interface.");
63  }
64  }
65  }
66 
73  public function getMessageByIdentity($identity)
74  {
75  $this->_loadMessages();
76  return $this->_messages[$identity] ?? null;
77  }
78 
84  public function asArray()
85  {
86  $this->_loadMessages();
87  return $this->_messages;
88  }
89 }
$objectManager
Definition: bootstrap.php:17
$message
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, $messages=[])
Definition: MessageList.php:36