Skip to content

Convert a php array to a .php file which can be included

License

Notifications You must be signed in to change notification settings

kfriars/php-array-to-file

Repository files navigation

Convert a php array into an includeable php file

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.

About

Convert a php array to a .php file which can be included

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages