hitsend/laravel-formatter

一个格式化库,用于在XML、CSV、JSON、TXT、YAML等格式之间转换数据输出。

3.1.1 2018-06-27 14:37 UTC

This package is auto-updated.

Last update: 2024-09-17 08:13:36 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

格式化包

Build Status

一个格式化包,可以帮助您轻松地在各种格式之间进行转换,例如 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());