Completed
Push — master ( af3067...26e39e )
by ignace nyamagana
04:48 queued 02:45
created

HTMLConverter::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
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 Traversable;
19
20
/**
21
 * A class to convert CSV records into a DOMDOcument object
22
 *
23
 * @package League.csv
24
 * @since   9.0.0
25
 * @author  Ignace Nyamagana Butera <[email protected]>
26
 */
27
class HTMLConverter implements ConverterInterface
28
{
29
    use ConverterTrait;
30
31
    /**
32
     * table class name
33
     *
34
     * @var string
35
     */
36
    protected $class_name = 'table-csv-data';
37
38
    /**
39
     * @var XMLConverter
40
     */
41
    protected $xml_converter;
42
43
    /**
44
     * New Instance
45
     */
46 2
    public function __construct()
47
    {
48 2
        $this->xml_converter = (new XMLConverter())
49 2
            ->rootElement('table')
50 2
            ->recordElement('tr')
51 2
            ->fieldElement('td')
52
        ;
53 2
    }
54
55
    /**
56
     * HTML table class name setter
57
     *
58
     * @param string $class_name
59
     *
60
     * @return self
61
     */
62 2
    public function className(string $class_name): self
63
    {
64 2
        $clone = clone $this;
65 2
        $clone->class_name = trim(filter_var($class_name, FILTER_SANITIZE_STRING, [
66 2
            'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH,
67
        ]));
68
69 2
        return $clone;
70
    }
71
72
    /**
73
     * HTML td field name attribute setter
74
     *
75
     * @param string $attribute_name
76
     *
77
     * @return self
78
     */
79 2
    public function fieldAttributeName(string $attribute_name)
80
    {
81 2
        $clone = clone $this;
82 2
        $clone->xml_converter = $this->xml_converter->fieldElement('td', $attribute_name);
83
84 2
        return $clone;
85
    }
86
87
    /**
88
     * HTML tr record offset attribute setter
89
     *
90
     * @param string $attribute_name
91
     *
92
     * @return self
93
     */
94 2
    public function recordOffsetAttributeName(string $attribute_name)
95
    {
96 2
        $clone = clone $this;
97 2
        $clone->xml_converter = $this->xml_converter->recordElement('tr', $attribute_name);
98
99 2
        return $clone;
100
    }
101
102
    /**
103
     * Convert an Record collection into a DOMDocument
104
     *
105
     * @param array|Traversable $records the CSV records collection
106
     *
107
     * @return DOMDocument
108
     */
109 2
    public function convert($records)
110
    {
111 2
        $doc = $this->xml_converter->inputEncoding($this->input_encoding)->convert($records);
112 2
        if ('' !== $this->class_name) {
113 2
            $doc->documentElement->setAttribute('class', $this->class_name);
114
        }
115
116 2
        return $doc->saveHTML($doc->documentElement);
117
    }
118
}
119