mgbg / laravel-formatter
一个格式化库,可以将数据输出格式在XML、CSV、JSON、TXT、YAML等格式之间转换。
dev-master
2023-04-04 13:13 UTC
Requires
- php: ^8.0
- illuminate/support: ^8|^9|^10
- league/csv: ~9.0
- mustangostang/spyc: ~0.6
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is not auto-updated.
Last update: 2024-10-03 08:20:18 UTC
README
- 更新 Laravel 6 & phpunit 8 的支持
- 更新 composer.json
- 升级到 PSR-4
- 在导出 CSV 时添加参数 newline、delimiter、enclosure 和 escape
- 在将 XML 转换为数组时,将 @attributes 转换为 attribute
- 在导出 XML 时添加参数 encoding 和 formated
- JSON 解析修复(而不是仅将第一级转换为数组,使用关联数组参数 true,因此所有级别都将解码为数组结构)
- 添加对 Laravel 5 的支持
- 为 Laravel 5 添加包发现
- 为 CSV 添加支持 delimiter
格式化包
一个格式化包,可以帮助您轻松地在各种格式之间进行转换,如 XML、JSON、CSV 等...
目标
该库的目标是允许数据格式从一种类型转换为另一种类型。有关受支持的输入/输出格式,请参阅解析器和格式。
安装
通过命令行
composer require soapbox/laravel-formatter
通过 composer.json
{ "require": { "soapbox/laravel-formatter": "2.x" } }
解析器
以下都是格式化库可以读取的受支持格式。
- 数组
- CSV
- JSON
- XML
- YAML
格式
以下都是支持的输出格式。
- 数组
- CSV
- JSON
- XML
- YAML
通用用法
包含格式化器
use SoapBox\Formatter\Formatter;
支持类型
Formatter::JSON; //json Formatter::CSV; //csv Formatter::XML; //xml Formatter::ARR; //array Formatter::YAML; //yaml
创建您的第一个格式化器
$formatter = Formatter::make($jsonString, Formatter::JSON); $formatter = Formatter::make($yamlString, Formatter::YAML); $formatter = Formatter::make($array, Formatter::ARR); ...
从您的格式化器输出
$csv = $formatter->toCsv(); $json = $formatter->toJson(); $xml = $formatter->toXml(); $array = $formatter->toArray(); $yaml = $formatter->toYaml();
已弃用功能
以下功能已被库弃用,但是您可以在应用程序中轻松继续使用它们
序列化数组
$serialized = serialize($formatter->toArray());
PHP 导出
$export = var_export($formatter->toArray());