frostaly/var-exporter

This package is abandoned and no longer maintained. No replacement package was suggested.

The frostaly VarExporter component.

dev-master 2022-07-13 19:49 UTC

This package is auto-updated.

Last update: 2023-11-13 22:40:12 UTC


README

The VarExporter component provides a simple alternative to PHP's var_export().

Source Code CI Status Code Quality Code Coverage Software License

Requirements

  • This library requires PHP 8.1 or later.

Installation

This library is installable via composer:

$ composer require frostaly/var-exporter

Quickstart

The VarExporter works like var_export(). It takes any value and returns the PHP code representation for that value:

echo Frostaly\VarExporter\VarExporter::export(['foo' => 'bar', 'baz' => [1.0, true, null]]);
[
    'foo' => 'bar',
    'baz' => [
        1.0,
        true,
        null,
    ]
]

Exporting Objects

VarExporter determines the most appropriate method to export your object using the registered encoders.

StdClassEncoder

It exports stdClass as an array to object cast.

(object) [
    'foo' => 'Hello'
    'bar' => 'World',
]

SetStateEncoder

It uses the __set_state() method just like var_export would do:

Namespace\CustomClass::__set_state([
    'foo' => 'Hello'
    'bar' => 'World',
])

SerializeEncoder

It uses __serialize() and __unserialize() methods and the Frostaly\VarExporter\Instantiator class:

Frostaly\VarExporter\Instantiator::unserialize(Namespace\CustomClass::class, [
    'foo' => 'Hello',
    'bar' => 'World',
]);

ObjectEncoder

It can encode any custom object using either its constructor and named arguments or the Frostaly\VarExporter\Instantiator class:

new Namespace\CustomClass(
    foo: 'Hello',
    bar: 'World',
)
Frostaly\VarExporter\Instantiator::construct(Namespace\CustomClass::class, [
    'foo' => 'Hello',
    'bar' => 'World',
]);

Contributing

Anyone can contribute to this repository. Please do so by posting issues when you've found something that is unexpected or sending a pull request for improvements.