| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html; |
| 4 |
|
| 5 |
use RuntimeException; |
| 6 |
use stdClass; |
| 7 |
|
| 8 |
class Table extends BaseHtmlElement |
| 9 |
{ |
| 10 |
protected $contentSeparator = "\n"; |
| 11 |
|
| 12 |
/** @var string */ |
| 13 |
protected $tag = 'table'; |
| 14 |
|
| 15 |
/** @var HtmlElement */ |
| 16 |
private $caption; |
| 17 |
|
| 18 |
/** @var HtmlElement */ |
| 19 |
private $header; |
| 20 |
|
| 21 |
/** @var HtmlElement */ |
| 22 |
private $body; |
| 23 |
|
| 24 |
/** @var HtmlElement */ |
| 25 |
private $footer; |
| 26 |
|
| 27 |
public function addHtml(ValidHtml ...$content) |
| 28 |
{ |
| 29 |
foreach ($content as $html) { |
| 30 |
if ($html instanceof BaseHtmlElement) { |
| 31 |
switch ($html->getTag()) { |
| 32 |
case 'tr': |
| 33 |
$this->getBody()->addHtml($html); |
| 34 |
|
| 35 |
break; |
| 36 |
case 'thead': |
| 37 |
parent::addHtml($html); |
| 38 |
$this->header = $html; |
| 39 |
|
| 40 |
break; |
| 41 |
case 'tbody': |
| 42 |
parent::addHtml($html); |
| 43 |
$this->body = $html; |
| 44 |
|
| 45 |
break; |
| 46 |
case 'tfoot': |
| 47 |
parent::addHtml($html); |
| 48 |
$this->footer = $html; |
| 49 |
|
| 50 |
break; |
| 51 |
case 'caption': |
| 52 |
if ($this->caption === null) { |
| 53 |
$this->prependHtml($html); |
| 54 |
$this->caption = $html; |
| 55 |
} else { |
| 56 |
throw new RuntimeException( |
| 57 |
'Tables allow only one <caption> tag' |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
break; |
| 62 |
default: |
| 63 |
$this->getBody()->addHtml(static::row([$html])); |
| 64 |
} |
| 65 |
} else { |
| 66 |
$this->getBody()->addHtml(static::row([$html])); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @param mixed $content |
| 75 |
* @return $this |
| 76 |
*/ |
| 77 |
public function add($content) |
| 78 |
{ |
| 79 |
if ($content instanceof stdClass) { |
| 80 |
$this->getBody()->addHtml(static::row((array) $content)); |
| 81 |
} elseif (is_iterable($content)) { |
| 82 |
$this->getBody()->addHtml(static::row($content)); |
| 83 |
} elseif ($content instanceof ValidHtml) { |
| 84 |
$this->addHtml($content); |
| 85 |
} else { |
| 86 |
$this->getBody()->addHtml(static::row([$content])); |
| 87 |
} |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the table title |
| 94 |
* |
| 95 |
* Will be rendered as a "caption" HTML element |
| 96 |
* |
| 97 |
* @param mixed $caption |
| 98 |
* @return $this |
| 99 |
*/ |
| 100 |
public function setCaption($caption) |
| 101 |
{ |
| 102 |
if ($caption instanceof BaseHtmlElement && $caption->getTag() === 'caption') { |
| 103 |
$this->caption = $caption; |
| 104 |
$this->prependHtml($caption); |
| 105 |
} elseif ($this->caption === null) { |
| 106 |
$this->caption = new HtmlElement('caption', null, ...Html::wantHtmlList($caption)); |
| 107 |
$this->prependHtml($this->caption); |
| 108 |
} else { |
| 109 |
$this->caption->setContent($caption); |
| 110 |
} |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Static helper creating a tr element |
| 117 |
* |
| 118 |
* @param Attributes|array $attributes |
| 119 |
* @param Html|array|string $content |
| 120 |
* @return HtmlElement |
| 121 |
*/ |
| 122 |
public static function tr($content = null, $attributes = null) |
| 123 |
{ |
| 124 |
return Html::tag('tr', $attributes, $content); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Static helper creating a th element |
| 129 |
* |
| 130 |
* @param Attributes|array $attributes |
| 131 |
* @param Html|array|string $content |
| 132 |
* @return HtmlElement |
| 133 |
*/ |
| 134 |
public static function th($content = null, $attributes = null) |
| 135 |
{ |
| 136 |
return Html::tag('th', $attributes, $content); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Static helper creating a td element |
| 141 |
* |
| 142 |
* @param Attributes|array $attributes |
| 143 |
* @param Html|array|string $content |
| 144 |
* @return HtmlElement |
| 145 |
*/ |
| 146 |
public static function td($content = null, $attributes = null) |
| 147 |
{ |
| 148 |
return Html::tag('td', $attributes, $content); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @param $row |
| 153 |
* @param null $attributes |
| 154 |
* @param string $tag |
| 155 |
* @return HtmlElement |
| 156 |
*/ |
| 157 |
public static function row($row, $attributes = null, $tag = 'td') |
| 158 |
{ |
| 159 |
$tr = static::tr(); |
| 160 |
foreach ((array) $row as $value) { |
| 161 |
$tr->addHtml(Html::tag($tag, null, $value)); |
| 162 |
} |
| 163 |
|
| 164 |
if ($attributes !== null) { |
| 165 |
$tr->setAttributes($attributes); |
| 166 |
} |
| 167 |
|
| 168 |
return $tr; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @return HtmlElement |
| 173 |
*/ |
| 174 |
public function getBody() |
| 175 |
{ |
| 176 |
if ($this->body === null) { |
| 177 |
$this->addHtml(Html::tag('tbody')->setSeparator("\n")); |
| 178 |
} |
| 179 |
|
| 180 |
return $this->body; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @return HtmlElement |
| 185 |
*/ |
| 186 |
public function getHeader() |
| 187 |
{ |
| 188 |
if ($this->header === null) { |
| 189 |
$this->addHtml(Html::tag('thead')->setSeparator("\n")); |
| 190 |
} |
| 191 |
|
| 192 |
return $this->header; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @return HtmlElement |
| 197 |
*/ |
| 198 |
public function getFooter() |
| 199 |
{ |
| 200 |
if ($this->footer === null) { |
| 201 |
$this->addHtml(Html::tag('tfoot')->setSeparator("\n")); |
| 202 |
} |
| 203 |
|
| 204 |
return $this->footer; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @return HtmlElement |
| 209 |
*/ |
| 210 |
public function nextBody() |
| 211 |
{ |
| 212 |
$this->body = null; |
| 213 |
|
| 214 |
return $this->getBody(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @return HtmlElement |
| 219 |
*/ |
| 220 |
public function nextHeader() |
| 221 |
{ |
| 222 |
$this->header = null; |
| 223 |
|
| 224 |
return $this->getHeader(); |
| 225 |
} |
| 226 |
} |