kfriars/php-array-to-file

Convert a php array into a .php file that can be included

1.1.2 2020-08-27 20:30 UTC

This package is auto-updated.

Last update: 2024-04-28 04:56:52 UTC


README

Latest Version on Packagist Total Downloads GitHub Workflow Status Code Climate coverage Code Climate maintainability

The purpose of this package is to print an array to a file in a reader-friendly format, that can later be included as php. The package supports deeply nested arrays, with numeric, string, boolean and object values.

Installation

You can install the package via composer:

composer require kfriars/php-array-to-file

Usage

You can use the static method toFile(...) on Kfriars\ArrayToFile\ArrayWriter for convenient use, or you can inject the Kfriars\ArrayToFile\ArrayToFile class as a dependency, and use write(...).

An example of use:

ArrayWriter::toFile([1, 2, 3], '/absolute/path/to/file.php');

Would create /absolute/path/to/file.php with the contents:

<?php

return [
    1,
    2,
    3,
];

The package also allows you to transform the values in your array by passing in a callable. The callable receives the value before it is written to the file, and should return the value you desire to have written. You can use it like:

function save(ArrayToFile $a2f)
{
    $a2f->write([0, 1, '', ' '], '/absolute/path/to/file.php', function ($value) {
        return (bool) $value;
    });
}

Which will create /absolute/path/to/file.php with the contents:

<?php

return [
    false,
    true,
    false,
    true,
];

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email nyxsoft.inc@gmail.com instead of using the issue tracker.

Credits

License

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