dragon-code/pretty-array

数组到美观视图的简单转换

v4.1.0 2023-06-02 11:37 UTC

README

Pretty Array

Stable Version Unstable Version Total Downloads Github Workflow Status License

数组到美观视图的简单转换。

安装

要获取最新版本的 Pretty Array 包,只需使用 Composer 依赖项目

composer require dragon-code/pretty-array

当然,您也可以手动更新您的 require 块并运行 composer update,如果您愿意的话

{
    "require": {
        "dragon-code/pretty-array": "^4.0"
    }
}

介绍

Q: 为什么你会创建这个包,尽管有更酷的 symfony/var-exporter 呢?

symfony/var-exporter 包的一个大缺点是它与数字键的工作方式不同。

例如,我们有一个数组

$array = [
    100 => 'foo',
    200 => 'bar',
    201 => 'baz',
    202 => 'qwe',
    205 => 'ert',
    206 => 'tyu'
];

通过它导出时,文件将包含以下内容

$array = [
    100 => 'foo',
    200 => 'bar',
    'baz',
    'qwe',
    205 => 'ert',
    'tyu'
];

Q: 你为什么认为这是不好的?

这个包有一个框架无关的基座。然而,它最初是作为 Laravel Lang: HTTP Statuses 包的辅助工具开发的。

这个包允许你发布 Laravel 和 Lumen 框架的 HTTP 状态代码的语言翻译。

框架的一个特点是,帮助开发的 IDE 无法读取翻译文件的数组中的数字键,因此有必要将它们转换为文本等效项。

这种行为包括 http-statuses.php 文件

<?php

