happyclicker / laravel-formatter
一个格式化库,可以将数据输出格式在 XML、CSV、JSON、TXT、YAML 以及其他几种格式之间进行转换。
10.0.1
2023-12-11 01:20 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: ^10.1
README
这是一个从 https://github.com/nmfmcosta/laravel-formatter 分支出来的项目,后者又是从 https://github.com/soapbox/laravel-formatter 分支出来的,已更新以支持 Laravel 10。
所有功劳都属于原始作者。
一个格式化包,可以帮助您轻松地在各种格式之间进行转换,如 XML、JSON、CSV 等...
目标
本库的目标是允许将数据格式从一种类型转换到另一种类型。查看“解析器和格式”以查看支持的输入/输出格式。
安装
通过命令行
composer require happyclicker/laravel-formatter
通过 composer.json
{ "require": { "happyclicker/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());