Skip to content

Commit

Permalink
Format the array as a PHP array
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 30, 2024
1 parent 3e44260 commit fbef18c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/testing/src/FluentTestingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,28 @@ protected function dd($var): void
echo $var.($var[-1] === "\n" ? '' : "\n");
echo "```\n";
} elseif(is_array($var)) {
var_export($var);
echo "```php\n";
echo $this->formatArray($var);
echo "```\n";
} else {
dd($var);
}

die;
}

/**
* @experimental Helper function to format an array as a plain PHP array with [] syntax.
*/
private function formatArray(array $array): string
{
$json = json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

// Transform JSON to PHP array syntax
$php = preg_replace('/^(\s*)\"(\w+)\":/m', '$1$2:', $json); // Remove quotes from keys
$php = str_replace('{', '[', $php); // Replace { with [
$php = str_replace('}', ']', $php); // Replace } with ]

return $php."\n";
}
}

0 comments on commit fbef18c

Please sign in to comment.