XmlObjectSerializer::serializeMultiple()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
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
 * 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