I have three Classes: Table, Row, and Cell.
Table has an array of Rows and an array with order of columns.
Row has an array of Cells and Cell has textValue.
Both Table and Row implements IteratorAggregate.
My problem is that Table has an order of columns specify by the user, but when the program iterate on rows, it returns the cells in the order they were added. I tried to implement Iterator instead of IteratorAggregate but I do not know how to pass the array that table has with order of column.
I want to keep the iteration as simple as possible, with this I meant that I want to be able to foreach the Table and Row.
<?php
class Table implements \IteratorAggregate
{
private $order;
private $rows;
public function __construct()
{
$this->rows = [];
$this->order = [];
}
public function addRow(Row $row){
array_push($this->rows, $row);
}
public function setOrder(Array $order){
$this->order = $order;
}
public function getIterator()
{
return new \ArrayIterator($this->rows);
}
}
class Row implements \IteratorAggregate
{
private $cells;
public function __construct()
{
$this->cells = [];
}
public function addCell(Cell $cell){
array_push($this->cells, $cell);
}
public function getIterator()
{
return new \ArrayIterator($this->cells);
}
}
class Cell extends Model{
public $text;
public function __construct($text = "")
{
$this->text = $text;
}
}
An example would be having a row with cells = [a,b,c]. Iterator always will give a,b,c. But the order could be specify in the table class and print something like b,c,a or c,a,b.
Rowthat takes the order into account.