Completed
Push — master ( 473a0b...a42475 )
by ignace nyamagana
03:08
created

XMLEncoder::itemToItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use DOMDocument;
18
use DOMElement;
19
use Iterator;
20
21
/**
22
 * A class to convert CSV records into a DOMDOcument object
23
 *
24
 * @package League.csv
25
 * @since   9.0.0
26
 * @author  Ignace Nyamagana Butera <[email protected]>
27
 * @internal used internally to returns the DOMDocument object representation of a CSV document
28
 *
29
 */
30
class XMLEncoder
31
{
32
    /**
33
     * XML Root name
34
     *
35
     * @var string
36
     */
37
    protected $root_name = 'csv';
38
39
    /**
40
     * XML Node name
41
     *
42
     * @var string
43
     */
44
    protected $record_name = 'row';
45
46
    /**
47
     * XML Item name
48
     *
49
     * @var string
50
     */
51
    protected $item_name = 'cell';
52
53
    /**
54
     * XML column attribute name
55
     *
56
     * @var string
57
     */
58
    protected $column_attr = 'name';
59
60
    /**
61
     * XML offset attribute name
62
     *
63
     * @var string
64
     */
65
    protected $offset_attr = 'offset';
66
67
    /**
68
     * Tell whether to preserve item offset
69
     *
70
     * @var bool
71
     */
72
    protected $preserve_item_offset = false;
73
74
    /**
75
     * Conversion method list
76
     *
77
     * @var array
78
     */
79
    protected $encoder = [
80
        'item' => [
81
            true => 'itemToItemWithAttribute',
82
            false => 'itemToItem',
83
        ],
84
        'record' => [
85
            true => 'recordToElementWithAttribute',
86
            false => 'recordToElement',
87
        ],
88
    ];
89
90
    /**
91
     * XML Root name setter
92
     *
93
     * @param string $root_name
94
     *
95
     * @return self
96
     */
97 6
    public function rootName(string $root_name): self
98
    {
99 6
        if ($root_name === $this->root_name) {
100 2
            return $this;
101
        }
102
103 4
        $clone = clone $this;
104 4
        $clone->root_name = $root_name;
105
106 4
        return $clone;
107
    }
108
109
    /**
110
     * XML Record name setter
111
     *
112
     * @param string $record_name
113
     *
114
     * @return self
115
     */
116 6
    public function recordName(string $record_name): self
117
    {
118 6
        if ($record_name === $this->record_name) {
119 2
            return $this;
120
        }
121
122 4
        $clone = clone $this;
123 4
        $clone->record_name = $record_name;
124
125 4
        return $clone;
126
    }
127
128
    /**
129
     * XML Item name setter
130
     *
131
     * @param string $item_name
132
     *
133
     * @return self
134
     */
135 6
    public function itemName(string $item_name): self
136
    {
137 6
        if ($item_name === $this->item_name) {
138 2
            return $this;
139
        }
140
141 4
        $clone = clone $this;
142 4
        $clone->item_name = $item_name;
143
144 4
        return $clone;
145
    }
146
147
    /**
148
     * XML Column attribute name setter
149
     *
150
     * @param string $column_attr
151
     *
152
     * @return self
153
     */
154 6
    public function columnAttributeName(string $column_attr): self
155
    {
156 6
        if ($column_attr === $this->column_attr) {
157 2
            return $this;
158
        }
159
160 4
        $clone = clone $this;
161 4
        $clone->column_attr = $column_attr;
162
163 4
        return $clone;
164
    }
165
166
    /**
167
     * XML Offset attribute name setter
168
     *
169
     * @param string $offset_attr
170
     *
171
     * @return self
172
     */
173 6
    public function offsetAttributeName(string $offset_attr): self
174
    {
175 6
        if ($offset_attr === $this->offset_attr) {
176 2
            return $this;
177
        }
178
179 4
        $clone = clone $this;
180 4
        $clone->offset_attr = $offset_attr;
181
182 4
        return $clone;
183
    }
184
185
    /**
186
     * Whether we should preserve the CSV records keys.
187
     *
188
     * If set to true CSV document record keys will added to
189
     * the conversion output .
190
     *
191
     * @param bool $status
192
     *
193
     * @return self
194
     */
195 6
    public function preserveItemOffset(bool $status): self
196
    {
197 6
        if ($status === $this->preserve_item_offset) {
198 4
            return $this;
199
        }
200
201 4
        $clone = clone $this;
202 4
        $clone->preserve_item_offset = $status;
203
204 4
        return $clone;
205
    }
206
207
    /**
208
     * Convert an Record collection into a DOMDocument
209
     *
210
     * @param Iterator $iterator               the CSV records iterator
211
     * @param bool     $preserve_record_offset Tell whether the CSV records offset must be kept
212
     *
213
     * @return DOMDocument
214
     */
215 6
    public function encode(Iterator $iterator, bool $preserve_record_offset = false): DOMDocument
216
    {
217 6
        $item_encoder = $this->encoder['item'][$this->preserve_item_offset];
218 6
        $record_encoder = $this->encoder['record'][$preserve_record_offset];
219
220 6
        $doc = new DOMDocument('1.0', 'UTF-8');
221 6
        $root = $doc->createElement($this->root_name);
222 6
        foreach ($iterator as $offset => $record) {
223 6
            $node = $this->{$record_encoder}($doc, $record, $item_encoder, $offset);
224 6
            $root->appendChild($node);
225
        }
226
227 6
        $doc->appendChild($root);
228
229 6
        return $doc;
230
    }
231
232
    /**
233
     * Convert a CSV record into a DOMElement and
234
     * adds its offset as DOMElement attribute
235
     *
236
     * @param DOMDocument $doc
237
     * @param array       $record       CSV record
238
     * @param string      $item_encoder CSV Cell encoder method name
239
     * @param int         $offset       CSV record offset
240
     *
241
     * @return DOMElement
242
     */
243 2
    protected function recordToElementWithAttribute(DOMDocument $doc, array $record, string $item_encoder, int $offset): DOMElement
244
    {
245 2
        $node = $this->recordToElement($doc, $record, $item_encoder);
246 2
        $node->setAttribute($this->offset_attr, (string) $offset);
247
248 2
        return $node;
249
    }
250
251
    /**
252
     * Convert a CSV record into a DOMElement
253
     *
254
     * @param DOMDocument $doc
255
     * @param array       $record       CSV record
256
     * @param string      $item_encoder CSV Cell encoder method name
257
     *
258
     * @return DOMElement
259
     */
260 6
    protected function recordToElement(DOMDocument $doc, array $record, string $item_encoder): DOMElement
261
    {
262 6
        $node = $doc->createElement($this->record_name);
263 6
        foreach ($record as $name => $value) {
264 6
            $item = $this->{$item_encoder}($doc, $value, $name);
265 6
            $node->appendChild($item);
266
        }
267
268 6
        return $node;
269
    }
270
271
    /**
272
     * Convert Cell to Item.
273
     *
274
     * Convert the CSV item into a DOMElement and adds the item offset
275
     * as attribute to the returned DOMElement
276
     *
277
     * @param DOMDocument $doc
278
     * @param string      $value Record item value
279
     * @param int|string  $name  Record item offset
280
     *
281
     * @return DOMElement
282
     */
283 4
    protected function itemToItemWithAttribute(DOMDocument $doc, string $value, $name): DOMElement
284
    {
285 4
        $item = $this->itemToItem($doc, $value);
286 4
        $item->setAttribute($this->column_attr, (string) $name);
287
288 4
        return $item;
289
    }
290
291
    /**
292
     * Convert Cell to Item
293
     *
294
     * @param DOMDocument $doc
295
     * @param string      $value Record item value
296
     * @param int|string  $name  Record item offset
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
297
     *
298
     * @return DOMElement
299
     */
300 6
    protected function itemToItem(DOMDocument $doc, string $value): DOMElement
301
    {
302 6
        $item = $doc->createElement($this->item_name);
303 6
        $item->appendChild($doc->createTextNode($value));
304
305 6
        return $item;
306
    }
307
}
308