andrey-helldar/pretty-array

Simple conversion of an array to a pretty view.

v2.4.0 2021-07-06 20:35 UTC

README

Simple conversion of an array to a pretty view.

pretty-array

Total Downloads Latest Stable Version Latest Unstable Version

StyleCI Travis-CI License

Installation

To get the latest version of Pretty Array package, simply require the project using Composer:

composer require andrey-helldar/pretty-array --dev

Instead, you may of course manually update your require-dev block and run composer update if you so choose:

{
    "require-dev": {
        "andrey-helldar/pretty-array": "^2.2"
    }
}

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 package lang-translations.

This package allows you to publish language translations for the Laravel framework.

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 them into a text equivalent.

This behavior includes errors.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 Helldar\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 Helldar\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 Helldar\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 Helldar\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 Helldar\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 Helldar\PrettyArray\Contracts\Caseable;
use Helldar\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 (Helldar\PrettyArray\Contracts\Caseable::CAMEL_CASE);
  • kebab-case (Helldar\PrettyArray\Contracts\Caseable::KEBAB_CASE);
  • PascalCase (Helldar\PrettyArray\Contracts\Caseable::PASCAL_CASE);
  • snake_case (Helldar\PrettyArray\Contracts\Caseable::SNAKE_CASE);
  • no case (Helldar\PrettyArray\Contracts\Caseable::NO_CASE). By default;

NO_CASE means that key register processing will not be performed.

Storing file

use Helldar\PrettyArray\Services\File;
use Helldar\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',
];

License

This package is licensed under the MIT License.

For Enterprise

Available as part of the Tidelift Subscription.

The maintainers of andrey-helldar/pretty-array and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.