HTMLConverter   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 101
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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