macropage / php-to-ascii-table
一个用于生成纯文本表格的PHP库。
v2.0
2020-01-21 09:14 UTC
Requires
- php: ^7
- ext-mbstring: *
- php-ds/php-ds: ^1.1
Requires (Dev)
- phpunit/phpunit: @stable
- vimeo/psalm: @stable
This package is auto-updated.
Last update: 2024-09-25 20:26:12 UTC
README
一个用于生成纯文本表格的小型PHP库。
入门指南
先决条件
- PHP >= 7
- ext-mbstring
- 可选安装 php-ds 扩展(推荐)。
安装
通过composer安装
$ composer require malios/php-to-ascii-table
用法
<?php $builder = new \AsciiTable\Builder(); $builder->addRows([ [ 'Order No' => 'A0001', 'Product Name' => 'Intel CPU', 'Price' => 700.00, 'Quantity' => 1 ], [ 'Order No' => 'A0002', 'Product Name' => 'Hard disk 10TB', 'Price' => 500.00, 'Quantity' => 2 ], [ 'Order No' => 'A0003', 'Product Name' => 'Dell Laptop', 'Price' => 11600.00, 'Quantity' => 8 ], [ 'Order No' => 'A0004', 'Product Name' => 'Intel CPU', 'Price' => 5200.00, 'Quantity' => 3 ] ]); $builder->addRow([ 'Order No' => 'A0005', 'Product Name' => 'A4Tech Mouse', 'Price' => 100.00, 'Quantity' => 10 ]); $builder->setTitle('Product List'); echo $builder->renderTable(); // Show only some fields $builder->showColumns(['Order No', 'Product Name', 'Quantity']); echo $builder->renderTable();
从对象构建表格
您可以从实现JsonSerializable接口的任何对象构建表格。
<?php class Person implements \JsonSerializable { private $name; private $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function jsonSerialize() { return [ 'name' => $this->name, 'age' => $this->age ]; } } $builder = new \AsciiTable\Builder(); $builder->addRow(new Person('John', 25)); $builder->addRow(new Person('Bill', 30)); echo $builder->renderTable();
贡献
所有贡献者都欢迎。您可以打开一个新问题或提交一个拉取请求。有关详细信息,请参阅CONTRIBUTING.md。
许可
本项目采用MIT许可协议 - 有关详细信息,请参阅LICENSE.md文件。