A look at sebastianbergmann/exporter: 6.8k stars on GitHub, written primarily in PHP. Provides the functionality to export PHP variables for visualization
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
This component provides the functionality to export PHP variables for visualization.
Installation
You can add this library as a local, per-project dependency to your project using Composer:
composer require sebastian/exporter
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter;
// []
print $exporter->shortenedExport([]);
// [...]
print $exporter->shortenedExport([1, 2, 3, 4, 5]);
// stdClass Object ()
print $exporter->shortenedExport(new stdClass);
// Exception Object (...)
print $exporter->shortenedExport(new Exception);
// 'this\nis\na\nsuper\nlong\nstr...nspace'
print $exporter->shortenedExport(
<<<LONG_STRING
this
is
a
super
long
string
that
wraps
a
lot
and
eats
up
a
lot
of
space
LONG_STRING
);
Customizing How Objects Are Exported
By default, an object is exported property by property. Implement the ObjectExporter interface to control how objects of a type are represented:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\ExportContext;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporter;
final class MoneyExporter implements ObjectExporter
{
public function handles(object $object): bool
{
return $object instanceof Money;
}
public function export(object $object, Exporter $exporter, int $indentation, ExportContext $context): string
{
assert($object instanceof Money);
return sprintf('Money (%d %s)', $object->amount(), $object->currency());
}
}
An object exporter is registered by passing it to Exporter's constructor. It is consulted before the default representation of an object is used:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter(objectExporter: new MoneyExporter);
// Money (1999 EUR)
print $exporter->export(new Money(1999, 'EUR'));
/*
stdClass Object #6 (
'net' => Money (1999 EUR),
'gross' => Money (2379 EUR),
)
*/
$price = new stdClass;
$price->net = new Money(1999, 'EUR');
$price->gross = new Money(2379, 'EUR');
print $exporter->export($price);
ObjectExporterChain composes multiple object exporters into a single one. They are consulted in the order in which they are composed into the chain and the first one whose handles() method returns true is asked for the representation:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporterChain;
$exporter = new Exporter(
objectExporter: new ObjectExporterChain(
[
new BasketExporter,
new MoneyExporter,
],
),
);
Enums are objects and are therefore passed to the object exporters as well. When no object exporter handles an object, the default representation is used for it.
Repeated Occurrences
When the same object occurs more than once, the default representation is only used for the first occurrence; subsequent occurrences are replaced with a reference to the object:
An object exporter is responsible for the entire representation of the object it handles. Every occurrence of such an object is therefore exported by it:
The exception is an object that is (indirectly) nested in itself: it cannot be exported this way without recursing infinitely and is replaced with a reference to the object (see Objects Nested in Themselves).
Exporting Nested Values
An object exporter may use the Exporter it is given to export values that are nested in the object it handles. The ExportContext it is given must be passed on when it does. Otherwise, the export of these nested values starts over with an empty context and, for instance, assigns references to arrays that are already in use elsewhere in the same export:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\ExportContext;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporter;
final class BasketExporter implements ObjectExporter
{
public function handles(object $object): bool
{
return $object instanceof Basket;
}
public function export(object $object, Exporter $exporter, int $indentation, ExportContext $context): string
{
assert($object instanceof Basket);
return 'Basket ' . $exporter->export($object->items(), $indentation, $context);
}
}
An object that is (indirectly) nested in itself cannot be exported by an object exporter without recursing infinitely. When such an object occurs again while it is being exported by an object exporter, it is replaced with a reference to the object:
/*
Person (Alice) Array &0 [
0 => Person (Bob) Array &1 [
0 => Person Object #2,
],
]
*/
$alice = new Person('Alice');
$bob = new Person('Bob');
$alice->befriend($bob);
print $exporter->export($alice);
An object exporter can provide the representation of this reference by also implementing the ObjectReferenceExporter interface. When its exportReference() method returns null, the default representation of the reference is used:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\ExportContext;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporter;
use SebastianBergmann\Exporter\ObjectReferenceExporter;
final class PersonExporter implements ObjectExporter, ObjectReferenceExporter
{
public function handles(object $object): bool
{
return $object instanceof Person;
}
public function export(object $object, Exporter $exporter, int $indentation, ExportContext $context): string
{
assert($object instanceof Person);
return sprintf(
'Person (%s) %s',
$object->name(),
$exporter->export($object->friends(), $indentation, $context),
);
}
public function exportReference(object $object): ?string
{
assert($object instanceof Person);
return sprintf('Person (%s)', $object->name());
}
}
/*
Person (Alice) Array &0 [
0 => Person (Bob) Array &1 [
0 => Person (Alice),
],
]
*/
print $exporter->export($alice);
The object exporter is only asked for the representation of the reference, it is not given the opportunity to export the object again. When object exporters are composed into an ObjectExporterChain, the reference is provided by the object exporter that exports the object, which is the first one whose handles() method returns true.
Compact Exports
Exporter::shortenedExport() and Exporter::shortenedRecursiveExport() consult object exporters as well. The representation an object exporter provides is collapsed to a single line and shortened when it is longer than the configured maximum length:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter(objectExporter: new MoneyExporter);
// Money (1999 EUR)
print $exporter->shortenedExport(new Money(1999, 'EUR'));
Bear in mind that these representations are used where a short, single-line, and stable string is required. The representation of a data set is built using Exporter::shortenedRecursiveExport(), for instance, and is part of the name of a test that uses a data provider. An object exporter should therefore provide a representation that is compact and that does not change from one export to the next.
Custom Representations
Exporter::hasCustomRepresentationFor() tells whether an object exporter provides the representation for an object. This is meant for code that renders objects itself and wants to use the representation an object exporter provides when there is one:
<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter(objectExporter: new MoneyExporter);
// true
var_dump($exporter->hasCustomRepresentationFor(new Money(1999, 'EUR')));
// false
var_dump($exporter->hasCustomRepresentationFor(new stdClass));
Bear in mind that an object that is (indirectly) nested in itself is replaced with a reference to the object instead of being exported by an object exporter again. An object exporter that implements ObjectReferenceExporter can provide the representation of this reference.
TopGit's last sync did not record any GitHub topics for sebastianbergmann/exporter. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on sebastianbergmann/exporter?
The most recent commit recorded on sebastianbergmann/exporter was 6 days ago, based on the GitHub push timestamp. The repository has 39 forks — one of the better signals of community interest.
How many stars does sebastianbergmann/exporter have?
sebastianbergmann/exporter has 6.8k GitHub stars — refresh the page for the live number, or check github.com/sebastianbergmann/exporter. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What is sebastianbergmann/exporter?
sebastianbergmann/exporter (sebastianbergmann/exporter) is a PHP project on GitHub. From the project's own README: Provides the functionality to export PHP variables for visualization
What language is sebastianbergmann/exporter written in?
sebastianbergmann/exporter is written primarily in PHP. GitHub's language field is based on the largest share of bytes in the default branch.
Read full README in the tab above.
Still deciding about exporter?
One click hands the question to an AI along with this page — see what it says about exporter.