88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
namespace Novaconium;
|
|
class MessageHandler {
|
|
private $messages = [
|
|
'error' => [],
|
|
'warning' => [],
|
|
'notice' => [],
|
|
'success' => []
|
|
];
|
|
|
|
public function __construct(array $sessionMessages = [])
|
|
{
|
|
// Merge existing session messages into the default structure
|
|
foreach ($this->messages as $type => $_) {
|
|
if (isset($sessionMessages[$type]) && is_array($sessionMessages[$type])) {
|
|
$this->messages[$type] = $sessionMessages[$type];
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
public function error($message){
|
|
$this->addMessage('error', $message);
|
|
}
|
|
|
|
public function notice($message){
|
|
$this->addMessage('notice', $message);
|
|
}
|
|
|
|
// Get all messages of a specific type
|
|
public function getMessages($type) {
|
|
return $this->messages[$type] ?? [];
|
|
}
|
|
|
|
// Get all messages of a specific type
|
|
public function showMessages($type) {
|
|
$result = $this->messages[$type] ?? [];
|
|
$this->messages[$type] = []; // Clear messages after showing
|
|
return $result;
|
|
}
|
|
|
|
// 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] = [];
|
|
}
|
|
}
|
|
}
|