Skip to content

Commit

Permalink
Implement suggestion from @staabm in #56 (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 29, 2024
1 parent ac6d770 commit 14a035b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 14 additions & 7 deletions src/Exporter.php
Expand Up @@ -334,7 +334,7 @@ private function exportArray(array &$value, RecursionContext $processed, string
return 'Array &' . (string) $key . ' [' . $values . ']';
}

private function exportObject(mixed $value, RecursionContext $processed, string $whitespace, int $indentation): string
private function exportObject(object $value, RecursionContext $processed, string $whitespace, int $indentation): string
{
$class = $value::class;

Expand All @@ -345,15 +345,22 @@ private function exportObject(mixed $value, RecursionContext $processed, string
$processed->add($value);

if ($this->objectExporter !== null && $this->objectExporter->handles($value)) {
return $this->objectExporter->export($value);
$buffer = $this->objectExporter->export($value);
} else {
$buffer = $this->defaultObjectExport($value, $processed, $whitespace, $indentation);
}

$values = '';
$array = $this->toArray($value);
return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')';
}

private function defaultObjectExport(object $object, RecursionContext $processed, string $whitespace, int $indentation): string
{
$buffer = '';
$array = $this->toArray($object);

if (count($array) > 0) {
foreach ($array as $k => $v) {
$values .=
$buffer .=
$whitespace
. ' ' .
$this->recursiveExport($k, $indentation)
Expand All @@ -362,9 +369,9 @@ private function exportObject(mixed $value, RecursionContext $processed, string
. ",\n";
}

$values = "\n" . $values . $whitespace;
$buffer = "\n" . $buffer . $whitespace;
}

return $class . ' Object #' . spl_object_id($value) . ' (' . $values . ')';
return $buffer;
}
}
6 changes: 3 additions & 3 deletions tests/ExporterTest.php
Expand Up @@ -496,11 +496,11 @@ public function testExportOfObjectsCanBeCustomized(): void

$exporter = new Exporter(new ObjectExporterChain([$objectExporter]));

$this->assertSame(
$this->assertStringMatchesFormat(
<<<'EOT'
Array &0 [
0 => custom object export,
1 => custom object export,
0 => stdClass Object #%d (custom object export),
1 => stdClass Object #%d (custom object export),
]
EOT
,
Expand Down

0 comments on commit 14a035b

Please sign in to comment.