Skip to content

Commit

Permalink
Merge pull request #40 from tg-bot-api/bugfix/39-fix-normalization
Browse files Browse the repository at this point in the history
Bugfix/39 fix normalization
  • Loading branch information
greenplugin authored Jul 13, 2020
2 parents fdd4b3f + e3032cd commit 1c867d9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
--->

## 1.6.2 - 2020-07-13

### Fixed
- fixed EditMessageMediaMethod normalization (#33).


## 1.6.1 - 2020-07-03

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions src/BotApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use TgBotApi\BotApiBase\Normalizer\AnswerInlineQueryNormalizer;
use TgBotApi\BotApiBase\Normalizer\EditMessageMediaNormalizer;
use TgBotApi\BotApiBase\Normalizer\EditMessageResponseNormalizer;
use TgBotApi\BotApiBase\Normalizer\InputFileNormalizer;
use TgBotApi\BotApiBase\Normalizer\InputMediaNormalizer;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function normalize($method): BotApiRequestInterface
new SetMyCommandsNormalizer($objectNormalizer),
new InputFileNormalizer($files),
new MediaGroupNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
new EditMessageMediaNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
new JsonSerializableNormalizer($objectNormalizer),
new AnswerInlineQueryNormalizer($objectNormalizer),
new DateTimeNormalizer(),
Expand Down
67 changes: 67 additions & 0 deletions src/Normalizer/EditMessageMediaNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\EditMessageMediaMethod;

/**
* Class MediaGroupNormalizer.
*/
class EditMessageMediaNormalizer implements NormalizerInterface
{
/**
* @var InputMediaNormalizer
*/
private $inputMediaNormalizer;
/**
* @var NormalizerInterface
*/
private $objectNormalizer;

/**
* MediaGroupNormalizer constructor.
*/
public function __construct(
InputMediaNormalizer $inputMediaNormalizer,
NormalizerInterface $objectNormalizer
) {
$this->inputMediaNormalizer = $inputMediaNormalizer;
$this->objectNormalizer = $objectNormalizer;
}

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

$topic->replyMarkup = $serializer->normalize($topic->replyMarkup, 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 EditMessageMediaMethod;
}
}
14 changes: 4 additions & 10 deletions src/Normalizer/InputMediaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use TgBotApi\BotApiBase\Type\InputFileType;
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaPhotoType;
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaVideoType;
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaType;

/**
* Class InputMediaNormalizer.
Expand All @@ -28,7 +27,6 @@ class InputMediaNormalizer implements NormalizerInterface
/**
* InputMediaNormalizer constructor.
*
* @param NormalizerInterface $objectNormalizer
* @param $files
*/
public function __construct(NormalizerInterface $objectNormalizer, &$files)
Expand All @@ -38,9 +36,8 @@ public function __construct(NormalizerInterface $objectNormalizer, &$files)
}

/**
* @param mixed $topic
* @param null $format
* @param array $context
* @param InputMediaType $topic
* @param null $format
*
* @throws ExceptionInterface
*
Expand Down Expand Up @@ -68,12 +65,9 @@ public function normalize($topic, $format = null, array $context = [])
/**
* @param mixed $data
* @param null $format
*
* @return bool
*/
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof InputMediaPhotoType ||
$data instanceof InputMediaVideoType;
return $data instanceof InputMediaType;
}
}
7 changes: 1 addition & 6 deletions src/Normalizer/JsonSerializableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class JsonSerializableNormalizer implements NormalizerInterface

/**
* JsonSerializableNormalizer constructor.
*
* @param NormalizerInterface $objectNormalizer
*/
public function __construct(NormalizerInterface $objectNormalizer)
{
Expand All @@ -36,11 +34,10 @@ public function __construct(NormalizerInterface $objectNormalizer)
/**
* @param mixed $topic
* @param null $format
* @param array $context
*
* @throws ExceptionInterface
*
* @return array|bool|false|float|int|string
* @return string
*/
public function normalize($topic, $format = null, array $context = [])
{
Expand All @@ -52,8 +49,6 @@ public function normalize($topic, $format = null, array $context = [])
/**
* @param mixed $data
* @param null $format
*
* @return bool
*/
public function supportsNormalization($data, $format = null): bool
{
Expand Down
7 changes: 2 additions & 5 deletions tests/Method/EditMessageMediaMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,15 @@ public function testEncodeMediaAnimation()
}

/**
* @param array $excepted
* @param EditMessageMediaMethod $actual
*
* @throws \TgBotApi\BotApiBase\Exception\ResponseException
*/
private function queryApi(array $excepted, EditMessageMediaMethod $actual)
{
$this->getBotWithFiles(
'editMessageMedia',
$excepted,
['media' => ['thumb' => true, 'media' => true]],
['reply_markup'],
['media' => ['media' => true, 'thumb' => true]],
['media', 'reply_markup'],
true
)->editMessageMedia($actual);
}
Expand Down

0 comments on commit 1c867d9

Please sign in to comment.