functions and classes added.

This commit is contained in:
2025-03-20 18:27:17 -07:00
parent 0c41ca9b65
commit 28513d367d
8 changed files with 250 additions and 72 deletions

62
src/MessageHandler.php Normal file
View File

@@ -0,0 +1,62 @@
<?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] = [];
}
}
}