dev-refactor_api
dev-refactor_apiRepresent and analyze tabular data
MIT
The Development Requires
by Daniel Leech
Represent and analyze tabular data
ABANDONED: Use Tabular instead., (*1)
The cellular library provides an object oriented way of building, representing and analyzing tabular data., (*3)
Features:, (*4)
sum
, avg
, min
, max
and median
.Table
, Row
and Column
.Table
,
Row
and Column
instances.SELECT bar, SUM(foo) FROM sometable GROUP BY bar
Note that this library is under development., (*5)
Col 1 | Col 2 | Col 3 |
---|---|---|
12 | 14 | 4 |
12 | 14 | 4 |
Would be created as follows:, (*6)
$table = Table::create(); $table->createAndAddRow() ->set('col1', 12) ->set('col2', 14) ->set('col3', 4); $table->createAndAddRow() ->set('col1', 12) ->set('col2', 14) ->set('col3', 4)
Or without the builder:, (*7)
$table = new Table( new Row(array( 'col1' => new Cell(12), 'col2' => new Cell(14), 'col3' => new Cell(4), )), new Row(array( 'col1' => new Cell(12), 'col2' => new Cell(14), 'col3' => new Cell(4), )), );
You can retrieve values from Table
, Row
and Column
instances as follows:, (*8)
$table->getValues(); // return an array containg all cell values $table->getRow(0)->getValues(); $table->getColumn('col1')->getValues();
You apply groups to cells:, (*9)
$table->createAndAddRow() ->set('hello, 'vaue', array('group1')); var_dump($table->getCells(array('group1'))); // dump all cells in group1 $table->mapValues(function ($value) { return 'goodbye'; }, array('group1')); // set the value of all cells in group1 to "goodbye"
The calculator allows you to apply certain calculations to values, Cell
instances or any instance of CellularInterface
:, (*10)
$mean = Calculator::mean($table); // return the mean (average) value of the table $median = Calculator::median($table->getRow(0)); // return the median value of the first row $deviation = Calculator::deviation( Calculator::mean($table->getColumn('col1')), $table->getRow(0)->getCell('col1') ); // return the deviation of "col1" in the first row as a percentage from the average value of "col1"
Current functions:, (*11)
sum
: Return the sum of valuesmin
: Return the minimum valuemax
: Return the maximum valuemean
: Return the mean (average) valuemedian
: Return the median valuedeviation
: Return the deviation as a percentageTable
and Row
instances provide the following methods:, (*12)
partition
: Internally divide the collection of elements according to a
given callback.aggregate
: Aggregate the partitions of a table back to a single partition.This is useful for creating summaries from multiple tables or rows:, (*13)
For example:, (*14)
$table ->partition(function ($row) { return $row['class']; }) ->partition(function ($table, $newInstance) use ($cols, &$newCols, $options, $functions) { $newInstance->createAndAddRow() ->set( 'number', Calculator::sum($table->getColumn('number')) ); });
The callback is passed each partition in turn alongisde a new instance of the collection class. The callback's responsiblity is to add new rows to the new instance, the primary partition of which will become the new primary partition for the collection upon which the operation is performed., (*15)
The result will be anagous to the following SQL: SELECT SUM(number) FROM table GROUP BY class
., (*16)
Sorting can be achieved as follows:, (*17)
$table->sort(function ($row1, $row2) { return $row1['col1'] > $row2['col1']; });
Values can be evaluated as follows:, (*18)
$sum = $table->evaluate(function ($row, $lastValue) { $lastValue += $row['cell']->getValue(); }, 0); // evaluates the sum of the column "cell"
The callback is passed the element and the previous result. The initial result
is given as the second argument to evaluate
., (*19)
It is possible to set attributes on Cell
and Cellular
instances. This is
useful when you need to store metadata about the source of the data before it
was transformed into cellular form., (*20)
$table->setAttribute('foo', 'bar'); $table->getAttribute('foo'); $table->hasAttribute('foo');
filter
: Filter the results by a closure. Returns a new instance.clear
: Clear the collectionRepresent and analyze tabular data
MIT