Skip to content

Commit

Permalink
Added Table::getAggregateValueFromRowCol
Browse files Browse the repository at this point in the history
  • Loading branch information
onlinesid committed Apr 26, 2018
1 parent 9165a84 commit 3c48393
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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.2",
"version": "1.0.3",
"authors": [
{
"name": "Sid Bachtiar",
Expand Down
41 changes: 41 additions & 0 deletions src/OnlineSid/DataTabulator/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ public function getNbRows() {
return $this->nb_rows;
}

/**
* Returns aggregate value, given row index and column index (they are NOT row ID and col ID!!!)
*
* @param int $r row index, starts from 1 because 0 is the row label
* @param int $c col index, starts from 1 because 0 is the col label
* @return mixed
*/
public function getAggregateValueFromRowCol($r, $c)
{
$columns = array_keys($this->unique_columns);
$rows = array_keys($this->unique_rows);

return $this->getAggregateValue($rows[$r-1], $columns[$c-1]);
}

/**
* @param mixed $row_id
* @param mixed $col_id
Expand Down Expand Up @@ -113,4 +128,30 @@ public function getRowLabel($index)
return $this->unique_rows[$keys[$index-1]]['name'];
}

/**
* @param bool $include_headers
* @return array
*/
public function toArray($include_headers)
{
$arr = [];

// headers
if ($include_headers) {
$row = [];
for ($c=0; $c < $this->getNbColumns(); $c++) {
$row[] = $this->getColumnLabel($c);
}
$arr[] = $row;
}

for ($r=0; $r < $this->getNbRows(); $r++) {
$row = [$this->getRowLabel($r)];
for ($c=0; $c < $this->getNbColumns(); $c++) {

}
}

return $arr;
}
}
5 changes: 5 additions & 0 deletions tests/OnlineSid/DataTabulator/DataTabulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ function test1()
$this->assertEquals( 'Name', $table->getRowLabel(0));
$this->assertEquals( 'Joan', $table->getRowLabel(1));
$this->assertEquals( 'Robb', $table->getRowLabel(2));

$this->assertEquals(11, $table->getAggregateValueFromRowCol(1, 1));
$this->assertEquals(0, $table->getAggregateValueFromRowCol(2, 1));
$this->assertEquals(2.3, $table->getAggregateValueFromRowCol(1, 2));
$this->assertEquals(8.7, $table->getAggregateValueFromRowCol(2, 2));
}
}

0 comments on commit 3c48393

Please sign in to comment.