frostaly / var-exporter
The frostaly VarExporter component.
Installs: 30
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/frostaly/var-exporter
Requires
- php: ^8.1
Requires (Dev)
- laminas/laminas-coding-standard: ^2.3
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^9.5
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().
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.