mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-24 13:36:50 +00:00
Added getUpdates
This commit is contained in:
@@ -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')
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user