kfriars / php-array-to-file
Convert a php array into a .php file that can be included
Installs: 4 655
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- mockery/mockery: ^1.3
- phpunit/phpunit: ^8.0
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2024-10-28 06:12:06 UTC
README
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.