XmlObjectSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A unserializeMultiple() 0 4 1
A serializeMultiple() 0 4 1
A __construct() 0 6 1
A unserialize() 0 4 1
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
 * XmlObjectSerializer serializes objects in XML format and vice versa.
13
 */
14
final class XmlObjectSerializer implements ObjectSerializerInterface
15
{
16
    use ObjectSerializerTrait;
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 10
    public function __construct(string $rootTag = 'response', string $version = '1.0', string $encoding = 'UTF-8')
24
    {
25 10
        $this->serializer = new Serializer([new ObjectNormalizer()], [new XmlEncoder([
26 10
            XmlEncoder::ROOT_NODE_NAME => $rootTag,
27 10
            XmlEncoder::VERSION => $version,
28 10
            XmlEncoder::ENCODING => $encoding,
29
        ])]);
30 10
    }
31
32 4
    public function serialize(object $object): string
33
    {
34 4
        return $this->serializer->serialize($object, XmlEncoder::FORMAT);
35
    }
36
37 2
    public function serializeMultiple(array $objects): string
38
    {
39 2
        $this->checkArrayObjects($objects);
40 1
        return $this->serializer->serialize($objects, XmlEncoder::FORMAT);
41
    }
42
43 3
    public function unserialize(string $value, string $class): object
44
    {
45 3
        $decodedValue = $this->checkAndDecodeData($value, $class, XmlEncoder::FORMAT);
46 1
        return $this->denormalizeObject($decodedValue, $class);
47
    }
48
49 3
    public function unserializeMultiple(string $value, string $class): array
50
    {
51 3
        $decodedValue = $this->checkAndDecodeData($value, $class, XmlEncoder::FORMAT);
52 1
        return $this->denormalizeObjects($decodedValue, $class);
53
    }
54
}
55