Skip to content

Commit

Permalink
#21 added normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
greenplugin committed Feb 26, 2020
1 parent b032509 commit a58f7d2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/BotApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
use TgBotApi\BotApiBase\Normalizer\LegacyObjectNormalizerWrapper;
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
use TgBotApi\BotApiBase\Normalizer\PollNormalizer;
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;

/**
Expand Down Expand Up @@ -73,6 +74,7 @@ public function normalize($method): BotApiRequestInterface
}

$serializer = new Serializer([
new PollNormalizer($objectNormalizer),
new InputFileNormalizer($files),
new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
new JsonSerializableNormalizer($objectNormalizer),
Expand Down
55 changes: 55 additions & 0 deletions src/Normalizer/PollNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Normalizer;

use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use TgBotApi\BotApiBase\Method\SendPollMethod;

class PollNormalizer implements NormalizerInterface
{
/**
* @var NormalizerInterface
*/
private $objectNormalizer;

/**
* JsonSerializableNormalizer constructor.
*/
public function __construct(NormalizerInterface $objectNormalizer)
{
$this->objectNormalizer = $objectNormalizer;
}

/**
* @param mixed $topic
* @param null $format
*
* @throws ExceptionInterface
*
* @return array|bool|false|float|int|string
*/
public function normalize($topic, $format = null, array $context = [])
{
$serializer = new Serializer([
new JsonSerializableNormalizer($this->objectNormalizer),
$this->objectNormalizer,
]);

$topic->options = \json_encode($topic->options);

return $serializer->normalize($topic, null, ['skip_null_values' => true]);
}

/**
* @param mixed $data
* @param null $format
*/
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof SendPollMethod;
}
}
5 changes: 1 addition & 4 deletions tests/Method/SendPollMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ public function testEncode()
$this->getApi()->sendPoll($this->getMethod());
}

/**
* @return BotApiComplete
*/
private function getApi(): BotApiComplete
{
return $this->getBot('sendPoll', [
'chat_id' => 'chat_id',
'question' => 'poll_question',
'options' => ['q1', 'q2'],
'options' => '["q1","q2"]',
'disable_notification' => true,
'reply_to_message_id' => 1,
'reply_markup' => '{"inline_keyboard":[]}',
Expand Down

0 comments on commit a58f7d2

Please sign in to comment.