dbt/table

用于表示固定长度表格数据的对象

1.0.10 2021-08-17 16:48 UTC

This package is auto-updated.

Last update: 2024-09-17 23:20:18 UTC


README

一组用于表示具有固定宽度行的表格数据的对象。值以字符串形式表示。

安装

您可以通过composer安装此包

composer require dbt/table

使用方法

创建新表

use Dbt\Table\Table;
use Dbt\Table\Row;
use Dbt\Table\Cell;

/**
 * Standard construction.
 */
$table = new Table(
    new Row(
        new Cell('row 0 / cell 0'),
        new Cell('row 0 / cell 1')
    ),
    new Row(
        new Cell('row 1 / cell 0'),
        new Cell('row 1 / cell 1')
    )
);

/**
 * From array. 
 */
$table = Table::fromArray([
    ['row 0 / cell 0', 'row 0 / cell 1'],
    ['row 1 / cell 0', 'row 1 / cell 1'],
]);

/*
 * Rows must have the same length. This will throw a LengthException:
 */
$table = Table::fromArray([
    ['row 0, cell 0', 'row 0, cell 1'],
    ['row 1, cell 0'], // Too short!
]);

表和行是列表对象,可以遍历并推送内容

use Dbt\Table\Table;
use Dbt\Table\Row;
use Dbt\Table\Cell;

$table = Table::fromArray([['row 0 / cell 0'], ['row 1 / cell 0']]);

/**
 * @var int $rowIndex
 * @var \Dbt\Table\Row $row 
 */
foreach ($table as $rowIndex => $row) {
    var_dump(count($table), count($row));
    
    /**
     * @var int $cellIndex
     * @var \Dbt\Table\Cell $cell 
     */
    foreach ($row as $cellIndex => $cell) {
        var_dump($cell->value());
    }
}

$table->push(new Row(new Cell('row 2 / cell 0')));

单元格可以从标量或标量数组创建

use \Dbt\Table\Cell;

/**
 * This int will be cast to a string. 
 */
$cell = Cell::make(1);

/**
 * This array will be serialized to a JSON string. 
 */
$cell = Cell::make(['string', 1, 9.9, true]);

表、行和单元格在被设置后不应修改。尽管您可以向表和行推送内容,但无法修改现有值。

等等。

有关详细信息,请参阅 CONTRIBUTING。MIT许可证(MIT)。有关更多信息,请参阅 许可证文件