1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Foo\Grid\Table; |
6
|
|
|
|
7
|
|
|
use Foo\Grid\Collection\Cells; |
8
|
|
|
use Foo\Grid\Collection\Rows; |
9
|
|
|
use Foo\Grid\Component\Component; |
10
|
|
|
use Foo\Translate\ITranslator; |
11
|
|
|
|
12
|
|
|
class Table extends Component implements ITable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* %1$s - thead - rows |
16
|
|
|
* %2$s - tbody - headers |
17
|
|
|
*/ |
18
|
|
|
const TEMPLATE_CONTENT = '%1$s%2$s'; |
19
|
|
|
|
20
|
|
|
const TAG_TABLE = 'table'; |
21
|
|
|
const TAG_HEADERS = 'thead'; |
22
|
|
|
const TAG_HEADER_CELL = 'th'; |
23
|
|
|
const TAG_ROWS = 'tbody'; |
24
|
|
|
|
25
|
|
|
/** @var Cells */ |
26
|
|
|
protected $headers; |
27
|
|
|
|
28
|
|
|
/** @var Rows */ |
29
|
|
|
protected $rows; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Rows $rows |
33
|
|
|
* @param Cells $headers |
34
|
|
|
* @param array $attributes |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct(Rows $rows, Cells $headers, array $attributes = []) |
37
|
|
|
{ |
38
|
5 |
|
$this->rows = $rows; |
39
|
5 |
|
$this->headers = $headers; |
40
|
|
|
|
41
|
5 |
|
parent::__construct('', static::TAG_TABLE, $attributes); |
42
|
5 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Cells |
46
|
|
|
*/ |
47
|
|
|
public function getHeader(): Cells |
48
|
|
|
{ |
49
|
|
|
return $this->headers; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Rows |
54
|
|
|
*/ |
55
|
|
|
public function getRows(): Rows |
56
|
|
|
{ |
57
|
|
|
return $this->rows; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $num |
62
|
|
|
* @param string $whitespace |
63
|
|
|
*/ |
64
|
2 |
|
public function setIndentation(int $num, string $whitespace = ' ') |
65
|
|
|
{ |
66
|
2 |
|
foreach ($this->headers as $header) { |
67
|
1 |
|
$header->setIndentation($num + 1, $whitespace); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
foreach ($this->rows as $row) { |
71
|
1 |
|
$row->setIndentation($num + 1, $whitespace); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->indentation = str_repeat($whitespace, $num); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ITranslator $translator |
79
|
|
|
*/ |
80
|
1 |
|
public function setTranslator(ITranslator $translator) |
81
|
|
|
{ |
82
|
1 |
|
foreach ($this->headers as $header) { |
83
|
1 |
|
$header->setTranslator($translator); |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function __toString(): string |
91
|
|
|
{ |
92
|
2 |
|
$thead = (string)$this->headers; |
93
|
2 |
|
$tbody = (string)$this->rows; |
94
|
|
|
|
95
|
2 |
|
$this->content = sprintf( |
96
|
2 |
|
static::TEMPLATE_CONTENT, |
97
|
|
|
$thead, |
98
|
|
|
$tbody |
99
|
|
|
); |
100
|
|
|
|
101
|
2 |
|
return parent::__toString(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|