Skip to content
Merged
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
26 changes: 26 additions & 0 deletions packages/mapper/src/Mappers/ArrayToObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function map(mixed $from, mixed $to): object
if ($isMissing && ($getHook = $property->getGetHook())) {
$from[$propertyName] = $getHook->invokeArgs($targetObject);
} elseif ($isMissing) {
if ($this->applyPromotedDefault($property, $targetObject)) {
continue;
}

$this->handleMissingProperty(
property: $property,
propertyName: $propertyName,
Expand Down Expand Up @@ -133,6 +137,28 @@ private function resolveObject(mixed $objectOrClass): object
return new ClassReflector($objectOrClass)->newInstanceWithoutConstructor();
}

/**
* Readonly properties cannot declare a class-level default, so a promoted
* constructor parameter is the only place a default can live for them.
* Returns false when no default could be applied.
*/
private function applyPromotedDefault(PropertyReflector $property, object $targetObject): bool
{
if (! $property->isReadonly() || ! $property->hasDefaultValue()) {
return false;
}

$parameter = $property->getClass()->getConstructor()?->getParameter($property->getName());

if ($parameter === null || ! $parameter->isDefaultValueAvailable()) {
return false;
}

$property->setValue($targetObject, $parameter->getDefaultValue());

return true;
}

private function setParentRelations(object $parent, ClassReflector $parentClass): void
{
static $plans = [];
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Mapper/Fixtures/ObjectWithPromotedDefaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

final class ObjectWithPromotedDefaults
{
public static int $constructorCalls = 0;

public function __construct(
public readonly string $name = 'default-name',
public readonly int $count = 5,
) {
self::$constructorCalls++;
}
}
13 changes: 13 additions & 0 deletions tests/Integration/Mapper/Mappers/ArrayToObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithMagicGetter;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithMyObject;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithPrimitiveArrayProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithPromotedDefaults;
use Tests\Tempest\Integration\Mapper\Fixtures\ParentObject;
use Tests\Tempest\Integration\Mapper\Fixtures\ParentObjectWithGetterChild;
use Tests\Tempest\Integration\Mapper\Fixtures\ParentObjectWithVirtualChild;
Expand Down Expand Up @@ -72,6 +73,18 @@ public function default_values(): void
$this->assertNull($object->b);
}

#[Test]
public function map_promoted_defaults_without_running_constructor(): void
{
ObjectWithPromotedDefaults::$constructorCalls = 0;

$object = map(['name' => 'given'])->to(ObjectWithPromotedDefaults::class);

$this->assertSame('given', $object->name);
$this->assertSame(5, $object->count);
$this->assertSame(0, ObjectWithPromotedDefaults::$constructorCalls);
}

#[Test]
public function built_in_casters(): void
{
Expand Down
Loading