Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4:
  separate the property info and write info extractors
  add missing assertion
  [Filesystem] Run high-deps tests with Process 7
  • Loading branch information
fabpot committed May 2, 2024
2 parents 2eb172b + fce227c commit b814b58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 11 additions & 2 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class ObjectNormalizer extends AbstractObjectNormalizer

protected PropertyAccessorInterface $propertyAccessor;
protected $propertyInfoExtractor;
private $writeInfoExtractor;

private readonly \Closure $objectClassResolver;

Expand All @@ -51,6 +52,7 @@ public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory

$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...);
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
$this->writeInfoExtractor = new ReflectionExtractor();
}

public function getSupportedTypes(?string $format): array
Expand Down Expand Up @@ -174,8 +176,15 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
}

return $this->propertyInfoExtractor->isWritable($class, $attribute)
|| ($writeInfo = $this->propertyInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType();
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
return true;
}

if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
return true;
}

return false;
}

private function hasAttributeAccessorMethod(string $class, string $attribute): bool
Expand Down
4 changes: 2 additions & 2 deletions Tests/Encoder/CsvEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public function testDecodeEmptyData()

public function testMultipleEmptyHeaderNamesWithSeparator()
{
$this->encoder->decode(',.
,', 'csv');
$this->assertSame([['', [1 => '']]], $this->encoder->decode(',.
,', 'csv'));
}

public function testEncodeVariableStructure()
Expand Down
16 changes: 16 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ public function testConstructorWithObjectDenormalize()
$this->assertEquals('bar', $obj->bar);
}

public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor()
{
$serializer = $this->createMock(ObjectSerializerNormalizer::class);
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, [], new PropertyInfoExtractor());
$normalizer->setSerializer($serializer);

$data = new \stdClass();
$data->foo = 'foo';
$data->bar = 'bar';
$data->baz = true;
$data->fooBar = 'foobar';
$obj = $normalizer->denormalize($data, ObjectConstructorDummy::class, 'any');
$this->assertEquals('foo', $obj->getFoo());
$this->assertEquals('bar', $obj->bar);
}

public function testConstructorWithObjectTypeHintDenormalize()
{
$data = [
Expand Down

0 comments on commit b814b58

Please sign in to comment.