return [
    'unknownError' => 'Unknown Error',
    '0' => 'Unknown Error',

    '100' => 'Continue',
    '101' => 'Switching Protocols',
    '102' => 'Processing',

    '200' => 'OK',
    '201' => 'Created',
    '202' => 'Accepted',
    '203' => 'Non-Authoritative Information',
    '204' => 'No Content',
    '205' => 'Reset Content',
    '206' => 'Partial Content',
    '207' => 'Multi-Status',
    '208' => 'Already Reported',
    '226' => 'IM Used',

// ...

这个包的特点是它将源文件的值与应用程序中已有的内容结合起来。因此,输出是一个 IDE 辅助工具无法读取的具有数字键的文件

<?php

return [
    'unknownError' => 'Unknown Error',
    0 => 'Unknown Error',

    100 => 'Continue',
    'Switching Protocols',
    'Processing',

    200 => 'OK',
    '201' => 'Created',
    'Accepted',
    'Non-Authoritative Information',
    'No Content',
    'Reset Content',
    'Partial Content',
    'Multi-Status',
    'Already Reported',
    226 => 'IM Used',

// ...

使用

所有示例的源数组

$array = array (
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
    'qwerty' => 'qaz',
    'baq' => array (
        0 => 'qwe',
        '1' => 'rty',
        'asd' => 'zxc',
    ),
    'asdfgh' => array (
        'foobarbaz' => 'qwe',
        2 => 'rty',
        'qawsed' => 'zxc',
    ),
    2 => 'iop',
);

保存未对齐的数字键

use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();

return $service->raw($array);

结果

[
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
    'qwerty' => 'qaz',
    'baq' => [
        0 => 'qwe',
        1 => 'rty',
        'asd' => 'zxc',
    ],
    'asdfgh' => [
        'foobarbaz' => 'qwe',
        2 => 'rty',
        'qawsed' => 'zxc',
    ],
    2 => 'iop',
]

保存未对齐的字符串键

use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();
$service->setKeyAsString();

return $service->raw($array);

结果

[
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
    'qwerty' => 'qaz',
    'baq' => [
        '0' => 'qwe',
        '1' => 'rty',
        'asd' => 'zxc',
    ],
    'asdfgh' => [
        'foobarbaz' => 'qwe',
        '2' => 'rty',
        'qawsed' => 'zxc',
    ],
    '2' => 'iop',
]

保存对齐的数字键

use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();
$service->setEqualsAlign();

return $service->raw($array);

结果

[
    'foo'    => 1,
    'bar'    => 2,
    'baz'    => 3,
    'qwerty' => 'qaz',
    'baq'    => [
        0     => 'qwe',
        1     => 'rty',
        'asd' => 'zxc',
    ],
    'asdfgh' => [
        'foobarbaz' => 'qwe',
        2           => 'rty',
        'qawsed'    => 'zxc',
    ],
    2        => 'iop',
]

保存对齐的字符串键

use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();
$service->setKeyAsString();
$service->setEqualsAlign();

return $service->raw($array);

结果

[
    'foo'    => 1,
    'bar'    => 2,
    'baz'    => 3,
    'qwerty' => 'qaz',
    'baq'    => [
        '0'   => 'qwe',
        '1'   => 'rty',
        'asd' => 'zxc',
    ],
    'asdfgh' => [
        'foobarbaz' => 'qwe',
        '2'         => 'rty',
        'qawsed'    => 'zxc',
    ],
    '2'      => 'iop',
]

保存简单数组

use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();
$service->setSimple();

return $service->raw($array);

结果

[
    1,
    2,
    3,
    'qaz',
    [
        'qwe',
        'rty',
        'zxc',
    ],
    [
        'qwe',
        'rty',
        'zxc',
    ],
    'iop',
]

更改键的大小写

use DragonCode\Contracts\Pretty\Arr\Caseable;
use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();
$service->setCase(Caseable::PASCAL_CASE);

return $service->raw($array);

结果

[
    'Foo' => 1,
    'Bar' => 2,
    'Baz' => 3,
    'QweRty' => 'qaz',
    'Baq' => [
        0 => 'qwe',
        1 => 'rty',
        'Asd' => 'zxc',
    ],
    'AsdFgh' => [
        'FooBarBaz' => 'qwe',
        2 => 'rty',
        'QawSed' => 'zxc',
    ],
    2 => 'iop',
]

以下选项可用

  • camelCase (DragonCode\Contracts\Pretty\Arr\Caseable::CAMEL_CASE);
  • kebab-case (DragonCode\Contracts\Pretty\Arr\Caseable::KEBAB_CASE);
  • PascalCase (DragonCode\Contracts\Pretty\Arr\Caseable::PASCAL_CASE);
  • snake_case (DragonCode\Contracts\Pretty\Arr\Caseable::SNAKE_CASE);
  • 无大小写 (DragonCode\Contracts\Pretty\Arr\Caseable::NO_CASE). 默认情况下;

NO_CASE 表示不会执行键注册处理。

存储文件

use DragonCode\PrettyArray\Services\File;
use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();

$formatted = $service->raw($array);

File::make($formatted)
    ->store('foo.php');

存储在文件 foo.php 中的结果

<?php

return [
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
    'qwerty' => 'qaz',
    'baq' => [
        0 => 'qwe',
        1 => 'rty',
        'asd' => 'zxc',
    ],
    'asdfgh' => [
        'foobarbaz' => 'qwe',
        2 => 'rty',
        'qawsed' => 'zxc',
    ],
    2 => 'iop',
];

作为 JSON

use DragonCode\PrettyArray\Services\File;
use DragonCode\PrettyArray\Services\Formatter;

$service = Formatter::make();

$service->asJson();

$formatted = $service->raw($array);

File::make($formatted)
    ->store('foo.json');

存储在文件 foo.json 中的结果

{
    "foo": 1,
    "bar": 2,
    "baz": 3,
    "qwerty": "qaz",
    "baq": {
        "0": "qwe",
        "1": "rty",
        "asd": "zxc"
    },
    "asdfgh": {
        "foobarbaz": "qwe",
        "2": "rty",
        "qawsed": "zxc"
    },
    "2": "'iop'"
}

许可

此包根据 MIT 许可证 许可。