大腾刺/cellular

此包已被废弃且不再维护。作者建议使用phpbench/cellular包。

表示和分析表格数据

0.3 2015-06-17 11:12 UTC

This package is auto-updated.

Last update: 2023-10-23 15:48:56 UTC


README

已废弃:请使用Tabular。

Build Status Scrutinizer Code Quality

cellular库提供了一种面向对象的方式来构建、表示和分析表格数据。

特性

  • 支持聚合函数sumavgminmaxmedian
  • 聚合函数可以应用于TableRowColumn
  • 支持单元格分组。
  • 可以将回调应用于整个或选定组的TableRowColumn实例的单元格。
  • 使用回调生成分组表 - 类似于SELECT bar, SUM(foo) FROM sometable GROUP BY bar
  • 流畅的表格构建器

注意,此库处于开发中。

创建

列 1 列 2 列 3
12 14 4
12 14 4

将按以下方式创建

$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)

或无需构建器

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

检索单元格值

您可以按以下方式从TableRowColumn实例检索值

$table->getValues(); // return an array containg all cell values
$table->getRow(0)->getValues();
$table->getColumn('col1')->getValues();

分组

您可以将分组应用于单元格

$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"

使用计算器

计算器允许您将某些计算应用于值、Cell实例或任何CellularInterface实例。

$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"

当前函数

  • sum:返回值的总和
  • min:返回最小值
  • max:返回最大值
  • mean:返回平均值
  • median:返回中值
  • deviation:返回偏差百分比

分区和分支

TableRow实例提供以下方法

  • partition:根据给定的回调内部划分元素集合。
  • aggregate:将表的分区汇总回单个分区。

这对于从多个表或行创建摘要很有用

例如

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

回调按顺序传递每个分区以及集合类的新实例。回调的职责是将新行添加到新实例中,该实例的主分区将成为执行操作时集合的新主分区。

结果将类似于以下SQL:SELECT SUM(number) FROM table GROUP BY class

排序

可以通过以下方式实现排序

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

评估值

值可以按以下方式评估

$sum = $table->evaluate(function ($row, $lastValue) {
    $lastValue += $row['cell']->getValue();
}, 0); // evaluates the sum of the column "cell"

回调被传递元素和上一个结果。初始结果作为evaluate的第二个参数给出。

设置属性

可以在CellCellular实例上设置属性。这在您需要存储关于数据来源的元数据,在将其转换为cellular形式之前很有用。

$table->setAttribute('foo', 'bar');
$table->getAttribute('foo');
$table->hasAttribute('foo');

其他方法

  • filter:通过闭包过滤结果。返回一个新的实例。
  • clear:清除集合