sevenecks / tableify
将多维数组转换为可在屏幕上回显或记录到文件的精美表格。
0.0.5
2018-04-06 17:06 UTC
Requires
- php: >=7.0
- sevenecks/string-utils: ^0.0.15
README
将多维PHP数组转换为可打印/可记录的表格,包括所有标题!
安装
通过Composer
composer require sevenecks/tableify
概述
使用方法链是使用此包的最佳方式。它使语法简单易读。除了toArray()之外的所有公共方法都是可链的,这意味着它们修改Tableify对象的实例并返回该对象本身。
示例用法
<?php require_once __DIR__ . '/../vendor/autoload.php'; use SevenEcks\Tableify\Tableify; $data = [ ['Name', 'Date', 'Phone', 'Age'], ['Altec Lansing', '03/22/18', '617-555-0584', '30'], ['Fack', '03/22/18', '508-555-0584', '24'], ['Seven Ecks', '03/22/18', '+1-888-555-0584', '100'], ['CK', '03/22/18', 'N/A', '33'], ['Jason Jasonson', '03/22/18', '978-555-0584', '34'], ['Waxillium Wick', '03/22/18', '978-555-0584', '34'], ['Ruby Reide', '03/22/18', '978-555-0584', '34'], ['Rex Gold', '03/22/18', '978-555-0584', '34'], ['Juicy Vee', '03/22/18', '978-555-0584', '34'], ]; echo "Table Construction using default values on class and no method chaining:\n"; $table = Tableify::new($data); $table = $table->make(); $table_data = $table->toArray(); foreach ($table_data as $row) { echo $row . "\n"; } echo "Table Construction using method chaining:\n"; $table_rows = Tableify::new($data)->center()->seperatorPadding(2)->seperator('*')->headerCharacter('@')->make()->toArray(); foreach ($table_rows as $row) { echo $row . "\n"; } // display the help list foreach (Tableify::help() as $row) { echo $row . "\n"; }
此代码将生成以下表格
Table Construction using default values on class and no method chaining:
-----------------------------------------------------
| Name | Date | Phone | Age |
-----------------------------------------------------
| Altec Lansing | 03/22/18 | 617-555-0584 | 30 |
| Fack | 03/22/18 | 508-555-0584 | 24 |
| Seven Ecks | 03/22/18 | +1-888-555-0584 | 100 |
| CK | 03/22/18 | N/A | 33 |
| Jason Jasonson | 03/22/18 | 978-555-0584 | 34 |
| Waxillium Wick | 03/22/18 | 978-555-0584 | 34 |
| Ruby Reide | 03/22/18 | 978-555-0584 | 34 |
| Rex Gold | 03/22/18 | 978-555-0584 | 34 |
| Juicy Vee | 03/22/18 | 978-555-0584 | 34 |
-----------------------------------------------------
Table Construction using method chaining:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* Name * Date * Phone * Age *
-------------------------------------------------------------
* Altec Lansing * 03/22/18 * 617-555-0584 * 30 *
* Fack * 03/22/18 * 508-555-0584 * 24 *
* Seven Ecks * 03/22/18 * +1-888-555-0584 * 100 *
* CK * 03/22/18 * N/A * 33 *
* Jason Jasonson * 03/22/18 * 978-555-0584 * 34 *
* Waxillium Wick * 03/22/18 * 978-555-0584 * 34 *
* Ruby Reide * 03/22/18 * 978-555-0584 * 34 *
* Rex Gold * 03/22/18 * 978-555-0584 * 34 *
* Juicy Vee * 03/22/18 * 978-555-0584 * 34 *
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-------------------------------------------------------------------------------------
| Method | Description of Method |
-------------------------------------------------------------------------------------
| Tableify::new(array) | Static method to create a new instance |
| center() | Set the formatter to use the center method |
| left() | Set the formatter to use the left method |
| right() | set the formatter to use the right method |
| setData(array) | Set the data array on the object |
| seperatorPadding(string) | Set the # of blank characters around the seperator |
| seperator(string) | Set the seperator string |
| headerCharacter(string) | Set the header character |
| belowHeaderCharacter(string) | Set the below header character string |
| make() | Make the data into an array of table rows |
| toArray() | Return the array of table rows |
| Tableify::help() | Display this help! |
-------------------------------------------------------------------------------------
TableifyInterface
TableifyInterface允许您以任何方式实现自己的Tableify,同时保持相同的公共接口。如果您有依赖注入容器,可以使用依赖注入来插入自己的。
变更日志
请参阅变更日志以获取更多信息。
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。