Skip to content

Commit

Permalink
Merge pull request #32 from rekalogika:fix/mapping-to-object-extendin…
Browse files Browse the repository at this point in the history
…g-stdclass

fix: map to object extending `stdClass` to property with no setter
  • Loading branch information
priyadi committed Feb 20, 2024
2 parents b1db821 + 8aa5844 commit d094695
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
replace with `ArrayObjectTransformer`.
* fix: Fix dynamic properties in Symfony profiler panel.
* fix: Fix `PresetTransformer`.
* fix: mapping to object extending `stdClass` to property with no setter.

## 1.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ public function createObjectToObjectMetadata(
};

if ($targetWriteMode === WriteMode::None) {
if (!$targetAllowsDynamicProperties) {
if ($targetAllowsDynamicProperties && $targetReadInfo === null) {
$targetWriteMode = WriteMode::DynamicProperty;
$targetWriteName = $targetProperty;
$targetWriteVisibility = Visibility::Public;
} else {
continue;
}
$targetWriteMode = WriteMode::DynamicProperty;
$targetWriteName = $targetProperty;
$targetWriteVisibility = Visibility::Public;

} else {
$targetWriteName = $targetWriteInfo->getName();
$targetWriteVisibility = match ($targetWriteInfo->getVisibility()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\DynamicProperty;

class ObjectExtendingStdClassWithProperties extends \stdClass
{
public ?string $public = null;
private ?string $private = null;

public function __construct(private ?string $constructor = null)
{
}

public function getConstructor(): ?string
{
return $this->constructor;
}

public function getPrivate(): ?string
{
return $this->private;
}
}
16 changes: 16 additions & 0 deletions tests/IntegrationTest/DynamicPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClass;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClassWithProperties;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties;
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto;

Expand Down Expand Up @@ -140,4 +141,19 @@ public function testStdClassToStdClass(): void
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}

public function testStdClassToStdClassWithExplicitProperties(): void
{
$source = new \stdClass();
$source->public = 'public';
$source->private = 'private';
$source->constructor = 'constructor';

$target = $this->mapper->map($source, ObjectExtendingStdClassWithProperties::class);

$this->assertInstanceOf(\stdClass::class, $target);
$this->assertSame('public', $target->public);
$this->assertNull($target->getPrivate());
$this->assertEquals('constructor', $target->getConstructor());
}
}

0 comments on commit d094695

Please sign in to comment.