/** * Replace column by another column * * @param Column $col * @param string $header * @return mechanicious\Tableman\Tableman */ public function replaceColumn(Column $col, $header) { if (!in_array($header, array_keys($this->items))) { throw new \Exception("column {$header} don't exist in " . implode(', ', array_keys($this->items))); } $this->items[$header] = $col; if ($header !== $col->getHeader()) { $this->renameColumns(array($header => $col->getHeader())); } return $this; }
public function testEachColumn() { // We'll try to rename columns while looping through the items and at the // same time we'll try to replace columns. $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray(); $tableman = new \mechanicious\Tableman\Tableman($columnBag); $tableman->eachColumn(function (&$ref, &$column, $header) { // If you actually want to make changes then make sure // you **reference** items! // Replace the id column if ($header === 'id') { $column = new \mechanicious\Columnizer\Column(array(3, 4), 'id'); } // Renaming columns while still in the loop if (isset($ref['id'])) { $ref->renameColumns(array('id' => 'identification', 'name' => 'firstname', 'age' => 'level', 'hobby' => 'likes')); } // Again replace the column, note that the column has different name than the // name of the column we're replacing, thus the name changes as well. if (in_array('identification', $ref->getColumnHeaders())) { $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6, 7), '#'), 'identification'); } }); $keyHeaders = $tableman->getColumnHeaders(); $columnHeaders = array_map(function ($column) { return $column->getHeader(); }, $tableman->getColumns()); $this->assertEquals($keyHeaders, array_values($columnHeaders)); $this->assertEquals($this->cleanWhiteSpace($tableman->toJson()), $this->cleanWhiteSpace(' { "#":[6,7], "firstname":["Joe","Tony"], "level":[25,27], "likes":[null,"sport"] } ')); }