nmfmcosta / laravel-formatter
一个格式化库,可以在XML、CSV、JSON、TXT、YAML等格式之间转换数据输出。
v1.3
2024-01-03 11:26 UTC
Requires
- php: ^7.2|^8.0
- illuminate/support: ^8.0|^9.0|^10.0
- league/csv: ~9.0
- mustangostang/spyc: ~0.6
Requires (Dev)
- phpunit/phpunit: ^9.0
README
这是一个从https://github.com/soapbox/laravel-formatter分叉的项目,已更新以支持Laravel 8和9。
所有荣誉归原作者所有。
一个格式化包,可以帮助您轻松地在各种格式之间进行转换,如XML、JSON、CSV等...
目标
本库的目标是允许数据格式从一种类型转换为另一种类型。请参阅解析器和格式以查看支持的输入/输出格式。
安装
通过命令行
composer require nmfmcosta/laravel-formatter
通过composer.json
{ "require": { "nmfmcosta/laravel-formatter": "1.x" } }
解析器
以下都是格式化库可以读取的格式。
- 数组
- CSV
- JSON
- XML
- YAML
格式
以下都是支持的输出格式。
- 数组
- CSV
- JSON
- XML
- YAML
通用用法
包含格式化库
use LB\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());