<?php
namespace App; class MessagesCollector { protected $messages = []; protected $hasType = []; final public function __construct() { } static public function create() { return new static(); } public function serialize() { return serialize( ['messages'=>$this->messages, 'hasType' => $this->hasType] ); } public function unserialize($serialized) { $arr = unserialize($serialized); $this->messages = $arr['messages']; $this->hasType = $arr['hasType']; return $this; } public function addMessage(\App\Message $message) { $this->messages[] = $message; $this->hasType[$message->getType()] = true; return $this; } public function addError($text, $code=null) { return $this->addMessage( \App\Message::create('error', $text, $code) ); } public function addWarning($text, $code=null) { return $this->addMessage( \App\Message::create('warning', $text, $code) ); } public function addInfo($text, $code=null) { return $this->addMessage( \App\Message::create('info', $text, $code) ); } public function addSuccess($text, $code=null) { return $this->addMessage( \App\Message::create('success', $text, $code) ); } public function hasError() { return (bool) $this->hasType['error']; } public function hasErrorOrWarning() { return ($this->hasType['error'] || $this->hasType['warning']); } public function getMessages() { return $this->messages; } public function groupMessagesByType() { $groupedMessages = []; foreach($this->messages as $msg) { $msgKey = $msg->getText(); if(!$groupedMessages[$msg->getType()]) { $groupedMessages[$msg->getType()] = [$msgKey=>1]; } else { $groupedMessages[$msg->getType()][$msgKey]++; } } $messages = []; foreach($groupedMessages as $type=>$messageList) { $msg = ''; foreach($messageList as $text=>$repeats) { $repeatTimes = ($repeats>1) ? ' (x'.$repeats.')' : ''; $msg .= sprintf("%s%s\n", $text, $repeatTimes); } $messages[] = \App\Message::create($type,trim($msg)); } $this->messages = $messages; return $this; } public function groupMessagesByTypeAndText() { $groupedMessages = []; foreach ($this->messages as $mes) { $key = sprintf("%s-%s", $mes->getType(), $mes->getText()); if (!$groupedMessages[$key]) { $groupedMessages[$key] = []; } $groupedMessages[$key][]= $mes; } $messages = []; foreach ($groupedMessages as $key=>$messageList) { if (count($messageList)>1) { $messages[]= \App\Message::create($messageList[0]->getType(), $messageList[0]->getText().' (x'.count($messageList).')'); } else { $messages[]= $messageList[0]; } } $this->messages = $messages; return $this; } public function getMessagesCount() { return count($this->messages); } public function getMessagesCountByType($type) { $i = 0; foreach($this->messages as $msg) { if($msg->getType()==$type) { $i++; } } return $i; } public function appendMessages(\App\MessagesCollector $msgs, $format=null) { foreach ($msgs->getMessages() as $msg) { if ($format and strpos($format, '%s')!==false) { $msg->setText( sprintf($format, $msg->getText()) ); } $this->addMessage($msg); } return $this; } public function getMessageBulk(array $types=[]) :string { $messages = []; foreach($this->messages as $msg) { if (!$types or in_array($msg->getType(), $types)) { $messages[]=$msg->getTextWithField(); } } return implode(' ', $messages); } public function removeSuccess() { foreach ($this->messages as $idx=>$msg) { if ($msg->getType()=='success') { unset($this->messages[$idx]); } } $this->hasType['success'] = false; return $this; } public function removeByCallback($filterCb) { foreach ($this->messages as $idx=>$msg) { if ($filterCb($msg, $idx)) { unset($this->messages[$idx]); } } $this->hasType = []; foreach ($this->messages as $msg) { $this->hasType[$msg->getType()] = true; } return $this; } } 