8000 [PropertyAccess] Implement DateTime type conversion in PropertyAccess by curry684 · Pull Request #47776 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Implement DateTime type conversion in PropertyAccess #47776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
24 changes: 24 additions & 0 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,20 @@ private function writeProperty(array $zval, string $property, mixed $value)

if (PropertyWriteInfo::TYPE_NONE !== $mutator->getType()) {
$type = $mutator->getType();
$reflClass = new \ReflectionClass($object);

if (PropertyWriteInfo::TYPE_METHOD === $type) {
if ($reflClass->hasMethod($mutator->getName())) {
$reflMethod = $reflClass->getMethod($mutator->getName());
if ($reflMethod->getNumberOfParameters() >= 1 && $type = $reflMethod->getParameters()[0]->getType()) {
$value = self::coerceValue($value, $type);
}
}
$object->{$mutator->getName()}($value);
} elseif (PropertyWriteInfo::TYPE_PROPERTY === $type) {
if ($reflClass->hasProperty($mutator->getName())) {
$value = self::coerceValue($value, $reflClass->getProperty($mutator->getName())->getType());
}
$object->{$mutator->getName()} = $value;
} elseif (PropertyWriteInfo::TYPE_ADDER_AND_REMOVER === $type) {
$this->writeCollection($zval, $property, $value, $mutator->getAdderInfo(), $mutator->getRemoverInfo());
Expand All @@ -538,6 +548,20 @@ private function writeProperty(array $zval, string $property, mixed $value)
}
}

private static function coerceValue(mixed $value, ?\ReflectionType $targetType): mixed
{
// Only attempt coercion for specifically typed targets
if ($targetType instanceof \ReflectionNamedType) {
$type = $targetType->getName();
if ($value instanceof \DateTimeInterface && is_a($type, \DateTimeInterface::class, true)
&& $type !== $value::class && \DateTimeInterface::class !== $type) {
$value = $type::createFromInterface($value);
}
}

return $value;
}

/**
* Adjusts a collection-valued property by calling add*() and remove*() methods.
*/
Expand Down
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\PropertyAccess\Tests\Fixtures;

/**
* @author Niels Keurentjes <niels.keurentjes@omines.com>
*/
class DateTimeConversion
{
public \DateTime $publicDateTime;
public \DateTimeImmutable $publicDateTimeImmutable;
public \DateTimeInterface $publicDateTimeInterface;

private \DateTime $dateTime;
private \DateTimeImmutable $dateTimeImmutable;

public function getDateTime(): \DateTime
{
return $this->dateTime;
}

public function setDateTime(\DateTime $dateTime): void
{
$this->dateTime = $dateTime;
}

public function getDateTimeImmutable(): \DateTimeImmutable
{
return $this->dateTimeImmutable;
}

public function setDateTimeImmutable(\DateTimeImmutable $dateTimeImmutable): void
{
$this->dateTimeImmutable = $dateTimeImmutable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@

class ExtendedUninitializedProperty extends UninitializedProperty
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers - this was an unintended side effect of running CS Fixer, should happen anyway.

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\Tests\Fixtures\DateTimeConversion;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ExtendedUninitializedProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidArgumentLength;
Expand Down Expand Up @@ -919,4 +920,34 @@ public function testSetValueWrongTypeShouldThrowWrappedException()
$this->expectExceptionMessage('Expected argument of type "float", "string" given at property path "publicProperty"');
$this->propertyAccessor->setValue($object, 'publicProperty', 'string');
}

public function testSetValueConvertDateTimeTypes(): void
{
$fixture = new DateTimeConversion();
$mutable = new \DateTime($mutableDate = '2021-01-01');
$immutable = new \DateTimeImmutable($immutableDate = '2022-02-02');

// Test conversion with public properties
$this->propertyAccessor->setValue($fixture, 'publicDateTimeImmutable', $mutable);
$this->assertSame($this->propertyAccessor->getValue($fixture, 'publicDateTimeImmutable')->format('Y-m-d'), $mutableDate);

$this->propertyAccessor->setValue($fixture, 'publicDateTime', $immutable);
$this->assertSame($this->propertyAccessor->getValue($fixture, 'publicDateTime')->format('Y-m-d'), $immutableDate);

// Test conversion via setters/getters
$this->propertyAccessor->setValue($fixture, 'dateTimeImmutable', $mutable);
$this->assertSame($this->propertyAccessor->getValue($fixture, 'dateTimeImmutable')->format('Y-m-d'), $mutableDate);

$this->propertyAccessor->setValue($fixture, 'dateTime', $immutable);
$this->assertSame($this->propertyAccessor->getValue($fixture, 'dateTime')->format('Y-m-d'), $immutableDate);

// Ensure conversion also happens on nested properties
$array = [$fixture];
$this->propertyAccessor->setValue($array, '[0].publicDateTime', new \DateTimeImmutable('2023-03-03'));
$this->assertSame($this->propertyAccessor->getValue($fixture, 'publicDateTime')->format('Y-m-d'), '2023-03-03');

// Ensure setting to an interface does not cause conversion or issues
$this->propertyAccessor->setValue($fixture, 'publicDateTimeInterface', $immutable);
$this->assertSame($fixture->publicDateTimeInterface, $immutable);
}
}
0