sebastianbergmann/exporter được TopGit xếp vào nhóm dự án mã nguồn mở, với 6.8k sao trên GitHub, viết chủ yếu bằng PHP. Provides the functionality to export PHP variables for visualization
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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.
sebastianbergmann/exporter có 6.8k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/sebastianbergmann/exporter. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
sebastianbergmann/exporter có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho sebastianbergmann/exporter. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
sebastianbergmann/exporter còn đang phát triển không?
Commit gần nhất trên sebastianbergmann/exporter là 8 ngày trước (theo timestamp GitHub). Repo có 39 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
sebastianbergmann/exporter là gì?
sebastianbergmann/exporter (sebastianbergmann/exporter) là dự án PHP trên GitHub. Theo mô tả gốc: Provides the functionality to export PHP variables for visualization
sebastianbergmann/exporter viết bằng ngôn ngữ gì?
sebastianbergmann/exporter chủ yếu viết bằng PHP. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về exporter?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về exporter.