Completed
Pull Request — master (#1984)
by
unknown
13:18
created

XMLSerializer::getDataNodeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Core\Serializer\Serializer;
14
15
use Symfony\Component\Serializer\Encoder\XmlEncoder;
16
use Thelia\Core\Serializer\AbstractSerializer;
17
18
/**
19
 * Class XMLSerializer
20
 * @author Jérôme Billiras <[email protected]>
21
 */
22
class XMLSerializer extends AbstractSerializer
23
{
24
    /**
25
     * @var \Symfony\Component\Serializer\Encoder\XmlEncoder An xml encoder instance
26
     */
27
    private $xmlEncoder;
28
29
    /**
30
     * @var integer Position of data start
31
     */
32
    private $xmlDataStart;
33
34
    /**
35
     * @var string Root node name
36
     */
37
    private $rootNodeName = 'root';
38
39
    /**
40
     * @var string Data node name
41
     */
42
    private $dataNodeName = 'data';
43
44
    /**
45
     * Class constructor.
46
     */
47
    public function __construct()
48
    {
49
        $this->xmlEncoder = new XmlEncoder;
50
        $this->xmlEncoder->setRootNodeName($this->dataNodeName);
51
    }
52
53
    public function getId()
54
    {
55
        return 'thelia.xml';
56
    }
57
58
    public function getName()
59
    {
60
        return 'XML';
61
    }
62
63
    public function getExtension()
64
    {
65
        return 'xml';
66
    }
67
68
    public function getMimeType()
69
    {
70
        return 'application/xml';
71
    }
72
73
    /**
74
     * Get root node name
75
     *
76
     * @return string Root node name
77
     */
78
    public function getRootNodeName()
79
    {
80
        return $this->rootNodeName;
81
    }
82
83
    /**
84
     * Set root node name
85
     *
86
     * @param string $rootNodeName Root node name
87
     *
88
     * @return $this Return $this, allow chaining
89
     */
90
    public function setRootNodeName($rootNodeName)
91
    {
92
        $this->rootNodeName = $rootNodeName;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Get data node name
99
     *
100
     * @return string Root node name
101
     */
102
    public function getDataNodeName()
103
    {
104
        return $this->dataNodeName;
105
    }
106
107
    /**
108
     * Set data node name
109
     *
110
     * @param string $dataNodeName Root node name
111
     *
112
     * @return $this Return $this, allow chaining
113
     */
114
    public function setDataNodeName($dataNodeName)
115
    {
116
        $this->dataNodeName = $dataNodeName;
117
        $this->xmlEncoder->setRootNodeName($this->dataNodeName);
118
119
        return $this;
120
    }
121
122
    public function prepareFile(\SplFileObject $fileObject)
123
    {
124
        $this->xmlDataStart = null;
125
126
        $fileObject->fwrite(
127
            '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . '<' . $this->rootNodeName . '>' . PHP_EOL
128
        );
129
    }
130
131
    public function serialize($data)
132
    {
133
        $xml = $this->xmlEncoder->encode($data, 'array');
134
135
        if ($this->xmlDataStart === null) {
136
            $this->xmlDataStart = strpos($xml, '<' . $this->dataNodeName . '>');
137
        }
138
139
        return substr($xml, $this->xmlDataStart, -1);
140
    }
141
142
    public function separator()
143
    {
144
        return PHP_EOL;
145
    }
146
147
    public function finalizeFile(\SplFileObject $fileObject)
148
    {
149
        $fileObject->fwrite(PHP_EOL . '</' . $this->rootNodeName . '>');
150
    }
151
152
    public function unserialize(\SplFileObject $fileObject)
153
    {
154
        $unserializedXml = $this->xmlEncoder->decode(file_get_contents($fileObject->getPathname()), 'null');
155
156
        return reset($unserializedXml);
157
    }
158
}
159