2017 © Pedro Peláez
 

library cellular

Represent and analyze tabular data

image

phpbench/cellular

Represent and analyze tabular data

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Cellular

ABANDONED: Use Tabular instead., (*1)

Build Status Scrutinizer Code Quality, (*2)

The cellular library provides an object oriented way of building, representing and analyzing tabular data., (*3)

Features:, (*4)

  • Supports aggregate functions sum, avg, min, max and median.
  • Aggreate functions can applied to Table, Row and Column.
  • Supports cell groups.
  • Callbacks can be applied to cells on whole, or selected groups of Table, Row and Column instances.
  • Produce grouped tables with callbacks - analagous to SELECT bar, SUM(foo) FROM sometable GROUP BY bar
  • Fluent table builder

Note that this library is under development., (*5)

Creating

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),
     )),
 );

Retrieving cell values

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();

Groups

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"

Using the Calculator

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 values
  • min: Return the minimum value
  • max: Return the maximum value
  • mean: Return the mean (average) value
  • median: Return the median value
  • deviation: Return the deviation as a percentage

Partitioning and Forking

Table 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

Sorting can be achieved as follows:, (*17)

$table->sort(function ($row1, $row2) {
    return $row1['col1'] > $row2['col1'];
});

Evaluating values

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)

Setting Attributes

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');

Other methods

  • filter: Filter the results by a closure. Returns a new instance.
  • clear: Clear the collection

The Versions

21/09 2015

dev-master

9999999-dev

Represent and analyze tabular data

  Sources   Download

MIT

The Requires

  • php ~5.4

 

27/06 2015

dev-materialize

dev-materialize

Represent and analyze tabular data

  Sources   Download

MIT

The Requires

  • php ~5.4

 

17/06 2015

0.3

0.3.0.0

Represent and analyze tabular data

  Sources   Download

MIT

The Requires

  • php ~5.4

 

13/06 2015

0.2.1

0.2.1.0

Represent and analyze tabular data

  Sources   Download

MIT

The Requires

  • php ~5.4

 

11/06 2015

0.2

0.2.0.0

Represent and analyze tabular data

  Sources   Download

MIT

The Requires

  • php ~5.4

 

25/05 2015

dev-refactor_api

dev-refactor_api

Represent and analyze tabular data

  Sources   Download

MIT

The Development Requires

25/05 2015

0.1

0.1.0.0

Represent and analyze tabular data

  Sources   Download

MIT

The Development Requires

17/05 2015

dev-refactoring

dev-refactoring

Represent and analyze tabular data

  Sources   Download

MIT