inpunktonet/object-flatten

This package lets you convert an object with a passed separator into a flat object and back again

v1.0.0 2024-10-27 09:04 UTC

This package is auto-updated.

Last update: 2024-10-27 09:15:13 UTC


README

Latest Version on Packagist Tests Total Downloads

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require inpunktonet/object-flatten

Usage

Flatten an object

$data = [
    'company' => [
        'name' => 'InPunktoNET',
        'depth' => [
            'level' => 1,
            'level2' => [
                'level3' => 3,
            ],
        ],
    ],
];

$skeleton = new InPunktoNET\ObjectFlatten();
$skeleton->setKeyValueSeparator(';');
$skeleton->setKeySeparator('.');
echo $skeleton->toFlattenString(data: $object);

Response:

company.name;InPunktoNET
company.depth.level;1
company.depth.level2.level3;3

Unflatten an object

$flattenedStrings = [
    "company.name;InPunktoNET",
    "company.depth.level;1",
    "company.depth.level2.level3;3"
];
$skeleton = new InPunktoNET\ObjectFlatten();
$skeleton->setKeyValueSeparator(';');
$skeleton->setKeySeparator('.');

echo $skeleton->toObject(data: $object);

Response:

[
    'company' => [
        'name' => 'InPunktoNET',
        'depth' => [
            'level' => "1",
            'level2' => [
                'level3' => "3",
            ],
        ],
    ],
]

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.