Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
40 / 40 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
HtmlTableBuilder | |
100.00% |
40 / 40 |
|
100.00% |
8 / 8 |
20 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
addHeader | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRows | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSortable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
appendRow | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
appendRows | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
toHtml | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\Html; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Html\Html; |
7 | use Wikimedia\Assert\Assert; |
8 | |
9 | /** |
10 | * @author BP2014N1 |
11 | * @license GPL-2.0-or-later |
12 | */ |
13 | class HtmlTableBuilder { |
14 | |
15 | /** |
16 | * @var HtmlTableHeaderBuilder[] |
17 | */ |
18 | private $headers = []; |
19 | |
20 | /** |
21 | * Array of HtmlTableCellBuilder arrays |
22 | * |
23 | * @var array[] |
24 | */ |
25 | private $rows = []; |
26 | |
27 | /** |
28 | * @var bool |
29 | */ |
30 | private $isSortable; |
31 | |
32 | /** |
33 | * @param array $headers |
34 | */ |
35 | public function __construct( array $headers ) { |
36 | foreach ( $headers as $header ) { |
37 | $this->addHeader( $header ); |
38 | } |
39 | } |
40 | |
41 | /** |
42 | * @param string|HtmlTableHeaderBuilder $header |
43 | * |
44 | * @throws InvalidArgumentException |
45 | */ |
46 | private function addHeader( $header ) { |
47 | Assert::parameterType( [ 'string', HtmlTableHeaderBuilder::class ], $header, '$header' ); |
48 | |
49 | if ( is_string( $header ) ) { |
50 | $header = new HtmlTableHeaderBuilder( $header ); |
51 | } |
52 | |
53 | $this->headers[] = $header; |
54 | |
55 | if ( $header->getIsSortable() ) { |
56 | $this->isSortable = true; |
57 | } |
58 | } |
59 | |
60 | /** |
61 | * @return HtmlTableHeaderBuilder[] |
62 | */ |
63 | public function getHeaders() { |
64 | return $this->headers; |
65 | } |
66 | |
67 | /** |
68 | * @return array[] |
69 | */ |
70 | public function getRows() { |
71 | return $this->rows; |
72 | } |
73 | |
74 | /** |
75 | * @return bool |
76 | */ |
77 | public function isSortable() { |
78 | return $this->isSortable; |
79 | } |
80 | |
81 | /** |
82 | * Adds row with specified cells to table. |
83 | * |
84 | * @param string[]|HtmlTableCellBuilder[] $cells |
85 | * |
86 | * @throws InvalidArgumentException |
87 | */ |
88 | public function appendRow( array $cells ) { |
89 | foreach ( $cells as $key => $cell ) { |
90 | if ( is_string( $cell ) ) { |
91 | $cells[$key] = new HtmlTableCellBuilder( $cell ); |
92 | } elseif ( !( $cell instanceof HtmlTableCellBuilder ) ) { |
93 | throw new InvalidArgumentException( '$cells must be array of HtmlTableCell objects.' ); |
94 | } |
95 | } |
96 | |
97 | $this->rows[] = $cells; |
98 | } |
99 | |
100 | /** |
101 | * Adds rows with specified cells to table. |
102 | * |
103 | * @param array[] $rows |
104 | * |
105 | * @throws InvalidArgumentException |
106 | */ |
107 | public function appendRows( array $rows ) { |
108 | foreach ( $rows as $cells ) { |
109 | if ( !is_array( $cells ) ) { |
110 | throw new InvalidArgumentException( '$rows must be array of arrays of HtmlTableCell objects.' ); |
111 | } |
112 | |
113 | $this->appendRow( $cells ); |
114 | } |
115 | } |
116 | |
117 | /** |
118 | * Returns table as html. |
119 | * |
120 | * @return string |
121 | */ |
122 | public function toHtml() { |
123 | // Open table |
124 | $tableClasses = 'wikitable'; |
125 | if ( $this->isSortable ) { |
126 | $tableClasses .= ' sortable'; |
127 | } |
128 | $html = Html::openElement( 'table', [ 'class' => $tableClasses ] ); |
129 | |
130 | // Write headers |
131 | $html .= Html::openElement( 'thead' ); |
132 | $html .= Html::openElement( 'tr' ); |
133 | foreach ( $this->headers as $header ) { |
134 | $html .= $header->toHtml(); |
135 | } |
136 | $html .= Html::closeElement( 'tr' ); |
137 | $html .= Html::closeElement( 'thead' ); |
138 | $html .= Html::openElement( 'tbody' ); |
139 | |
140 | // Write rows |
141 | foreach ( $this->rows as $row ) { |
142 | $html .= Html::openElement( 'tr' ); |
143 | |
144 | /** |
145 | * @var HtmlTableCellBuilder $cell |
146 | */ |
147 | foreach ( $row as $cell ) { |
148 | $html .= $cell->toHtml(); |
149 | } |
150 | |
151 | $html .= Html::closeElement( 'tr' ); |
152 | } |
153 | |
154 | // Close table |
155 | $html .= Html::closeElement( 'tbody' ); |
156 | $html .= Html::closeElement( 'table' ); |
157 | |
158 | return $html; |
159 | } |
160 | |
161 | } |