Passed
Push — master ( 10695e...1f9b85 )
by Alexander
16:09 queued 14:59
created

XmlSerializer::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Serializer;
6
7
use Symfony\Component\Serializer\Encoder\XmlEncoder;
8
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
9
use Symfony\Component\Serializer\Serializer;
10
11
/**
12
 * XmlSerializer serializes data in XML format.
13
 */
14
final class XmlSerializer implements SerializerInterface
15
{
16
    private Serializer $serializer;
17
18
    /**
19
     * @param string $rootTag The name of the root element.
20
     * @param string $version The XML version.
21
     * @param string $encoding The XML encoding.
22
     */
23 13
    public function __construct(string $rootTag = 'response', string $version = '1.0', string $encoding = 'UTF-8')
24
    {
25 13
        $this->serializer = new Serializer([new ObjectNormalizer()], [new XmlEncoder([
26 13
            XmlEncoder::ROOT_NODE_NAME => $rootTag,
27 13
            XmlEncoder::VERSION => $version,
28 13
            XmlEncoder::ENCODING => $encoding,
29
        ])]);
30 13
    }
31
32 8
    public function serialize($value): string
33
    {
34 8
        return $this->serializer->serialize($value, XmlEncoder::FORMAT);
35
    }
36
37 6
    public function unserialize(string $value)
38
    {
39 6
        return $this->serializer->decode($value, XmlEncoder::FORMAT);
40
    }
41
}
42