dragon-code/pretty-array
Composer 安装命令:
composer require dragon-code/pretty-array
包简介
Simple conversion of an array to a pretty view
README 文档
README
Simple conversion of an array to a pretty view.
Installation
To get the latest version of Pretty Array package, simply require the project using Composer:
composer require dragon-code/pretty-array
Instead, you may of course manually update your require block and run composer update if you so choose:
{
"require": {
"dragon-code/pretty-array": "^4.0"
}
}
Introduction
Q: Why did you create this package when there is a cooler symfony/var-exporter?
The big minus of package symfony/var-exporter is that it works differently with numeric keys.
For example, we have an array:
$array = [ 100 => 'foo', 200 => 'bar', 201 => 'baz', 202 => 'qwe', 205 => 'ert', 206 => 'tyu' ];
When exporting through it, the file will contain the following content:
$array = [ 100 => 'foo', 200 => 'bar', 'baz', 'qwe', 205 => 'ert', 'tyu' ];
Q: Why do you think this is bad?
This package has a framework-independent base. However, it was originally developed as an assistant for the Laravel Lang: HTTP Statuses package.
This package allows you to publish language translations of the HTTP Status Codes for the Laravel and Lumen frameworks.
A feature of the framework is that IDEs that help with development do not know how to read the numeric keys of arrays of translation files, so it was necessary to translate theminto a text equivalent.
This behavior includes http-statuses.php file:
<?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', // ...
The peculiarity of the package is that it takes the values of the source file and combines it with what is already in the application. Thus, the output is a file with numeric keys that IDE helpers cannot read:
<?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', // ...
Using
Source array for all examples:
$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', );
Saving numeric keys without alignment
use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); return $service->raw($array);
Result:
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
0 => 'qwe',
1 => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
],
2 => 'iop',
]
Saving string keys without alignment
use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $service->setKeyAsString(); return $service->raw($array);
Result:
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
'0' => 'qwe',
'1' => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
'2' => 'rty',
'qawsed' => 'zxc',
],
'2' => 'iop',
]
Saving numeric keys with alignment
use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $service->setEqualsAlign(); return $service->raw($array);
Result:
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
0 => 'qwe',
1 => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
2 => 'rty',
'qawsed' => 'zxc',
],
2 => 'iop',
]
Saving string keys with alignment
use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $service->setKeyAsString(); $service->setEqualsAlign(); return $service->raw($array);
Result:
[
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qwerty' => 'qaz',
'baq' => [
'0' => 'qwe',
'1' => 'rty',
'asd' => 'zxc',
],
'asdfgh' => [
'foobarbaz' => 'qwe',
'2' => 'rty',
'qawsed' => 'zxc',
],
'2' => 'iop',
]
Saving simple array
use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $service->setSimple(); return $service->raw($array);
Result:
[
1,
2,
3,
'qaz',
[
'qwe',
'rty',
'zxc',
],
[
'qwe',
'rty',
'zxc',
],
'iop',
]
Change key case
use DragonCode\Contracts\Pretty\Arr\Caseable; use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $service->setCase(Caseable::PASCAL_CASE); return $service->raw($array);
Result:
[
'Foo' => 1,
'Bar' => 2,
'Baz' => 3,
'QweRty' => 'qaz',
'Baq' => [
0 => 'qwe',
1 => 'rty',
'Asd' => 'zxc',
],
'AsdFgh' => [
'FooBarBaz' => 'qwe',
2 => 'rty',
'QawSed' => 'zxc',
],
2 => 'iop',
]
The following options are available:
- 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); - no case (
DragonCode\Contracts\Pretty\Arr\Caseable::NO_CASE). By default;
NO_CASE means that key register processing will not be performed.
Storing file
use DragonCode\PrettyArray\Services\File; use DragonCode\PrettyArray\Services\Formatter; $service = Formatter::make(); $formatted = $service->raw($array); File::make($formatted) ->store('foo.php');
Result in stored file 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', ];
As 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');
Result in stored file 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'"
}
License
This package is licensed under the MIT License.
dragon-code/pretty-array 适用场景与选型建议
dragon-code/pretty-array 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.65M 次下载、GitHub Stars 达 17, 最近一次更新时间为 2021 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「pretty」 「dragon」 「pretty array」 「dragon code」 「andrey helldar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dragon-code/pretty-array 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dragon-code/pretty-array 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dragon-code/pretty-array 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Performing any actions during the deployment process
Support package is a collection of helpers and tools for any project.
A set of useful PHP classes.
HTML Formatter pritifies HTML content by applying proper intendation (no tidy, purification, etc.).
Simple conversion of an array to a pretty view
The extension of JsonFormatter of Monolog, which pretty prints json and supports unicode. Does not escape slashes.
统计信息
- 总下载量: 8.65M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 20
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-16