Completed
Push — master ( 5bc0ec...360629 )
by Gunnar
03:05
created
src/HTMLTable/CHTMLTable.php 1 patch
Indentation   +456 added lines, -456 removed lines patch added patch discarded remove patch
@@ -17,460 +17,460 @@
 block discarded – undo
17 17
  */
18 18
 class CHTMLTable
19 19
 {
20
-    const FOOTER = 'footer';
21
-
22
-    private $tableSpec;
23
-    private $tableHead;
24
-    private $tableBody;
25
-    private $tableFoot;
26
-
27
-    /**
28
-     * Constructor
29
-     *
30
-     * Creates a table with table head, table body and if specified, a table
31
-     * footer. It is possible to specify the table and the tabel cells settings
32
-     * per column.
33
-     *
34
-     * @param string[] $tableSpecs  table settings.
35
-     * @param mixed[] $data         table cell data.
36
-     * @param mixed[] $columnSpecs  table columns cell settings.
37
-     */
38
-    public function __construct($tableSpecs = [], $data = [], $columnSpecs = [])
39
-    {
40
-        $this->create($tableSpecs, $data, $columnSpecs);
41
-    }
42
-
43
-    /**
44
-     * Creates a table with cell data.
45
-     *
46
-     * Creates a table with table head, table body with table data and if
47
-     * specified, a table footer. It is possible to specify the table and the
48
-     * tabel cells settings per column.
49
-     *
50
-     * @param string[] $tableSpecs  table settings.
51
-     * @param mixed[] $data         table cell data.
52
-     * @param mixed[] $columnSpecs  table columns cell settings.
53
-     *
54
-     * @return object the html table object.
55
-     */
56
-    public function create($tableSpecs = [], $data = [], $columnSpecs = [])
57
-    {
58
-        $this->resetTableTags();
59
-        $this->setTableSpecifications($tableSpecs);
60
-
61
-        $this->createTableHead($data, $columnSpecs);
62
-        $this->createTableBody($data, $columnSpecs);
63
-        $this->createTableFooter($columnSpecs);
64
-
65
-        return $this;
66
-    }
67
-
68
-    /**
69
-     * Helper method to reset main parts of table tags.
70
-     *
71
-     * Sets the table head, table body and table foot tag to null.
72
-     *
73
-     * @return void
74
-     */
75
-    private function resetTableTags()
76
-    {
77
-        $this->tableHead = null;
78
-        $this->tableBody = null;
79
-        $this->tableFoot = null;
80
-    }
81
-
82
-    /**
83
-     * Helper method to set the table specifications.
84
-     *
85
-     * Merges the table specifications with the default specifications.
86
-     * Default table CSS id is html-table.
87
-     *
88
-     * @param  string[] $tableSpec the table specification.
89
-     *
90
-     * @return void
91
-     */
92
-    private function setTableSpecifications($tableSpec)
93
-    {
94
-        $defaults = [
95
-            // Always have a id for the form
96
-            'id' => 'html-table',
97
-        ];
98
-
99
-        if ($this->isClassPresent($tableSpec)) {
100
-            $tableSpec = $this->removeId($tableSpec);
101
-        }
102
-
103
-        $this->tableSpec = array_merge($defaults, $tableSpec);
104
-    }
105
-
106
-    /**
107
-     * Helper method to check if a CSS class tag is present
108
-     *
109
-     * Checks if a CSS class tag is present in the table specification.
110
-     *
111
-     * @param  string[] $tableSpec the table specification.
112
-     *
113
-     * @return boolean  true if class is present in the table specification,
114
-     *                  false otherwise.
115
-     */
116
-    private function isClassPresent($tableSpec)
117
-    {
118
-        return isset($tableSpec['class']) ? true : false;
119
-    }
120
-
121
-    /**
122
-     * Helper method to reset the id tag.
123
-     *
124
-     * Sets the CSS id tag to null.
125
-     *
126
-     * @param  string[] $tableSpec the table specification.
127
-     *
128
-     * @return string[] the table specification without the CSS id tag.
129
-     */
130
-    private function removeId($tableSpec) {
131
-        $tableSpec['id'] = null;
132
-
133
-        return $tableSpec;
134
-    }
135
-
136
-    /**
137
-     * Helper method to create the table head.
138
-     *
139
-     * Creates the table head. The title of the columns are set according to
140
-     * the table tag in the column specifications. Otherwise, the title is set
141
-     * to the keys name in the table cell data array.
142
-     *
143
-     * @param  mixed[]  $data        table cell data.
144
-     * @param  mixed[]  $columnSpecs table columns cell settings.
145
-     *
146
-     * @return void
147
-     */
148
-    private function createTableHead($data, $columnSpecs)
149
-    {
150
-        $this->tableHead = "\n<thead>";
151
-        $this->tableHead .= "\n<tr>";
152
-
153
-        if (empty($columnSpecs))
154
-        {
155
-            $this->setColumnTitlesFromData($data);
156
-        } else {
157
-            $this->setColumnTitlesFromColumnSpecifications($columnSpecs);
158
-        }
159
-
160
-        $this->tableHead .= "\n</tr>";
161
-        $this->tableHead .= "\n</thead>";
162
-    }
163
-
164
-    /**
165
-     * Helper method to set the column titles from the data array.
166
-     *
167
-     * Uses the first row in the table cell data array to set the titles of
168
-     * the columns. The name of the columns are the key name in the associative
169
-     * array containing data for the table.
170
-     *
171
-     * @param  mixed[]  $data   table cell data.
172
-     *
173
-     * @return void
174
-     */
175
-    private function setColumnTitlesFromData($data)
176
-    {
177
-        $firstRow = isset($data[0]) ? $data[0] : [];
178
-        foreach ($firstRow as $key => $value) {
179
-            $this->tableHead .= "\n<th>";
180
-            $this->tableHead .= $key;
181
-            $this->tableHead .= "</th>";
182
-        }
183
-    }
184
-
185
-    /**
186
-     * Helper method to set the column titles from column specifications.
187
-     *
188
-     * Uses column specifications to set the name of the columns in the table
189
-     * head.
190
-     *
191
-     * @param  mixed[]  $columnSpecs    table columns cell settings
192
-     *
193
-     * @return void
194
-     */
195
-    private function setColumnTitlesFromColumnSpecifications($columnSpecs)
196
-    {
197
-        foreach ($columnSpecs as $key => $columnSpec) {
198
-            if (!$this->isTableFooter($columnSpec)) {
199
-                $this->tableHead .= "\n<th>";
200
-                $this->tableHead .= $this->getTitle($key, $columnSpec);
201
-                $this->tableHead .= "</th>";
202
-            }
203
-        }
204
-    }
205
-
206
-    /**
207
-     * Helper method to check if the column cell belongs to the footer.
208
-     *
209
-     * Checks the type tag, in the column specification for one column, if the
210
-     * tag is present and set to footer.
211
-     *
212
-     * @param  mixed[]  $columnSpec cell settings for one column.
213
-     *
214
-     * @return boolean true if the cell type belongs to the footer, false otherwise.
215
-     */
216
-    private function isTableFooter($columnSpec)
217
-    {
218
-        $isFooter = false;
219
-        if (isset($columnSpec['type'])) {
220
-            if (strcmp($columnSpec['type'], self::FOOTER) === 0) {
221
-                $isFooter = true;
222
-            }
223
-        }
224
-
225
-        return $isFooter;
226
-    }
227
-
228
-    /**
229
-     * Helper method to get title from a column specification, if specified.
230
-     *
231
-     * Uses the title tag in the column specification for one column to get
232
-     * the title. If the title tag is not set, the title is the key name in
233
-     * the associative array containing data for the table.
234
-     *
235
-     * @param  array<integer,string>    $key        the name of the key for the
236
-     *                                              table cell data.
237
-     * @param  mixed[]                  $columnSpec cell settings for one column.
238
-     *
239
-     * @return string[] the name from the title tag in the cell specification.
240
-     *                  Otherwise, the table cell data key name.
241
-     */
242
-    private function getTitle($key, $columnSpec)
243
-    {
244
-        return isset($columnSpec['title']) ? $columnSpec['title'] : $key;
245
-    }
246
-
247
-    /**
248
-     * Helper method to create the table body with table cell data.
249
-     *
250
-     * Sets the table cell data in the table body.
251
-     *
252
-     * @param  mixed[] $data        table cell data.
253
-     * @param  mixed[] $columnSpecs table columns cell settings.
254
-     *
255
-     * @return void
256
-     */
257
-    private function createTableBody($data, $columnSpecs)
258
-    {
259
-        $this->setTableData($data, $columnSpecs);
260
-        if (isset($this->tableBody)) {
261
-            $this->tableBody = "\n<tbody>" . $this->tableBody . "\n</tbody>";
262
-        }
263
-    }
264
-
265
-    /**
266
-     * Helper method to set table data in table body.
267
-     *
268
-     * Sets table data according to the column specifications, if it is
269
-     * specified. Otherwise it sets the data as it is stored in the data array.
270
-     *
271
-     * @param  mixed[] $data        table cell data.
272
-     * @param  mixed[] $columnSpecs table columns cell settings.
273
-     *
274
-     * @return void
275
-     */
276
-    private function setTableData($data, $columnSpecs)
277
-    {
278
-        if (empty($columnSpecs)) {
279
-            $this->setTableDataFromData($data);
280
-        } else {
281
-            $this->setTableDataAsSpecified($data, $columnSpecs);
282
-        }
283
-    }
284
-
285
-    /**
286
-     * Helper method to set table data from the data array.
287
-     *
288
-     * Sets table data from the data array.
289
-     *
290
-     * @param  mixed[]  $data   table cell data.
291
-     *
292
-     * @return void
293
-     */
294
-    private function setTableDataFromData($data)
295
-    {
296
-        foreach ($data as $row) {
297
-            $this->tableBody .= "\n<tr>";
298
-            foreach ($row as $value) {
299
-                $this->tableBody .= "\n<td>";
300
-                $this->tableBody .= $value;
301
-                $this->tableBody .= "</td>";
302
-            }
303
-            $this->tableBody .= "\n</tr>";
304
-        }
305
-    }
306
-
307
-    /**
308
-     * Helper method to set table data according to the column specifications.
309
-     *
310
-     * Sets the table data according to the column specifications, if the cell
311
-     * does not belong to the footer. Adds a colspan tag, if it is specified
312
-     * for the cell in the column.
313
-     *
314
-     * @param  mixed[] $data         table cell data.
315
-     * @param  mixed[] $columnSpecs  table columns cell settings.
316
-     *
317
-     * @return void
318
-     */
319
-    private function setTableDataAsSpecified($data, $columnSpecs)
320
-    {
321
-        foreach ($data as $row) {
322
-            $this->tableBody .= "\n<tr>";
323
-            foreach ($columnSpecs as $key => $columnSpec) {
324
-                if (!$this->isTableFooter($columnSpec)) {
325
-                    $colspan = $this->getColspan($columnSpec);
326
-                    $this->tableBody .= "\n<td{$colspan}>";
327
-                    $this->tableBody .= $this->getValue($row, $key, $columnSpec);
328
-                    $this->tableBody .= "</td>";
329
-                }
330
-            }
331
-            $this->tableBody .= "\n</tr>";
332
-        }
333
-    }
334
-
335
-    /**
336
-     * Helper method to get the colspan value, if specified in the column
337
-     * specification for the cell.
338
-     *
339
-     * @param  mixed[]  $columnSpec cell settings for one column.
340
-     *
341
-     * @return int the colspan value if specified. Otherwise null.
342
-     */
343
-    private function getColspan($columnSpec)
344
-    {
345
-        return isset($columnSpec['colspan']) ? " colspan='{$columnSpec['colspan']}'" : null;
346
-    }
347
-
348
-    /**
349
-     * Helper method to get the value for a specific position in one row in
350
-     * the data array.
351
-     *
352
-     * Gets the data from a specific position in one row in the data array.
353
-     * If a function is specified for the cell in the column, the data is
354
-     * runned through the function before it is returned.
355
-     *
356
-     * @param  mixed[]  $row        one row of in the array of table data.
357
-     * @param  string   $key        the name of the key in the associative data array.
358
-     * @param  mixed[]  $columnSpec cell settings for one column.
359
-     */
360
-    private function getValue($row, $key, $columnSpec)
361
-    {
362
-        if ($this->isFunctionSpecified($columnSpec)) {
363
-            $dataValue = isset($row[$key]) ? $row[$key] : "";
364
-            return $this->getValueThroughFunction($columnSpec, $dataValue);
365
-        } else {
366
-            return isset($row[$key]) ? $row[$key] : "";
367
-        }
368
-    }
369
-
370
-    /**
371
-     * Helper method t check if the function tag is specified for the cells in
372
-     * one column.
373
-     *
374
-     * Checks if the function tag is set for the cell in one column.
375
-     *
376
-     * @param  mixed[]  $columnSpec     cell settings for one column.
377
-     *
378
-     * @return boolean true if a function is connected to the cell, false otherwise.
379
-     */
380
-    private function isFunctionSpecified($columnSpec)
381
-    {
382
-        return isset($columnSpec['function']) ? true : false;
383
-    }
384
-
385
-    /**
386
-     * Helper method to run the value through a function before it is returned.
387
-     *
388
-     * Runs the value through a function, if a function is connected to the cell
389
-     * in the column. If not function is connected to the cell through the
390
-     * column specification, the value is returned as it is.
391
-     *
392
-     * @param mixed[]   $columnSpec     cell settings for one column
393
-     * @param mixed     $dataValue      the value to run through function, if specified.
394
-     *
395
-     * @return the value.
396
-     */
397
-    private function getValueThroughFunction($columnSpec, $dataValue)
398
-    {
399
-        if (!empty($columnSpec['function'])) {
400
-            return call_user_func($columnSpec['function'], $dataValue);
401
-        } else {
402
-            return $dataValue;
403
-        }
404
-    }
405
-
406
-    /**
407
-     * Helper method to create table footer with data.
408
-     *
409
-     * Creates table footer if the cell settings for the column is set to
410
-     * footer in the column specifications.
411
-     * Adds a colspan tag, if it is specified for the cell in the column.
412
-     *
413
-     * @param  mixed[] $columnSpecs table columns cell settings.
414
-     *
415
-     * @return void
416
-     */
417
-    private function createTableFooter($columnSpecs)
418
-    {
419
-        foreach ($columnSpecs as $columnSpec) {
420
-            if ($this->isTableFooter($columnSpec)) {
421
-                $colspan = $this->getColspan($columnSpec);
422
-                $this->tableFoot .= "\n<td{$colspan}>";
423
-                $this->tableFoot .= $this->getFooterData($columnSpec);
424
-                $this->tableFoot .= "</td>";
425
-            }
426
-        }
427
-
428
-        if (isset($this->tableFoot)) {
429
-            $this->tableFoot = "\n<tfoot>\n<tr>" . $this->tableFoot . "\n</tr>\n</tfoot>";
430
-        }
431
-    }
432
-
433
-    /**
434
-     * Helper method to get table footer data.
435
-     *
436
-     * Gets table footer data from the column specification. Checks if the
437
-     * value should be fetched from a function or from the value tag.
438
-     * If either the function or the value specified, an empty string is
439
-     * returned.
440
-     *
441
-     * @param  mixed[] $columnSpec  cell settings for one column.
442
-     *
443
-     * @return mixed    the cell data value.
444
-     */
445
-    private function getFooterData($columnSpec)
446
-    {
447
-        if ($this->isFunctionSpecified($columnSpec)) {
448
-            return call_user_func($columnSpec['function']);
449
-        } else {
450
-            return isset($columnSpec['value']) ? $columnSpec['value'] : "";
451
-        }
452
-    }
453
-
454
-    /**
455
-     * Gets the table.
456
-     *
457
-     * Gets the table with table data.
458
-     *
459
-     * @return html     the table with table data.
460
-     */
461
-    public function getHTMLTable()
462
-    {
463
-        $id = isset($this->tableSpec['id']) ? " id='{$this->tableSpec['id']}'" : null;
464
-        $class = isset($this->tableSpec['class']) ? " class='{$this->tableSpec['class']}'" : null;
465
-        $caption = isset($this->tableSpec['caption']) ? "<caption>{$this->tableSpec['caption']}</caption>" : null;
466
-
467
-        $htmlTable = "<table{$id}{$class}>";
468
-        $htmlTable .= $caption;
469
-        $htmlTable .= $this->tableHead;
470
-        $htmlTable .= $this->tableBody;
471
-        $htmlTable .= $this->tableFoot;
472
-        $htmlTable .= "\n</table>";
473
-
474
-        return $htmlTable;
475
-    }
20
+	const FOOTER = 'footer';
21
+
22
+	private $tableSpec;
23
+	private $tableHead;
24
+	private $tableBody;
25
+	private $tableFoot;
26
+
27
+	/**
28
+	 * Constructor
29
+	 *
30
+	 * Creates a table with table head, table body and if specified, a table
31
+	 * footer. It is possible to specify the table and the tabel cells settings
32
+	 * per column.
33
+	 *
34
+	 * @param string[] $tableSpecs  table settings.
35
+	 * @param mixed[] $data         table cell data.
36
+	 * @param mixed[] $columnSpecs  table columns cell settings.
37
+	 */
38
+	public function __construct($tableSpecs = [], $data = [], $columnSpecs = [])
39
+	{
40
+		$this->create($tableSpecs, $data, $columnSpecs);
41
+	}
42
+
43
+	/**
44
+	 * Creates a table with cell data.
45
+	 *
46
+	 * Creates a table with table head, table body with table data and if
47
+	 * specified, a table footer. It is possible to specify the table and the
48
+	 * tabel cells settings per column.
49
+	 *
50
+	 * @param string[] $tableSpecs  table settings.
51
+	 * @param mixed[] $data         table cell data.
52
+	 * @param mixed[] $columnSpecs  table columns cell settings.
53
+	 *
54
+	 * @return object the html table object.
55
+	 */
56
+	public function create($tableSpecs = [], $data = [], $columnSpecs = [])
57
+	{
58
+		$this->resetTableTags();
59
+		$this->setTableSpecifications($tableSpecs);
60
+
61
+		$this->createTableHead($data, $columnSpecs);
62
+		$this->createTableBody($data, $columnSpecs);
63
+		$this->createTableFooter($columnSpecs);
64
+
65
+		return $this;
66
+	}
67
+
68
+	/**
69
+	 * Helper method to reset main parts of table tags.
70
+	 *
71
+	 * Sets the table head, table body and table foot tag to null.
72
+	 *
73
+	 * @return void
74
+	 */
75
+	private function resetTableTags()
76
+	{
77
+		$this->tableHead = null;
78
+		$this->tableBody = null;
79
+		$this->tableFoot = null;
80
+	}
81
+
82
+	/**
83
+	 * Helper method to set the table specifications.
84
+	 *
85
+	 * Merges the table specifications with the default specifications.
86
+	 * Default table CSS id is html-table.
87
+	 *
88
+	 * @param  string[] $tableSpec the table specification.
89
+	 *
90
+	 * @return void
91
+	 */
92
+	private function setTableSpecifications($tableSpec)
93
+	{
94
+		$defaults = [
95
+			// Always have a id for the form
96
+			'id' => 'html-table',
97
+		];
98
+
99
+		if ($this->isClassPresent($tableSpec)) {
100
+			$tableSpec = $this->removeId($tableSpec);
101
+		}
102
+
103
+		$this->tableSpec = array_merge($defaults, $tableSpec);
104
+	}
105
+
106
+	/**
107
+	 * Helper method to check if a CSS class tag is present
108
+	 *
109
+	 * Checks if a CSS class tag is present in the table specification.
110
+	 *
111
+	 * @param  string[] $tableSpec the table specification.
112
+	 *
113
+	 * @return boolean  true if class is present in the table specification,
114
+	 *                  false otherwise.
115
+	 */
116
+	private function isClassPresent($tableSpec)
117
+	{
118
+		return isset($tableSpec['class']) ? true : false;
119
+	}
120
+
121
+	/**
122
+	 * Helper method to reset the id tag.
123
+	 *
124
+	 * Sets the CSS id tag to null.
125
+	 *
126
+	 * @param  string[] $tableSpec the table specification.
127
+	 *
128
+	 * @return string[] the table specification without the CSS id tag.
129
+	 */
130
+	private function removeId($tableSpec) {
131
+		$tableSpec['id'] = null;
132
+
133
+		return $tableSpec;
134
+	}
135
+
136
+	/**
137
+	 * Helper method to create the table head.
138
+	 *
139
+	 * Creates the table head. The title of the columns are set according to
140
+	 * the table tag in the column specifications. Otherwise, the title is set
141
+	 * to the keys name in the table cell data array.
142
+	 *
143
+	 * @param  mixed[]  $data        table cell data.
144
+	 * @param  mixed[]  $columnSpecs table columns cell settings.
145
+	 *
146
+	 * @return void
147
+	 */
148
+	private function createTableHead($data, $columnSpecs)
149
+	{
150
+		$this->tableHead = "\n<thead>";
151
+		$this->tableHead .= "\n<tr>";
152
+
153
+		if (empty($columnSpecs))
154
+		{
155
+			$this->setColumnTitlesFromData($data);
156
+		} else {
157
+			$this->setColumnTitlesFromColumnSpecifications($columnSpecs);
158
+		}
159
+
160
+		$this->tableHead .= "\n</tr>";
161
+		$this->tableHead .= "\n</thead>";
162
+	}
163
+
164
+	/**
165
+	 * Helper method to set the column titles from the data array.
166
+	 *
167
+	 * Uses the first row in the table cell data array to set the titles of
168
+	 * the columns. The name of the columns are the key name in the associative
169
+	 * array containing data for the table.
170
+	 *
171
+	 * @param  mixed[]  $data   table cell data.
172
+	 *
173
+	 * @return void
174
+	 */
175
+	private function setColumnTitlesFromData($data)
176
+	{
177
+		$firstRow = isset($data[0]) ? $data[0] : [];
178
+		foreach ($firstRow as $key => $value) {
179
+			$this->tableHead .= "\n<th>";
180
+			$this->tableHead .= $key;
181
+			$this->tableHead .= "</th>";
182
+		}
183
+	}
184
+
185
+	/**
186
+	 * Helper method to set the column titles from column specifications.
187
+	 *
188
+	 * Uses column specifications to set the name of the columns in the table
189
+	 * head.
190
+	 *
191
+	 * @param  mixed[]  $columnSpecs    table columns cell settings
192
+	 *
193
+	 * @return void
194
+	 */
195
+	private function setColumnTitlesFromColumnSpecifications($columnSpecs)
196
+	{
197
+		foreach ($columnSpecs as $key => $columnSpec) {
198
+			if (!$this->isTableFooter($columnSpec)) {
199
+				$this->tableHead .= "\n<th>";
200
+				$this->tableHead .= $this->getTitle($key, $columnSpec);
201
+				$this->tableHead .= "</th>";
202
+			}
203
+		}
204
+	}
205
+
206
+	/**
207
+	 * Helper method to check if the column cell belongs to the footer.
208
+	 *
209
+	 * Checks the type tag, in the column specification for one column, if the
210
+	 * tag is present and set to footer.
211
+	 *
212
+	 * @param  mixed[]  $columnSpec cell settings for one column.
213
+	 *
214
+	 * @return boolean true if the cell type belongs to the footer, false otherwise.
215
+	 */
216
+	private function isTableFooter($columnSpec)
217
+	{
218
+		$isFooter = false;
219
+		if (isset($columnSpec['type'])) {
220
+			if (strcmp($columnSpec['type'], self::FOOTER) === 0) {
221
+				$isFooter = true;
222
+			}
223
+		}
224
+
225
+		return $isFooter;
226
+	}
227
+
228
+	/**
229
+	 * Helper method to get title from a column specification, if specified.
230
+	 *
231
+	 * Uses the title tag in the column specification for one column to get
232
+	 * the title. If the title tag is not set, the title is the key name in
233
+	 * the associative array containing data for the table.
234
+	 *
235
+	 * @param  array<integer,string>    $key        the name of the key for the
236
+	 *                                              table cell data.
237
+	 * @param  mixed[]                  $columnSpec cell settings for one column.
238
+	 *
239
+	 * @return string[] the name from the title tag in the cell specification.
240
+	 *                  Otherwise, the table cell data key name.
241
+	 */
242
+	private function getTitle($key, $columnSpec)
243
+	{
244
+		return isset($columnSpec['title']) ? $columnSpec['title'] : $key;
245
+	}
246
+
247
+	/**
248
+	 * Helper method to create the table body with table cell data.
249
+	 *
250
+	 * Sets the table cell data in the table body.
251
+	 *
252
+	 * @param  mixed[] $data        table cell data.
253
+	 * @param  mixed[] $columnSpecs table columns cell settings.
254
+	 *
255
+	 * @return void
256
+	 */
257
+	private function createTableBody($data, $columnSpecs)
258
+	{
259
+		$this->setTableData($data, $columnSpecs);
260
+		if (isset($this->tableBody)) {
261
+			$this->tableBody = "\n<tbody>" . $this->tableBody . "\n</tbody>";
262
+		}
263
+	}
264
+
265
+	/**
266
+	 * Helper method to set table data in table body.
267
+	 *
268
+	 * Sets table data according to the column specifications, if it is
269
+	 * specified. Otherwise it sets the data as it is stored in the data array.
270
+	 *
271
+	 * @param  mixed[] $data        table cell data.
272
+	 * @param  mixed[] $columnSpecs table columns cell settings.
273
+	 *
274
+	 * @return void
275
+	 */
276
+	private function setTableData($data, $columnSpecs)
277
+	{
278
+		if (empty($columnSpecs)) {
279
+			$this->setTableDataFromData($data);
280
+		} else {
281
+			$this->setTableDataAsSpecified($data, $columnSpecs);
282
+		}
283
+	}
284
+
285
+	/**
286
+	 * Helper method to set table data from the data array.
287
+	 *
288
+	 * Sets table data from the data array.
289
+	 *
290
+	 * @param  mixed[]  $data   table cell data.
291
+	 *
292
+	 * @return void
293
+	 */
294
+	private function setTableDataFromData($data)
295
+	{
296
+		foreach ($data as $row) {
297
+			$this->tableBody .= "\n<tr>";
298
+			foreach ($row as $value) {
299
+				$this->tableBody .= "\n<td>";
300
+				$this->tableBody .= $value;
301
+				$this->tableBody .= "</td>";
302
+			}
303
+			$this->tableBody .= "\n</tr>";
304
+		}
305
+	}
306
+
307
+	/**
308
+	 * Helper method to set table data according to the column specifications.
309
+	 *
310
+	 * Sets the table data according to the column specifications, if the cell
311
+	 * does not belong to the footer. Adds a colspan tag, if it is specified
312
+	 * for the cell in the column.
313
+	 *
314
+	 * @param  mixed[] $data         table cell data.
315
+	 * @param  mixed[] $columnSpecs  table columns cell settings.
316
+	 *
317
+	 * @return void
318
+	 */
319
+	private function setTableDataAsSpecified($data, $columnSpecs)
320
+	{
321
+		foreach ($data as $row) {
322
+			$this->tableBody .= "\n<tr>";
323
+			foreach ($columnSpecs as $key => $columnSpec) {
324
+				if (!$this->isTableFooter($columnSpec)) {
325
+					$colspan = $this->getColspan($columnSpec);
326
+					$this->tableBody .= "\n<td{$colspan}>";
327
+					$this->tableBody .= $this->getValue($row, $key, $columnSpec);
328
+					$this->tableBody .= "</td>";
329
+				}
330
+			}
331
+			$this->tableBody .= "\n</tr>";
332
+		}
333
+	}
334
+
335
+	/**
336
+	 * Helper method to get the colspan value, if specified in the column
337
+	 * specification for the cell.
338
+	 *
339
+	 * @param  mixed[]  $columnSpec cell settings for one column.
340
+	 *
341
+	 * @return int the colspan value if specified. Otherwise null.
342
+	 */
343
+	private function getColspan($columnSpec)
344
+	{
345
+		return isset($columnSpec['colspan']) ? " colspan='{$columnSpec['colspan']}'" : null;
346
+	}
347
+
348
+	/**
349
+	 * Helper method to get the value for a specific position in one row in
350
+	 * the data array.
351
+	 *
352
+	 * Gets the data from a specific position in one row in the data array.
353
+	 * If a function is specified for the cell in the column, the data is
354
+	 * runned through the function before it is returned.
355
+	 *
356
+	 * @param  mixed[]  $row        one row of in the array of table data.
357
+	 * @param  string   $key        the name of the key in the associative data array.
358
+	 * @param  mixed[]  $columnSpec cell settings for one column.
359
+	 */
360
+	private function getValue($row, $key, $columnSpec)
361
+	{
362
+		if ($this->isFunctionSpecified($columnSpec)) {
363
+			$dataValue = isset($row[$key]) ? $row[$key] : "";
364
+			return $this->getValueThroughFunction($columnSpec, $dataValue);
365
+		} else {
366
+			return isset($row[$key]) ? $row[$key] : "";
367
+		}
368
+	}
369
+
370
+	/**
371
+	 * Helper method t check if the function tag is specified for the cells in
372
+	 * one column.
373
+	 *
374
+	 * Checks if the function tag is set for the cell in one column.
375
+	 *
376
+	 * @param  mixed[]  $columnSpec     cell settings for one column.
377
+	 *
378
+	 * @return boolean true if a function is connected to the cell, false otherwise.
379
+	 */
380
+	private function isFunctionSpecified($columnSpec)
381
+	{
382
+		return isset($columnSpec['function']) ? true : false;
383
+	}
384
+
385
+	/**
386
+	 * Helper method to run the value through a function before it is returned.
387
+	 *
388
+	 * Runs the value through a function, if a function is connected to the cell
389
+	 * in the column. If not function is connected to the cell through the
390
+	 * column specification, the value is returned as it is.
391
+	 *
392
+	 * @param mixed[]   $columnSpec     cell settings for one column
393
+	 * @param mixed     $dataValue      the value to run through function, if specified.
394
+	 *
395
+	 * @return the value.
396
+	 */
397
+	private function getValueThroughFunction($columnSpec, $dataValue)
398
+	{
399
+		if (!empty($columnSpec['function'])) {
400
+			return call_user_func($columnSpec['function'], $dataValue);
401
+		} else {
402
+			return $dataValue;
403
+		}
404
+	}
405
+
406
+	/**
407
+	 * Helper method to create table footer with data.
408
+	 *
409
+	 * Creates table footer if the cell settings for the column is set to
410
+	 * footer in the column specifications.
411
+	 * Adds a colspan tag, if it is specified for the cell in the column.
412
+	 *
413
+	 * @param  mixed[] $columnSpecs table columns cell settings.
414
+	 *
415
+	 * @return void
416
+	 */
417
+	private function createTableFooter($columnSpecs)
418
+	{
419
+		foreach ($columnSpecs as $columnSpec) {
420
+			if ($this->isTableFooter($columnSpec)) {
421
+				$colspan = $this->getColspan($columnSpec);
422
+				$this->tableFoot .= "\n<td{$colspan}>";
423
+				$this->tableFoot .= $this->getFooterData($columnSpec);
424
+				$this->tableFoot .= "</td>";
425
+			}
426
+		}
427
+
428
+		if (isset($this->tableFoot)) {
429
+			$this->tableFoot = "\n<tfoot>\n<tr>" . $this->tableFoot . "\n</tr>\n</tfoot>";
430
+		}
431
+	}
432
+
433
+	/**
434
+	 * Helper method to get table footer data.
435
+	 *
436
+	 * Gets table footer data from the column specification. Checks if the
437
+	 * value should be fetched from a function or from the value tag.
438
+	 * If either the function or the value specified, an empty string is
439
+	 * returned.
440
+	 *
441
+	 * @param  mixed[] $columnSpec  cell settings for one column.
442
+	 *
443
+	 * @return mixed    the cell data value.
444
+	 */
445
+	private function getFooterData($columnSpec)
446
+	{
447
+		if ($this->isFunctionSpecified($columnSpec)) {
448
+			return call_user_func($columnSpec['function']);
449
+		} else {
450
+			return isset($columnSpec['value']) ? $columnSpec['value'] : "";
451
+		}
452
+	}
453
+
454
+	/**
455
+	 * Gets the table.
456
+	 *
457
+	 * Gets the table with table data.
458
+	 *
459
+	 * @return html     the table with table data.
460
+	 */
461
+	public function getHTMLTable()
462
+	{
463
+		$id = isset($this->tableSpec['id']) ? " id='{$this->tableSpec['id']}'" : null;
464
+		$class = isset($this->tableSpec['class']) ? " class='{$this->tableSpec['class']}'" : null;
465
+		$caption = isset($this->tableSpec['caption']) ? "<caption>{$this->tableSpec['caption']}</caption>" : null;
466
+
467
+		$htmlTable = "<table{$id}{$class}>";
468
+		$htmlTable .= $caption;
469
+		$htmlTable .= $this->tableHead;
470
+		$htmlTable .= $this->tableBody;
471
+		$htmlTable .= $this->tableFoot;
472
+		$htmlTable .= "\n</table>";
473
+
474
+		return $htmlTable;
475
+	}
476 476
 }
Please login to merge, or discard this patch.