Skip to content

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.

// Retrieve a cached message directly (no API call)
$message = $sharkord->messages->getFromCache(609);
// Iterate all cached messages
foreach ($sharkord->messages->collection() as $id => $message) {
echo "{$id}: {$message->content}\n";
}
// Check how many messages are currently cached
echo $sharkord->messages->count();

Messages constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordReference to the main bot instance.
$maxSize (optional)intMaximum 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

ParameterTypeDescription
$id`intstring`

Returns \Message|null — The cached Message model, or null if not found.


Returns all currently cached messages as an array keyed by message ID.

Returns array<string,\Message>


Parameters

ParameterTypeDescription
$offsetmixedThe message ID.

Returns bool


Parameters

ParameterTypeDescription
$offsetmixedThe message ID.

Returns \Message|null


Parameters

ParameterTypeDescription
$offsetmixed
$valuemixed

Throws

  • \LogicException — Message collections are read-only via array access.

Parameters

ParameterTypeDescription
$offsetmixed

Throws

  • \LogicException — Message collections are read-only via array access.

Returns the number of messages currently held in the cache.

Returns int


Returns \ArrayIterator<string,\Message>