diff --git a/src/Api.php b/src/Api.php index 226d06a..425722b 100644 --- a/src/Api.php +++ b/src/Api.php @@ -8,7 +8,9 @@ use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException; use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException; use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException; use BushlanovDev\MaxMessengerBot\Models\BotInfo; +use BushlanovDev\MaxMessengerBot\Models\ResultModel; use BushlanovDev\MaxMessengerBot\Models\Subscription; +use BushlanovDev\MaxMessengerBot\Models\SubscriptionRequestBody; use InvalidArgumentException; /** @@ -20,7 +22,7 @@ use InvalidArgumentException; class Api { private const string METHOD_GET = 'GET'; - // private const string METHOD_POST = 'POST'; + private const string METHOD_POST = 'POST'; // private const string METHOD_DELETE = 'DELETE'; private const string ACTION_ME = '/me'; @@ -84,4 +86,27 @@ class Api $this->client->request(self::METHOD_GET, self::ACTION_SUBSCRIPTIONS) ); } + + /** + * Subscribes the bot to receive updates via WebHook. + * + * @param SubscriptionRequestBody $body + * + * @return ResultModel + * + * @throws ClientApiException + * @throws NetworkException + * @throws SerializationException + */ + public function subscribe(SubscriptionRequestBody $body): ResultModel + { + return $this->modelFactory->createResult( + $this->client->request( + self::METHOD_POST, + self::ACTION_SUBSCRIPTIONS, + [], + $body->toArray(), + ) + ); + } } diff --git a/src/ModelFactory.php b/src/ModelFactory.php index e9df7d5..a839116 100644 --- a/src/ModelFactory.php +++ b/src/ModelFactory.php @@ -6,6 +6,7 @@ namespace BushlanovDev\MaxMessengerBot; use BushlanovDev\MaxMessengerBot\Models\BotCommand; use BushlanovDev\MaxMessengerBot\Models\BotInfo; +use BushlanovDev\MaxMessengerBot\Models\ResultModel; use BushlanovDev\MaxMessengerBot\Models\Subscription; /** @@ -13,6 +14,18 @@ use BushlanovDev\MaxMessengerBot\Models\Subscription; */ class ModelFactory { + /** + * Simple response to request. + * + * @param array $data + * + * @return ResultModel + */ + public function createResult(array $data): ResultModel + { + return ResultModel::fromArray($data); + } + /** * Information about the current bot. * diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index 35e5c9e..9f6cad7 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -16,6 +16,25 @@ abstract readonly class AbstractModel */ public function toArray(): array { - return get_object_vars($this); + return array_map(function ($value) { + return $this->convertValue($value); + }, get_object_vars($this)); + } + + private function convertValue(mixed $value): mixed + { + if ($value instanceof AbstractModel) { + return $value->toArray(); + } + + if (is_array($value)) { + return array_map([$this, 'convertValue'], $value); + } + + if ($value instanceof \BackedEnum) { + return $value->value; + } + + return $value; } } diff --git a/src/Models/ResultModel.php b/src/Models/ResultModel.php new file mode 100644 index 0000000..71a74c3 --- /dev/null +++ b/src/Models/ResultModel.php @@ -0,0 +1,32 @@ + UpdateType::from($typeValue), + fn(string $typeValue): UpdateType => UpdateType::from($typeValue), $data['update_types'], ); } diff --git a/src/Models/SubscriptionRequestBody.php b/src/Models/SubscriptionRequestBody.php new file mode 100644 index 0000000..c98c5f3 --- /dev/null +++ b/src/Models/SubscriptionRequestBody.php @@ -0,0 +1,45 @@ + UpdateType::from($typeValue), + $data['update_types'], + ); + } + + return new static( + (string)$data['url'], + $data['secret'] ?? null, + $updateTypes, + $data['version'] ?? null, + ); + } +}