Skip to content

Commit

Permalink
Merge pull request #89 from yiisoft/datetime
Browse files Browse the repository at this point in the history
Custom dumps for DateTime* objects (+convert data providers to generators)
  • Loading branch information
xepozz committed Jun 4, 2023
2 parents d478f05 + 02ccf4d commit a9f296c
Show file tree
Hide file tree
Showing 3 changed files with 1,070 additions and 982 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,8 @@

## 1.5.1 under development

- Enh #88: Removed leading spaces in closure dumps (@xepozz)
- Enh #89: Add individual dumps for `DateTime` and `DateTimeImmutable` objects (@xepozz)
- Enh #88: Remove leading spaces in closure dumps (@xepozz)

## 1.5.0 January 16, 2023

Expand Down
20 changes: 19 additions & 1 deletion src/VarDumper.php
Expand Up @@ -6,12 +6,13 @@

use __PHP_Incomplete_Class;
use Closure;
use DateTimeInterface;
use Exception;
use IteratorAggregate;
use JsonException;
use JsonSerializable;
use ReflectionObject;
use ReflectionException;
use ReflectionObject;
use Yiisoft\Arrays\ArrayableInterface;

use function array_keys;
Expand Down Expand Up @@ -253,6 +254,9 @@ private function dumpInternal($var, bool $format, int $depth, int $level): strin
if ($var instanceof Closure) {
return $this->exportClosure($var);
}
if ($var instanceof DateTimeInterface) {
return $this->exportDateTime($var);
}

if ($depth <= $level) {
return $this->getObjectDescription($var) . ' (...)';
Expand Down Expand Up @@ -319,6 +323,10 @@ private function exportInternal($variable, bool $format, int $level): string
if ($variable instanceof Closure) {
return $this->exportClosure($variable, $level);
}
if ($variable instanceof DateTimeInterface) {
return $this->exportDateTime($variable);
}


$reflectionObject = new ReflectionObject($variable);
try {
Expand Down Expand Up @@ -551,4 +559,14 @@ private function getObjectProperties(object $var): array

return (array) $var;
}

private function exportDateTime(DateTimeInterface $variable): string
{
return sprintf(
"new \%s('%s', new \DateTimeZone('%s'))",
$variable::class,
$variable->format(DateTimeInterface::RFC3339_EXTENDED),
$variable->getTimezone()->getName()
);
}
}

0 comments on commit a9f296c

Please sign in to comment.