Skip to content

Core\Debug::render

Builds the same text Core\Debug::dump writes, and returns it instead of writing it.

You get the rendering as a Core\Cli\Text. You can print it, put it in a message, keep it in an array, or compare it with the rendering of another value. It names the type of the value, the length of every string and the class of every object. A control character such as a tab is made visible, a very deep or very long structure is cut short, and a property declared secret is shown as [redacted].

The text has no colour, even on a terminal that shows colour. It is the same on every machine, so you can compare it with a string in a test. The text has no line break at the end, so you decide where the line ends. echo of a rendering prints it as it is, because those bytes are already prepared for the screen and are not changed a second time.

The examples below show a rendering printed, renderings kept for a summary at the end, and two renderings compared.

Parameters

  • $valuemixedThe value to format. It is formatted the same way as a value given to dump.

Return value

Returns Core\Cli\Text. The plain text as a Core\Cli\Text, wherever the output goes. It has no colour and no newline at the end.

Examples

Print what a value is

<?nvs
// Shows `Core\Debug::render`. It builds the text that describes a value and
// returns it, so you can print it as part of your own output.
array<string> $row = [
'name' => 'Ada Lovelace',
'email' => 'ada@example.com',
];
// `render` returns the same text `Core\Debug::dump` writes, without a line
// break at the end. The `echo` below adds one.
echo Core\Debug::render($row), "\n";
Output
array(2) [
"name" => string(12) "Ada Lovelace"
"email" => string(15) "ada@example.com"
]

Keep a rendering for a summary

<?nvs
// Shows how to keep a rendering and print it later. The rows that fail a check
// are rendered and collected, and the summary is printed at the end.
array<array<string>> $rows = [
['Ada Lovelace', 'ada@example.com'],
['Grace Hopper', ''],
['Alan Turing', ''],
];
array<string> $failed = [];
foreach ($rows as array<string> $row) {
if (Core\Str::length($row[1]) == 0) {
// `as string` turns the rendering into plain text you can store.
$failed[] = Core\Debug::render($row) as string;
}
}
echo Core\Arr::count($failed), " rows have no email address\n";
foreach ($failed as string $one) {
echo $one, "\n";
}
Output
2 rows have no email address
array(2) [
0 => string(12) "Grace Hopper"
1 => string(0) ""
]
array(2) [
0 => string(11) "Alan Turing"
1 => string(0) ""
]

See what changed between two values

<?nvs
// Shows how to compare two values through their renderings. The settings a
// program started with and the settings it has now are rendered and compared
// as text.
array<string> $started = [
'host' => 'example.com',
'port' => '8080',
];
array<string> $current = [
'host' => 'example.com',
'port' => '9090',
];
string $before = Core\Debug::render($started) as string;
string $after = Core\Debug::render($current) as string;
if ($before == $after) {
echo "the settings are unchanged\n";
} else {
// Both renderings are printed, so you can see which line is different.
echo "the settings changed\n";
echo $before, "\n";
echo $after, "\n";
}
Output
the settings changed
array(2) [
"host" => string(11) "example.com"
"port" => string(4) "8080"
]
array(2) [
"host" => string(11) "example.com"
"port" => string(4) "9090"
]