diff --git a/ChangeLog.md b/ChangeLog.md index c9b4170..1bc8b22 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -6,7 +6,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt ### Added -`ObjectExporter` interface, `ObjectExporterChain`, and `ExportContext` for customizing how objects are exported (a chain can be passed to `Exporter`'s constructor and is consulted before the default representation of an object is used) +`ObjectExporter` interface, `ObjectExporterChain`, and `ExportContext` for customizing how objects are exported (an object exporter can be passed to `Exporter`'s constructor and is consulted before the default representation of an object is used) ## [8.1.1] - 2026-07-13 diff --git a/README.md b/README.md index 29f65bf..adbfb04 100644 --- a/README.md +++ b/README.md @@ -202,16 +202,13 @@ final class MoneyExporter implements ObjectExporter } ``` -Object exporters are registered by passing an `ObjectExporterChain` to `Exporter`'s constructor. They are consulted, in the order in which they are composed into the chain, before the default representation of an object is used. The first object exporter whose `handles()` method returns `true` is asked for the representation: +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 export(new Money(1999, 'EUR')); @@ -230,6 +227,23 @@ $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 +export(['basket' => new Basket([new Money(1999, 'EUR')])]); ``` diff --git a/src/Exporter.php b/src/Exporter.php index 4c35270..f3186a4 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -61,13 +61,13 @@ final readonly class Exporter * @var positive-int */ private int $maxLengthForStrings; - private ?ObjectExporterChain $objectExporter; + private ?ObjectExporter $objectExporter; /** * @param non-negative-int $shortenArraysLongerThan * @param positive-int $maxLengthForStrings */ - public function __construct(int $shortenArraysLongerThan = 0, int $maxLengthForStrings = 40, ?ObjectExporterChain $objectExporter = null) + public function __construct(int $shortenArraysLongerThan = 0, int $maxLengthForStrings = 40, ?ObjectExporter $objectExporter = null) { $this->shortenArraysLongerThan = $shortenArraysLongerThan; $this->maxLengthForStrings = $maxLengthForStrings; diff --git a/tests/ExporterTest.php b/tests/ExporterTest.php index c99e2a4..8e5b8ef 100644 --- a/tests/ExporterTest.php +++ b/tests/ExporterTest.php @@ -670,6 +670,20 @@ EOF; ); } + public function testObjectCanBeExportedByCustomObjectExporterThatIsNotComposedIntoChain(): void + { + $exporter = new Exporter( + 0, + 40, + new ObjectExporterThatHandlesEveryObject, + ); + + $this->assertSame( + 'stdClass (indentation: 0)', + $exporter->export(new stdClass), + ); + } + public function testObjectNestedInArrayCanBeExportedByCustomObjectExporter(): void { $exporter = new Exporter(