*/ protected iterable $iterator; /** * The string and/or integer keys of the columns to pick and reorder from the table. * * @var list */ protected array $columns; /** * @param iterable $iterator * @param (string|int)[] $columns */ public function __construct(iterable $iterator, array $columns) { $this->iterator = $iterator; $this->columns = array_values($columns); } /** * Return an iterator for the records in the data table. * * @return \Generator */ public function getIterator() : Traversable { foreach ($this->iterator as $i => $record) { $picked = []; foreach ($this->columns as $column) { if (!isset($record[$column])) { throw new RuntimeException("Column '$column' not found" . " at row offset $i."); } $picked[$column] = $record[$column]; } yield $picked; } } }