Untitled
unknown
plain_text
3 years ago
1.7 kB
14
Indexable
<?php
class Row
{
private array $values;
public function __construct(array $array)
{
$this->values = $array;
}
public function getSize()
{
return sizeof($this->values);
}
public function getValues()
{
return $this->values;
}
}
class Table
{
private Row $header;
private array $rows;
public function __construct(Row $header)
{
$this->header = $header;
}
public function addRow(Row $row): Table
{
if ($this->header->getSize() == $row->getSize())
$this->rows[] = $row;
return $this;
}
public function switchRow(int $cellFirst, int $cellLast): Table
{
$temp = $this->rows[$cellFirst];
$this->rows[$cellFirst] = $this->rows[$cellLast];
$this->rows[$cellLast] = $temp;
return $this;
}
public function toHtml(): string
{
$string = '<table><thead>';
foreach ($this->header->getValues() as $cell)
$string .= "<td>$cell</td>";
$string .= "</thead><tbody>";
foreach ($this->rows as $row) {
$string .= "<tr>";
foreach ($row->getValues() as $cell) {
$string .= "<td>$cell</td>";
}
$string .= "</tr>";
}
$string .= "</tbody></table>";
return $string;
}
}
$table=new Table(new Row(['name','family','age']));
$table->addRow(new Row(['mohammad','mohammdi',24]))->addRow(new Row(['ahmad','ahmadi',20]));
$table->switchRow(0, 1);
echo ($table->toHtml());Editor is loading...