ixnode/php-container

PHP 容器 - 一系列各种 PHP 容器类,如 JSON、文件等。

0.1.23 2024-02-25 21:37 UTC

README

Release PHP PHPStan PHPUnit PHPCS PHPMD Rector - Instant Upgrades and Automated Refactoring LICENSE

一系列各种 PHP 容器类,如 JSON、文件等。

1) 安装

composer require ixnode/php-container
vendor/bin/php-container -V
php-container 0.1.0 (12-19-2022 01:17:26) - Björn Hempel <bjoern@hempel.li>

2) 使用

2.1) 文件

use Ixnode\PhpContainer\File;

2.1.1) 检查文件是否存在

$exists = (new File('path-to-file'))->exist();
true || false

2.1.2) 获取文件大小(整数值)

$fileSize = (new File('path-to-file'))->getFileSize();
1523943

2.1.3) 获取文件大小(可读性)

$fileSizeHuman = (new File('path-to-file'))->getFileSizeHuman();
1.45 MB

2.1.4) 获取文件内容

$content = (new File('path-to-file'))->getContentAsText();
line 1
line 2
line 3
...

2.1.5) 获取 JSON 对象形式的文件内容

$content = (new File('path-to-json-file'))->getJson()->getJsonStringFormatted();
{
    "data": "Content of file 'path-to-json-file'."
}

2.2) JSON

use Ixnode\PhpContainer\Json;

2.2.1) 将数组转换为 JSON

$json = (new Json(['data' => 'json']))->getJsonStringFormatted();
{
    "data": "json"
}

2.2.2) 将 JSON 转换为数组

$array = (new Json('{"data": "json"}'))->getArray();
[
    'data' => 'json',
]

2.2.3) 将 JSON 文件转换为数组

$array = (new Json(new File('path-to-json-file')))->getArray();
[
    "data" => "Content of file 'path-to-json-file'.",
]

2.2.4) 从 JSON 构建新数组

$array = (new Json('[{"key1": 111, "key2": "222"},{"key1": 333, "key2": "444"}]'))->buildArray(
    [
        /* path []['key1'] as area1 */
        'area1' => [['key1']],
        /* path []['key2'] as area2 */
        'area2' => [['key2']],
    ]
);
[
    'area1' => [111, 333],
    'area2' => ['222', '444'],
]

2.3) CSV

use Ixnode\PhpContainer\Csv;

2.3.1) 解析 CSV 文件到数组

$array = (new Csv(new File('path-to-csv-file')))->getArray();

"path-to-csv-file" 的内容

"headerLine1Cell1";"headerLine1Cell2"
"valueLine2Cell1";"valueLine2Cell2"
"valueLine3Cell1";"valueLine3Cell2"

响应

[
    [
        'headerLine1Cell1' => 'valueLine2Cell1',
        'headerLine1Cell2' => 'valueLine2Cell2',
    ],    
    [
        'headerLine1Cell1' => 'valueLine3Cell1',
        'headerLine1Cell2' => 'valueLine3Cell2',
    ],
    ...
]

2.4) Curl

use Ixnode\PhpContainer\Curl;

2.4.1) 从 'URL' 返回响应值

$text = (new Curl('URL')->getContentAsText();

2.5) 图片

use Ixnode\PhpContainer\Image;

2.5.1) 返回给定图片的宽度。

$imageWidth = (new Image(new File('path-to-json-file')))->getWidth();

2.5.2) 返回一个调整大小的图片。

$imageString = (new Image(new File('path-to-json-file')))->getImageString(1000, Image::FORMAT_JPG, 85);

3.) 开发

git clone git@github.com:ixnode/php-container.git && cd php-container
composer install
composer test

4.) 许可证

此工具采用 MIT 许可证 - 详细信息请参阅 LICENSE 文件。