Messages
Class · Sharkord\Collections\Messages
Class Messages
A bounded, array-accessible, iterable cache of Message objects keyed by message ID (string).
Owns all cache storage and eviction logic so that MessageManager can focus solely on API interactions. When the cache reaches its size limit the oldest inserted entry is evicted (FIFO) to make room for the new one. PHP arrays preserve insertion order, so eviction is a cheap reset()/key()/unset().
The collection is intentionally write-protected from outside callers — ArrayAccess mutations throw a LogicException. All writes go through the explicit add(), update(), and remove() methods so eviction is always applied.
Examples
Section titled “Examples”// Retrieve a cached message directly (no API call)$message = $sharkord->messages->getFromCache(609);
// Iterate all cached messagesforeach ($sharkord->messages->collection() as $id => $message) { echo "{$id}: {$message->content}\n";}
// Check how many messages are currently cachedecho $sharkord->messages->count();Methods
Section titled “Methods”__construct()
Section titled “__construct()”Messages constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sharkord | Sharkord | Reference to the main bot instance. |
$maxSize (optional) | int | Maximum number of messages to hold in memory at once. When the limit is reached the oldest entry is evicted. Defaults to 500. |
Retrieves a cached message by ID without hitting the API.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | `int | string` |
Returns \Message|null — The cached Message model, or null if not found.
cached()
Section titled “cached()”Returns all currently cached messages as an array keyed by message ID.
Returns array<string,\Message>
offsetExists()
Section titled “offsetExists()”Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The message ID. |
Returns bool
offsetGet()
Section titled “offsetGet()”Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The message ID. |
Returns \Message|null
offsetSet()
Section titled “offsetSet()”Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | |
$value | mixed |
Throws
\LogicException— Message collections are read-only via array access.
offsetUnset()
Section titled “offsetUnset()”Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed |
Throws
\LogicException— Message collections are read-only via array access.
count()
Section titled “count()”Returns the number of messages currently held in the cache.
Returns int
getIterator()
Section titled “getIterator()”Returns \ArrayIterator<string,\Message>