1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\Html; |
4
|
|
|
|
5
|
|
|
use Html; |
6
|
|
|
use InvalidArgumentException; |
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 jquery-tablesort'; |
127
|
|
|
} |
128
|
|
|
$html = Html::openElement( 'table', [ 'class' => $tableClasses ] ); |
129
|
|
|
|
130
|
|
|
// Write headers |
131
|
|
|
$html .= Html::openElement( 'tr' ); |
132
|
|
|
foreach ( $this->headers as $header ) { |
133
|
|
|
$html .= $header->toHtml(); |
134
|
|
|
} |
135
|
|
|
$html .= Html::closeElement( 'tr' ); |
136
|
|
|
|
137
|
|
|
// Write rows |
138
|
|
|
foreach ( $this->rows as $row ) { |
139
|
|
|
$html .= Html::openElement( 'tr' ); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @var HtmlTableCellBuilder $cell |
143
|
|
|
*/ |
144
|
|
|
foreach ( $row as $cell ) { |
145
|
|
|
$html .= $cell->toHtml(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$html .= Html::closeElement( 'tr' ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Close table |
152
|
|
|
$html .= Html::closeElement( 'table' ); |
153
|
|
|
|
154
|
|
|
return $html; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|