63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
class MessageHandler {
|
|
private $messages = [
|
|
'error' => [],
|
|
'warning' => [],
|
|
'notice' => [],
|
|
'success' => []
|
|
];
|
|
|
|
// Add a message of a specific type
|
|
public function addMessage($type, $message) {
|
|
if (!isset($this->messages[$type])) {
|
|
throw new Exception("Invalid message type: $type");
|
|
}
|
|
$this->messages[$type][] = $message;
|
|
}
|
|
|
|
// Get all messages of a specific type
|
|
public function getMessages($type) {
|
|
return $this->messages[$type] ?? [];
|
|
}
|
|
|
|
// Get all messages of all types
|
|
public function getAllMessages() {
|
|
return $this->messages;
|
|
}
|
|
|
|
// Get the count of messages for a specific type
|
|
public function count($type) {
|
|
return isset($this->messages[$type]) ? count($this->messages[$type]) : 0;
|
|
}
|
|
|
|
// Get the total count of all messages
|
|
public function totalCount() {
|
|
return array_sum(array_map('count', $this->messages));
|
|
}
|
|
|
|
// Check if there are any messages of a specific type
|
|
public function hasMessages($type) {
|
|
return !empty($this->messages[$type]);
|
|
}
|
|
|
|
// Check if there are any messages at all
|
|
public function hasAnyMessages() {
|
|
return $this->totalCount() > 0;
|
|
}
|
|
|
|
// Clear messages of a specific type
|
|
public function clear($type) {
|
|
if (isset($this->messages[$type])) {
|
|
$this->messages[$type] = [];
|
|
}
|
|
}
|
|
|
|
// Clear all messages
|
|
public function clearAll() {
|
|
foreach ($this->messages as $type => $list) {
|
|
$this->messages[$type] = [];
|
|
}
|
|
}
|
|
}
|