Skip to content

Bugfix/21 pool normalization bug #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
--->

## 1.3.2 - 2020-02-26

### Fixed
- fixed Poll normalization bug #21.

## 1.3.1 - 2020-01-30

### Fixed
Expand Down
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