Skip to content

Commit d3ef14c

Browse files
authored
Merge pull request #22 from tg-bot-api/bugfix/21-pool-normalization-bug
Bugfix/21 pool normalization bug
2 parents b032509 + 7bd63bb commit d3ef14c

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2323
- Nothing
2424
--->
2525

26+
## 1.3.2 - 2020-02-26
27+
28+
### Fixed
29+
- fixed Poll normalization bug #21.
30+
2631
## 1.3.1 - 2020-01-30
2732

2833
### Fixed

src/BotApiNormalizer.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer;
2020
use TgBotApi\BotApiBase\Normalizer\LegacyObjectNormalizerWrapper;
2121
use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer;
22+
use TgBotApi\BotApiBase\Normalizer\PollNormalizer;
2223
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
2324

2425
/**
@@ -73,6 +74,7 @@ public function normalize($method): BotApiRequestInterface
7374
}
7475

7576
$serializer = new Serializer([
77+
new PollNormalizer($objectNormalizer),
7678
new InputFileNormalizer($files),
7779
new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
7880
new JsonSerializableNormalizer($objectNormalizer),

src/Normalizer/PollNormalizer.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Normalizer;
6+
7+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
use Symfony\Component\Serializer\Serializer;
10+
use TgBotApi\BotApiBase\Method\SendPollMethod;
11+
12+
class PollNormalizer implements NormalizerInterface
13+
{
14+
/**
15+
* @var NormalizerInterface
16+
*/
17+
private $objectNormalizer;
18+
19+
/**
20+
* JsonSerializableNormalizer constructor.
21+
*/
22+
public function __construct(NormalizerInterface $objectNormalizer)
23+
{
24+
$this->objectNormalizer = $objectNormalizer;
25+
}
26+
27+
/**
28+
* @param mixed $topic
29+
* @param null $format
30+
*
31+
* @throws ExceptionInterface
32+
*
33+
* @return array|bool|false|float|int|string
34+
*/
35+
public function normalize($topic, $format = null, array $context = [])
36+
{
37+
$serializer = new Serializer([
38+
new JsonSerializableNormalizer($this->objectNormalizer),
39+
$this->objectNormalizer,
40+
]);
41+
42+
$topic->options = \json_encode($topic->options);
43+
44+
return $serializer->normalize($topic, null, ['skip_null_values' => true]);
45+
}
46+
47+
/**
48+
* @param mixed $data
49+
* @param null $format
50+
*/
51+
public function supportsNormalization($data, $format = null): bool
52+
{
53+
return $data instanceof SendPollMethod;
54+
}
55+
}

tests/Method/SendPollMethodTest.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ public function testEncode()
1818
$this->getApi()->sendPoll($this->getMethod());
1919
}
2020

21-
/**
22-
* @return BotApiComplete
23-
*/
2421
private function getApi(): BotApiComplete
2522
{
2623
return $this->getBot('sendPoll', [
2724
'chat_id' => 'chat_id',
2825
'question' => 'poll_question',
29-
'options' => ['q1', 'q2'],
26+
'options' => '["q1","q2"]',
3027
'disable_notification' => true,
3128
'reply_to_message_id' => 1,
3229
'reply_markup' => '{"inline_keyboard":[]}',

0 commit comments

Comments
 (0)