Added getUpdates

This commit is contained in:
Alex
2025-07-21 18:49:14 +03:00
parent 07c6bb35b4
commit 4c988c9005
13 changed files with 653 additions and 7 deletions
+51
View File
@@ -4,12 +4,18 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
use BushlanovDev\MaxMessengerBot\Models\Updates\AbstractUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use LogicException;
use ReflectionException;
/**
@@ -109,4 +115,49 @@ class ModelFactory
{
return Chat::fromArray($data);
}
/**
* Creates a list of updates from a raw API response.
*
* @param array<string, mixed> $data Raw response data.
*
* @return UpdateList
* @throws ReflectionException
* @throws LogicException
*/
public function createUpdateList(array $data): UpdateList
{
$updateObjects = [];
if (isset($data['updates']) && is_array($data['updates'])) {
foreach ($data['updates'] as $updateData) {
// Here we delegate the creation of a specific update to another factory method
$updateObjects[] = $this->createUpdate($updateData);
}
}
return new UpdateList(
$updateObjects,
$data['marker'] ?? null,
);
}
/**
* Creates a specific Update model based on the 'update_type' field.
*
* @param array<string, mixed> $data Raw data for a single update.
*
* @return AbstractUpdate
* @throws ReflectionException
* @throws LogicException
*/
public function createUpdate(array $data): AbstractUpdate
{
return match (UpdateType::tryFrom($data['update_type'] ?? '')) {
UpdateType::MessageCreated => MessageCreatedUpdate::fromArray($data),
UpdateType::BotStarted => BotStartedUpdate::fromArray($data),
default => throw new LogicException(
'Unknown or unsupported update type received: ' . ($data['update_type'] ?? 'none')
),
};
}
}