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
8 changes: 8 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,16 @@ private function getSourceValue(object $source, object $target, mixed $value, \W
} elseif ($objectMap->offsetExists($value)) {
$value = $objectMap[$value];
} elseif (\PHP_VERSION_ID < 80400) {
if ($mapTo->transform) {
return $value;
}

return ($this->objectMapper ?? $this)->map($value, $mapTo->target);
} else {
if ($mapTo->transform) {
return $value;
}

$refl = new \ReflectionClass($mapTo->target);
$mapper = $this->objectMapper ?? $this;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

class LoadedValue
{
public function __construct(public string $name)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

class LoadedValueService
{
public function __construct(private ?LoadedValue $value = null)
{
}

public function load(): void
{
$this->value = new LoadedValue(name: 'loaded');
}

public function get(): LoadedValue
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

class LoadedValueTarget
{
public function __construct(public ?LoadedValue $relation = null)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
use Symfony\Component\ObjectMapper\TransformCallableInterface;

/**
* @implements TransformCallableInterface<object,object>
*/
class ServiceLoadedValueTransformer implements TransformCallableInterface
{
public function __construct(private readonly LoadedValueService $serviceLoadedValue, private readonly ObjectMapperMetadataFactoryInterface $metadata)
{
}

public function __invoke(mixed $value, object $source, ?object $target): mixed
{
$metadata = $this->metadata->create($value);
\assert(count($metadata) === 1);
\assert($metadata[0]->target === LoadedValue::class);
return $this->serviceLoadedValue->get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: LoadedValueTarget::class)]
final class ValueToMap
{
public ?ValueToMapRelation $relation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: LoadedValue::class, transform: ServiceLoadedValueTransformer::class)]
class ValueToMapRelation
{
public function __construct(public string $name)
{
}
}
30 changes: 29 additions & 1 deletion src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructorWithMetadata\Target as PromotedConstructorWithMetadataTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\Dto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\LoadedValueService;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\ServiceLoadedValueTransformer;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\ValueToMap;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\ValueToMapRelation;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\A as ServiceLocatorA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\B as ServiceLocatorB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\ConditionCallable;
Expand All @@ -90,7 +94,7 @@ public function testMap($expect, $args, array $deps = [])
$mapper = new ObjectMapper(...$deps);
$mapped = $mapper->map(...$args);

if (\PHP_VERSION_ID >= 80400 && isset($mapped->relation) && $mapped->relation instanceof D ) {
if (\PHP_VERSION_ID >= 80400 && isset($mapped->relation) && $mapped->relation instanceof D) {
$mapped->relation->baz;
}

Expand Down Expand Up @@ -583,4 +587,28 @@ public function testEmbedsAreLazyLoadedByDefault()
$this->assertSame('Test User', $target->user->name);
$this->assertFalse($refl->isUninitializedLazyObject($target->user));
}

public function testSkipLazyGhostWithClassTransform()
{
$service = new LoadedValueService();
$service->load();

$metadataFactory = new ReflectionObjectMapperMetadataFactory();
$mapper = new ObjectMapper(
metadataFactory: $metadataFactory,
transformCallableLocator: $this->getServiceLocator([ServiceLoadedValueTransformer::class => new ServiceLoadedValueTransformer($service, $metadataFactory)])
);

$value = new ValueToMap();
$value->relation = new ValueToMapRelation('test');

$result = $mapper->map($value);
if (\PHP_VERSION_ID >= 80400) {
$refl = new \ReflectionClass($result->relation);
$this->assertFalse($refl->isUninitializedLazyObject($result->relation));
}

$this->assertSame($result->relation, $service->get());
$this->assertSame($result->relation->name, 'loaded');
}
}
Loading