Skip to content

Commit a58f7d2

Browse files
committed
#21 added normalizer
1 parent b032509 commit a58f7d2

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

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)