Skip to content

Commit e7c6dd5

Browse files
committed
Dumper:: $customObjects can be handler
1 parent 9d3ceb2 commit e7c6dd5

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/PhpGenerator/Dumper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class Dumper
2222
public int $maxDepth = 50;
2323
public int $wrapLength = 120;
2424
public string $indentation = "\t";
25-
public bool $customObjects = true;
25+
public bool|\Closure $customObjects = true;
2626
public bool $ref = false;
2727
private array $references;
2828

@@ -191,6 +191,9 @@ private function dumpObject(object $var, array $parents, int $level, int $column
191191

192192
throw new Nette\InvalidStateException('Cannot dump object of type Closure.');
193193

194+
} elseif ($this->customObjects instanceof \Closure) {
195+
return ($this->customObjects)($var, fn($v) => $this->dumpVar($v, $parents, $level + 1));
196+
194197
} elseif ($this->customObjects) {
195198
return $this->dumpCustomObject($var, $parents, $level);
196199

tests/PhpGenerator/Dumper.dump().phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,25 @@ Assert::exception(
208208
Nette\InvalidStateException::class,
209209
'Cannot dump object of type TestSer.',
210210
);
211+
212+
213+
// handler for custom objects
214+
$dumper = new Dumper;
215+
$dumper->customObjects = function (mixed $val, $dumper) {
216+
Assert::type(TestSer::class, $val);
217+
Assert::same('[1, 2]', $dumper([1, 2]));
218+
return 'res';
219+
};
220+
Assert::same('res', $dumper->dump(new TestSer));
221+
222+
223+
// handler for custom objects: recursive dependency
224+
$dumper = new Dumper;
225+
$dumper->customObjects = function (mixed $val, $dumper) {
226+
$dumper($val);
227+
};
228+
Assert::exception(
229+
fn() => $dumper->dump(new TestSer),
230+
Nette\InvalidStateException::class,
231+
'Nesting level too deep or recursive dependency.',
232+
);

0 commit comments

Comments
 (0)