Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
6562680 committed Dec 2, 2024
1 parent bda879e commit 65984ba
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/Lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static function php_var_dump($value, array $options = []) : string
{
$maxlen = $options[ 'maxlen' ] ?? null;
$withArrays = $options[ 'with_arrays' ] ?? true;
$withIds = $options[ 'with_ids' ] ?? true;

if ($maxlen < 1) $maxlen = null;

Expand All @@ -152,7 +153,11 @@ public static function php_var_dump($value, array $options = []) : string

if (is_iterable($value)) {
if (is_object($value)) {
$var = 'iterable(' . get_class($value) . ' # ' . spl_object_id($value) . ')';
$id = $withIds
? ' # ' . spl_object_id($value)
: '';

$var = 'iterable(' . get_class($value) . $id . ')';

} else {
$var = 'array(' . count($value) . ')';
Expand All @@ -176,7 +181,11 @@ public static function php_var_dump($value, array $options = []) : string

} else {
if (is_object($value)) {
$var = 'object(' . get_class($value) . ' # ' . spl_object_id($value) . ')';
$id = $withIds
? ' # ' . spl_object_id($value)
: '';

$var = 'object(' . get_class($value) . $id . ')';

if (method_exists($value, '__debugInfo')) {
ob_start();
Expand All @@ -187,10 +196,14 @@ public static function php_var_dump($value, array $options = []) : string
} elseif (is_string($value)) {
$var = 'string(' . strlen($value) . ')';

$dump = "\"{$value}\"";
$dump = "\"{$dump}\"";

} elseif (is_resource($value)) {
$var = '{ resource(' . gettype($value) . ' # ' . ((int) $value) . ') }';
$id = $withIds
? ' # ' . ((int) $value)
: '';

$var = '{ resource(' . get_resource_type($value) . $id . ') }';

} else {
$var = null
Expand Down Expand Up @@ -235,6 +248,7 @@ public static function php_var_export($var, array $options = []) : string
$indent = $options[ 'indent' ] ?? " ";
$newline = $options[ 'newline' ] ?? PHP_EOL;
$withObjects = $options[ 'with_objects' ] ?? true;
$withIds = $options[ 'with_ids' ] ?? true;

switch ( gettype($var) ) {
case "NULL":
Expand All @@ -250,7 +264,6 @@ public static function php_var_export($var, array $options = []) : string
break;

case "array":

$keys = array_keys($var);

foreach ( $keys as $key ) {
Expand Down Expand Up @@ -292,7 +305,25 @@ public static function php_var_export($var, array $options = []) : string
$result = var_export($var, true);

} else {
$result = '{ object(' . get_class($var) . ' # ' . spl_object_id($var) . ') }';
$id = $withIds
? ' # ' . spl_object_id($var)
: null;

$result = '{ object(' . get_class($var) . $id . ') }';
}

break;

case "resource":
if ($withObjects) {
$result = var_export($var, true);

} else {
$id = $withIds
? ' # ' . spl_object_id($var)
: null;

$result = '{ resource(' . get_resource_type($var) . $id . ') }';
}

break;
Expand Down

0 comments on commit 65984ba

Please sign in to comment.