Completed
Push — master ( 046fb8...827e43 )
by ignace nyamagana
09:07 queued 07:27
created

HTMLConverter::encoding()   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 1
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 Traversable;
18
19
/**
20
 * A class to convert CSV records into a DOMDOcument object
21
 *
22
 * @package League.csv
23
 * @since   9.0.0
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 */
26
class HTMLConverter implements Converter
27
{
28
    /**
29
     * table class attribute value
30
     *
31
     * @var string
32
     */
33
    protected $class_name = 'table-csv-data';
34
35
    /**
36
     * table id attribute value
37
     *
38
     * @var string
39
     */
40
    protected $id_value = '';
41
42
    /**
43
     * @var XMLConverter
44
     */
45
    protected $xml_converter;
46
47
    /**
48
     * New Instance
49
     */
50 2
    public function __construct()
51
    {
52 2
        $this->xml_converter = (new XMLConverter())
53 2
            ->rootElement('table')
54 2
            ->recordElement('tr')
55 2
            ->fieldElement('td')
56
        ;
57 2
    }
58
59
    /**
60
     * HTML encoding
61
     *
62
     * @param string $encoding
63
     *
64
     * @return static
65
     */
66 2
    public function encoding(string $encoding): self
67
    {
68 2
        $clone = clone $this;
69 2
        $clone->xml_converter = $this->xml_converter->encoding($encoding);
70
71 2
        return $clone;
72
    }
73
74
    /**
75
     * HTML table class name setter
76
     *
77
     * @param string $class_name
78
     * @param string $id_value
79
     *
80
     * @return self
81
     */
82 2
    public function table(string $class_name, string $id_value = ''): self
83
    {
84 2
        $clone = clone $this;
85 2
        $clone->class_name = $class_name;
86 2
        $clone->id_value = $id_value;
87
88 2
        return $clone;
89
    }
90
91
    /**
92
     * HTML tr record offset attribute setter
93
     *
94
     * @param string $record_offset_attribute_name
95
     *
96
     * @return self
97
     */
98 2
    public function tr(string $record_offset_attribute_name): self
99
    {
100 2
        $clone = clone $this;
101 2
        $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name);
102
103 2
        return $clone;
104
    }
105
106
    /**
107
     * HTML td field name attribute setter
108
     *
109
     * @param string $fieldname_attribute_name
110
     *
111
     * @return self
112
     */
113 2
    public function td(string $fieldname_attribute_name): self
114
    {
115 2
        $clone = clone $this;
116 2
        $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name);
117
118 2
        return $clone;
119
    }
120
121
    /**
122
     * Convert an Record collection into a DOMDocument
123
     *
124
     * @param array|Traversable $records the CSV records collection
125
     *
126
     * @return string
127
     */
128 2
    public function convert($records)
129
    {
130 2
        $doc = $this->xml_converter->convert($records);
131 2
        $doc->documentElement->setAttribute('class', $this->class_name);
132 2
        $doc->documentElement->setAttribute('id', $this->id_value);
133
134 2
        return $doc->saveHTML($doc->documentElement);
135
    }
136
}
137