diff --git a/system/DataConverter/DataConverter.php b/system/DataConverter/DataConverter.php index 123e1eefe72b..f649e3a00564 100644 --- a/system/DataConverter/DataConverter.php +++ b/system/DataConverter/DataConverter.php @@ -141,13 +141,13 @@ public function reconstruct(string $classname, array $row): object return $classObj; } - $classSet = Closure::bind(function ($key, $value): void { - $this->{$key} = $value; + $classSet = Closure::bind(function (array $data): void { + foreach ($data as $key => $value) { + $this->{$key} = $value; + } }, $classObj, $classname); - foreach ($phpData as $key => $value) { - $classSet($key, $value); - } + $classSet($phpData); return $classObj; } diff --git a/tests/system/DataConverter/DataConverterTest.php b/tests/system/DataConverter/DataConverterTest.php index 4ee4a8078217..313905e98318 100644 --- a/tests/system/DataConverter/DataConverterTest.php +++ b/tests/system/DataConverter/DataConverterTest.php @@ -797,6 +797,35 @@ public function testReconstructObjectWithClosure(): void $this->assertInstanceOf(Time::class, $obj->updated_at); } + public function testReconstructPlainObject(): void + { + $types = [ + 'id' => 'int', + 'status' => 'int', + ]; + $converter = $this->createDataConverter($types, [], null, null); + + $plainClass = new class () { + public int $id; + public int $status; + public string $name; + }; + + $dbData = [ + 'id' => '123', + 'status' => '1', + 'name' => 'Alice', + ]; + + /** @var object{id: int, status: int, name: string} $obj */ + $obj = $converter->reconstruct($plainClass::class, $dbData); + + $this->assertInstanceOf($plainClass::class, $obj); + $this->assertSame(123, $obj->id); + $this->assertSame(1, $obj->status); + $this->assertSame('Alice', $obj->name); + } + public function testExtract(): void { $types = [