supportsNormalization()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
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\Type\ForceReplyType;
11
use TgBotApi\BotApiBase\Type\InlineKeyboardMarkupType;
12
use TgBotApi\BotApiBase\Type\MaskPositionType;
13
use TgBotApi\BotApiBase\Type\ReplyKeyboardMarkupType;
14
use TgBotApi\BotApiBase\Type\ReplyKeyboardRemoveType;
15
16
/**
17
 * Class JsonSerializableNormalizer.
18
 */
19
class JsonSerializableNormalizer implements NormalizerInterface
20
{
21
    /**
22
     * @var NormalizerInterface
23
     */
24
    private $objectNormalizer;
25
26
    /**
27
     * JsonSerializableNormalizer constructor.
28
     */
29 120
    public function __construct(NormalizerInterface $objectNormalizer)
30
    {
31 120
        $this->objectNormalizer = $objectNormalizer;
32 120
    }
33
34
    /**
35
     * @param mixed $topic
36
     * @param null  $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
37
     *
38
     * @throws ExceptionInterface
39
     *
40
     * @return string
41
     */
42 41
    public function normalize($topic, $format = null, array $context = [])
43
    {
44 41
        $serializer = new Serializer([$this->objectNormalizer]);
45
46 41
        return \json_encode($serializer->normalize($topic, null, ['skip_null_values' => true]));
47
    }
48
49
    /**
50
     * @param mixed $data
51
     * @param null  $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
52
     */
53 119
    public function supportsNormalization($data, $format = null): bool
54
    {
55 119
        return $data instanceof InlineKeyboardMarkupType ||
56 119
            $data instanceof ReplyKeyboardMarkupType ||
57 119
            $data instanceof ReplyKeyboardRemoveType ||
58 119
            $data instanceof MaskPositionType ||
59 119
            $data instanceof ForceReplyType;
60
    }
61
}
62