Skip to content

Commit

Permalink
Support for internal classes providing __set_state() implementation (#10
Browse files Browse the repository at this point in the history
) (#11)
  • Loading branch information
GameplayJDK authored Mar 13, 2020
1 parent f3931c4 commit 411110b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ try {

## Limitations

- Exporting internal classes other than `stdClass` and `Closure` is currently not supported. `VarExporter` will throw an `ExportException` if it finds one.
- Exporting internal classes other than `stdClass` and `Closure`, and classes implementing `__set_state()` (most notably DateTime classes) is currently not supported. `VarExporter` will throw an `ExportException` if it finds one.

To avoid hitting this brick wall, you can implement `__serialize()` and `__unserialize()` in classes that contain references to internal objects.

Expand Down
4 changes: 2 additions & 2 deletions src/Internal/GenericExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public function __construct(int $options)
$this->objectExporters[] = new ObjectExporter\ClosureExporter($this);
}

$this->objectExporters[] = new ObjectExporter\InternalClassExporter($this);

if (! ($options & VarExporter::NO_SET_STATE)) {
$this->objectExporters[] = new ObjectExporter\SetStateExporter($this);
}

$this->objectExporters[] = new ObjectExporter\InternalClassExporter($this);

if (! ($options & VarExporter::NO_SERIALIZE)) {
$this->objectExporters[] = new ObjectExporter\SerializeExporter($this);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,42 @@ public function testInlineNumericScalarArray()
$this->assertExportEquals($expected, $var, VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
}

public function testExportDateTime()
{
$timezone = new \DateTimeZone('Europe/Berlin');
$format = 'Y-m-d H:i:s.u';

$var = \DateTime::createFromFormat($format, '2020-03-09 18:51:23.000000', $timezone);

$expected = <<<'PHP'
\DateTime::__set_state([
'date' => '2020-03-09 18:51:23.000000',
'timezone_type' => 3,
'timezone' => 'Europe/Berlin'
])
PHP;

$this->assertExportEquals($expected, $var);
}

public function testExportDateTimeImmutable()
{
$timezone = new \DateTimeZone('Europe/Berlin');
$format = 'Y-m-d H:i:s.u';

$var = \DateTimeImmutable::createFromFormat($format, '2020-03-10 17:06:19.000000', $timezone);

$expected = <<<'PHP'
\DateTimeImmutable::__set_state([
'date' => '2020-03-10 17:06:19.000000',
'timezone_type' => 3,
'timezone' => 'Europe/Berlin'
])
PHP;

$this->assertExportEquals($expected, $var);
}

public function testExportInternalClass()
{
$object = new \stdClass;
Expand Down

0 comments on commit 411110b

Please sign in to comment.