Passed
Push — master ( be8931...3944bf )
by Richard
02:46
created

Table::toHtml()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 24
rs 8.8333
cc 7
nc 16
nop 1
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
7
use Illuminate\Support\Arr;
8
use Riclep\Storyblok\Field;
9
10
class Table extends Field
11
{
12
	/**
13
	 * @var string a class to apply to the <table> tag
14
	 */
15
	protected $cssClass;
16
17
	/**
18
	 * @var array|int the column numbers to convert to headers
19
	 */
20
	protected $headerColumns;
21
22
	public function __toString()
23
	{
24
		return $this->toHtml($this->content);
25
	}
26
27
	protected function toHtml($table) {
28
		$html = '<table ' . ($this->cssClass ? 'class="' . $this->cssClass . '"' : null) . '><thead><tr>';
29
30
		foreach ($table['thead'] as $header) {
31
			$html .= '<th>' . $header['value'] . '</th>';
32
		}
33
34
		$html .= '</tr></thead><tbody>';
35
36
		foreach ($table['tbody'] as $row) {
37
			$html .= '<tr>';
38
39
			foreach ($row['body'] as $column => $cell) {
40
				if ($this->headerColumns && in_array(($column + 1), Arr::wrap($this->headerColumns))) {
41
					$html .= '<th>' . $cell['value'] . '</th>';
42
				} else {
43
					$html .= '<td>' . $cell['value'] . '</td>';
44
				}
45
			}
46
47
			$html .= '</tr>';
48
		}
49
50
		return $html . '</tbody></table>';
51
	}
52
}