Skip to content

A collection of various PHP container classes like JSON, File, etc.

License

Notifications You must be signed in to change notification settings

ixnode/php-container

Repository files navigation

PHP Container

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

A collection of various PHP container classes like JSON, File, etc.

1) Installation

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) Usage

2.1) File

use Ixnode\PhpContainer\File;

2.1.1) Check if file exists

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

2.1.2) Get the filesize (integer value)

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

2.1.3) Get the filesize (human readable)

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

2.1.4) Get the file content

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

2.1.5) Get the file content as JSON object

$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) Convert array to JSON

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

2.2.2) Convert JSON to array

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

2.2.3) Convert JSON file to array

$array = (new Json(new File('path-to-json-file')))->getArray();

2.2.4) Access to JSON object

$json = (new Json([
    'key1' => 'value1',
    'key2' => [
        'associative' => [
            'name' => 'Test',
            'id' => 123
        ],
        'indexed' => [1, 2, 3],
    ],
    'key3' => 'value3',
    'key4' => 'value4',
    'key5' => 'value5',
]));
print $json->getKeyString(['key2', 'associative', 'name']);
// return value: (string) 'Test'
print $json->getKeyInteger(['key2', 'indexed', 0]);
// return value: (int) 1
print_r($json->getKeyArray(['key2', 'indexed']));
// return value: (array) [1, 2, 3]

2.2.5) Build a new array from 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) Parse CSV file to array

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

Content of "path-to-csv-file":

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

Response:

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

2.4) Curl

use Ixnode\PhpContainer\Curl;

2.4.1) Return the response value from 'URL'

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

2.5) Image

use Ixnode\PhpContainer\Image;

2.5.1) Return width of given image.

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

2.5.2) Returns a resized image.

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

3.) Development

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

4.) License

This tool is licensed under the MIT License - see the LICENSE file for details