Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5547e8e

Browse files
committed
[VarExporter] Fix exporting classes with __serialize() but not __unserialize()
1 parent 0c1e73e commit 5547e8e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Symfony/Component/VarExporter/Internal/Exporter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
8383
throw new \TypeError($class.'::__serialize() must return an array');
8484
}
8585

86+
if (!$reflector->hasMethod('__unserialize')) {
87+
$serializeProperties = $properties;
88+
$properties = [];
89+
foreach ($serializeProperties as $n => $v) {
90+
$c = \PHP_VERSION_ID >= 80100 && $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
91+
$properties[$c][$n] = $v;
92+
}
93+
}
94+
8695
goto prepare_value;
8796
}
8897

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ public static function provideExport()
247247

248248
yield ['__unserialize-but-no-__serialize', new __UnserializeButNo__Serialize()];
249249

250+
yield ['__serialize-but-no-__unserialize', new __SerializeButNo__Unserialize()];
251+
250252
if (\PHP_VERSION_ID < 80100) {
251253
return;
252254
}
@@ -470,3 +472,20 @@ public function __unserialize(array $data): void
470472
$this->foo = $data['foo'];
471473
}
472474
}
475+
476+
class __SerializeButNo__Unserialize
477+
{
478+
public $foo;
479+
480+
public function __construct()
481+
{
482+
$this->foo = 'ccc';
483+
}
484+
485+
public function __serialize(): array
486+
{
487+
return [
488+
'foo' => $this->foo,
489+
];
490+
}
491+
}

0 commit comments

Comments
 (0)