From 917e31f4c8cc3348d72247b14b999a8eb905a716 Mon Sep 17 00:00:00 2001 From: greenplugin Date: Thu, 28 May 2020 00:03:28 +0300 Subject: [PATCH 1/2] #33: SendInvoiceMethod bug fixed, New normalizer added --- src/BotApiNormalizer.php | 2 + src/Normalizer/InvoiceNormalizer.php | 55 ++++++++++++++++++++++++++ tests/Method/SendInvoiceMethodTest.php | 7 +--- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src/Normalizer/InvoiceNormalizer.php diff --git a/src/BotApiNormalizer.php b/src/BotApiNormalizer.php index c7a13fa..5ebc4aa 100644 --- a/src/BotApiNormalizer.php +++ b/src/BotApiNormalizer.php @@ -16,6 +16,7 @@ use TgBotApi\BotApiBase\Normalizer\EditMessageResponseNormalizer; use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer; use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer; +use TgBotApi\BotApiBase\Normalizer\InvoiceNormalizer; use TgBotApi\BotApiBase\Normalizer\JsonSerializableNormalizer; use TgBotApi\BotApiBase\Normalizer\LegacyObjectNormalizerWrapper; use TgBotApi\BotApiBase\Normalizer\MediaGroupNormalizer; @@ -76,6 +77,7 @@ public function normalize($method): BotApiRequestInterface $serializer = new Serializer([ new PollNormalizer($objectNormalizer), + new InvoiceNormalizer($objectNormalizer), new SetMyCommandsNormalizer($objectNormalizer), new InputFileNormalizer($files), new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer), diff --git a/src/Normalizer/InvoiceNormalizer.php b/src/Normalizer/InvoiceNormalizer.php new file mode 100644 index 0000000..a37da2f --- /dev/null +++ b/src/Normalizer/InvoiceNormalizer.php @@ -0,0 +1,55 @@ +objectNormalizer = $objectNormalizer; + } + + /** + * @param SendInvoiceMethod $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->prices = \json_encode($serializer->normalize($topic->prices, null, ['skip_null_values' => true])); + + 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 SendInvoiceMethod; + } +} diff --git a/tests/Method/SendInvoiceMethodTest.php b/tests/Method/SendInvoiceMethodTest.php index e673806..c0b9b97 100644 --- a/tests/Method/SendInvoiceMethodTest.php +++ b/tests/Method/SendInvoiceMethodTest.php @@ -23,9 +23,6 @@ public function testEncode() $this->getApi()->send($this->getMethod()); } - /** - * @return BotApiComplete - */ private function getApi(): BotApiComplete { return $this->getBot('sendInvoice', [ @@ -52,13 +49,11 @@ private function getApi(): BotApiComplete 'disable_notification' => true, 'reply_to_message_id' => 1, 'reply_markup' => $this->buildInlineMarkupArray(), - ], [], ['reply_markup']); + ], [], ['reply_markup', 'prices']); } /** * @throws \TgBotApi\BotApiBase\Exception\BadArgumentException - * - * @return SendInvoiceMethod */ private function getMethod(): SendInvoiceMethod { From 6cd7432c9132e085718eb786be3601f5da13ce6e Mon Sep 17 00:00:00 2001 From: greenplugin Date: Thu, 28 May 2020 00:08:00 +0300 Subject: [PATCH 2/2] #33: Changelog update --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ced753..bd65f24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Security - Nothing ---> + +## 1.5.1 - 2020-05-28 + +### Fixed +- fixed SendInvoiceMethod normalization #33. + ## 1.5.0 - 2020-04-24 #### April 24, 2020