From cb945ffcf109960b2dd33eed40ca82c77f674d66 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 18 Jan 2024 00:53:16 +0100 Subject: [PATCH] Dumper: added $customObjects --- src/PhpGenerator/Dumper.php | 6 +++++- tests/PhpGenerator/Dumper.dump().phpt | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/PhpGenerator/Dumper.php b/src/PhpGenerator/Dumper.php index 07f3dfcd..ed3dbf1d 100644 --- a/src/PhpGenerator/Dumper.php +++ b/src/PhpGenerator/Dumper.php @@ -22,6 +22,7 @@ final class Dumper public int $maxDepth = 50; public int $wrapLength = 120; public string $indentation = "\t"; + public bool $customObjects = true; /** @@ -172,8 +173,11 @@ private function dumpObject(object $var, array $parents, int $level, int $column $var = (array) $var; return '(object) ' . $this->dumpArray($var, $parents, $level, $column + 10); - } else { + } elseif ($this->customObjects) { return $this->dumpCustomObject($var, $parents, $level); + + } else { + throw new Nette\InvalidArgumentException("Unable to dump class $class."); } } diff --git a/tests/PhpGenerator/Dumper.dump().phpt b/tests/PhpGenerator/Dumper.dump().phpt index 6a47dcc9..2ae47c7f 100644 --- a/tests/PhpGenerator/Dumper.dump().phpt +++ b/tests/PhpGenerator/Dumper.dump().phpt @@ -198,3 +198,13 @@ same( XX, $dumper->dump(new TestDateTime('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))), ); + + +// disallow custom objects +$dumper = new Dumper; +$dumper->customObjects = false; +Assert::exception( + fn() => $dumper->dump(new TestSer), + Nette\InvalidArgumentException::class, + 'Unable to dump class TestSer.', +);