Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions system/DataConverter/DataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/system/DataConverter/DataConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading