Skip to content

Commit

Permalink
Get array with re-ordered columns
Browse files Browse the repository at this point in the history
  • Loading branch information
onlinesid committed Apr 26, 2018
1 parent c1717e2 commit 1a625a9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A PHP library to transform rows into an aggregated table",
"minimum-stability": "dev",
"license": "MIT",
"version": "1.0.4",
"version": "1.0.5",
"authors": [
{
"name": "Sid Bachtiar",
Expand Down
64 changes: 63 additions & 1 deletion src/OnlineSid/DataTabulator/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,71 @@ public function getRowID($index)
return $this->unique_rows[$keys[$index-1]]['id'];
}

/**
* @param array $new_orders
*/
private function reorderUniqueColumns(array $new_orders)
{
$new_columns = [];
$new_data = [];

$maps = [];

foreach ($new_orders as $i => $id)
{
// reorder unique columns
$old_index = 0;
foreach ($this->unique_columns as $old_key => $old_col) {
if ($old_key == $id) {
$new_columns[$id] = $this->unique_columns[$id];

// map from old to new index
$maps[$old_index+1] = $i+1;

continue;
}
$old_index++;
}
}

// we also need to re-order $this->data
foreach ($this->data as $old_row) {
$new_row = [
$old_row[0],
];

for ($old_index = 1; $old_index < count($old_row); $old_index++) {
$new_row[$maps[$old_index]] = $old_row[$old_index];
}

$new_data[] = $new_row;
}

unset($this->unique_columns);
unset($this->data);
$this->data = $new_data;
$this->unique_columns = $new_columns;
}

/**
* @param bool $include_headers
* @param bool $return_labels if false the row/col IDs will be returned rather than the labels
* @param null|array $col_ids_order order based on col IDs
* @return array
*/
public function toArray($include_headers, $return_labels=true)
public function toArray($include_headers, $return_labels=true, $col_ids_order=null)
{
$arr = [];

if ($col_ids_order !== null) {
// preserve
$unique_columns = $this->unique_columns;
$data = $this->data;

// change the orders
$this->reorderUniqueColumns($col_ids_order);
}

// headers
if ($include_headers) {
$row = [];
Expand All @@ -182,6 +238,12 @@ public function toArray($include_headers, $return_labels=true)
$arr[] = $row;
}

if ($col_ids_order !== null) {
// restore
$this->unique_columns = $unique_columns;
$this->data = $data;
}

return $arr;
}
}
70 changes: 69 additions & 1 deletion tests/OnlineSid/DataTabulator/DataTabulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,64 @@

class DataTabulatorTest extends PHPUnit_Framework_TestCase
{
/**
* Testing a bit more complex columns reordering
*/
function testReorder()
{
$rows = [
['id' => 7, 'u_id' => 1, 'u_name' => 'Joan', 'a_id' => 'PK', 'a_name' => 'Packing', 'num' => 10.5, ],
['id' => 4, 'u_id' => 1, 'u_name' => 'Joan', 'a_id' => 'PK', 'a_name' => 'Packing', 'num' => 0.5, ],
['id' => 2, 'u_id' => 1, 'u_name' => 'Joan', 'a_id' => 'DR', 'a_name' => 'Driving', 'num' => 2.3, ],
['id' => 5, 'u_id' => 2, 'u_name' => 'Robb', 'a_id' => 'DR', 'a_name' => 'Driving', 'num' => 8.7, ],
['id' => 15, 'u_id' => 2, 'u_name' => 'Robb', 'a_id' => 'LF', 'a_name' => 'Lifting', 'num' => 7, ],
];
$tabulator = new DataTabulator($rows);

$table = $tabulator->to2DTable('Name', 'u_id', 'u_name', 'a_id', 'a_name', 'num');

// Expected result ($table) is something like:
//
// Name Packing (PK) Driving (DR) Lifting (LF)
// Joan (1) 11 2.3 0
// Robb (2) 0 8.7 7

$this->assertEquals(3, $table->getNbRows());
$this->assertEquals(4, $table->getNbColumns());


// testing with headers included with reverse row IDs ordering
$arr = $table->toArray(true, false, ['LF', 'DR', 'PK',]);


// Expected result ($arr) is something like:
//
// Name Lifting (LF) Driving (DR) Packing (PK)
// Joan (1) 0 2.3 11
// Robb (2) 7 8.7 0


// first row
$this->assertEquals('Name', $arr[0][0]);
$this->assertEquals('LF', $arr[0][1]);
$this->assertEquals('DR', $arr[0][2]);
$this->assertEquals('PK', $arr[0][3]);
// second row
$this->assertEquals(1, $arr[1][0]);
$this->assertEquals(0, $arr[1][1]);
$this->assertEquals(2.3, $arr[1][2]);
$this->assertEquals(11, $arr[1][3]);
// third row
$this->assertEquals(2, $arr[2][0]);
$this->assertEquals(7, $arr[2][1]);
$this->assertEquals(8.7, $arr[2][2]);
$this->assertEquals(0, $arr[2][3]);
}

/**
* Testing a simple scenario
*/
function test1()
function testSimple()
{
$rows = [
['id' => 7, 'u_id' => 1, 'u_name' => 'Joan', 'a_id' => 'PK', 'a_name' => 'Packing', 'num' => 10.5, ],
Expand Down Expand Up @@ -90,5 +144,19 @@ function test1()
$this->assertEquals(0, $arr[2][1]);
$this->assertEquals(8.7, $arr[2][2]);

// testing with headers included with reverse row IDs ordering
$arr = $table->toArray(true, false, ['DR', 'PK',]);
// first row
$this->assertEquals('Name', $arr[0][0]);
$this->assertEquals('DR', $arr[0][1]);
$this->assertEquals('PK', $arr[0][2]);
// second row
$this->assertEquals(1, $arr[1][0]);
$this->assertEquals(2.3, $arr[1][1]);
$this->assertEquals(11, $arr[1][2]);
// third row
$this->assertEquals(2, $arr[2][0]);
$this->assertEquals(8.7, $arr[2][1]);
$this->assertEquals(0, $arr[2][2]);
}
}

0 comments on commit 1a625a9

Please sign in to comment.