Skip to content

Commit

Permalink
Merge branch '4.4' into 5.3
Browse files Browse the repository at this point in the history
* 4.4:
  [Serializer] fix support for unset properties on PHP < 7.4
  • Loading branch information
nicolas-grekas committed Nov 17, 2021
2 parents 5d7f068 + 36b95b0 commit 54489a9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
18 changes: 6 additions & 12 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,17 @@ protected function extractAttributes(object $object, string $format = null, arra
}
}

$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;

// properties
$propertyValues = (array) $object;
foreach ($reflClass->getProperties() as $reflProperty) {
$isPublic = $reflProperty->isPublic();

if ($checkPropertyInitialization) {
if (!$isPublic) {
$reflProperty->setAccessible(true);
}
if (!$reflProperty->isInitialized($object)) {
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
if ($reflProperty->isPublic()
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
|| ($reflProperty->isPrivate() && !\array_key_exists("\0{$reflProperty->class}\0{$reflProperty->name}", $propertyValues))
) {
unset($attributes[$reflProperty->name]);
continue;
}
}

if (!$isPublic) {
continue;
}

Expand Down
18 changes: 6 additions & 12 deletions Normalizer/PropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,15 @@ protected function extractAttributes(object $object, string $format = null, arra
{
$reflectionObject = new \ReflectionObject($object);
$attributes = [];
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
$propertyValues = (array) $object;

do {
foreach ($reflectionObject->getProperties() as $property) {
if ($checkPropertyInitialization) {
if (!$property->isPublic()) {
$property->setAccessible(true);
}

if (!$property->isInitialized($object)) {
continue;
}
}

if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
if (($property->isPublic() && !\array_key_exists($property->name, $propertyValues))
|| ($property->isProtected() && !\array_key_exists("\0*\0{$property->name}", $propertyValues))
|| ($property->isPrivate() && !\array_key_exists("\0{$property->class}\0{$property->name}", $propertyValues))
|| !$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)
) {
continue;
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ public function testNormalizeObjectWithUninitializedProperties()
);
}

public function testNormalizeObjectWithUnsetProperties()
{
$obj = new ObjectInner();
unset($obj->foo);
$this->assertEquals(
['bar' => null],
$this->normalizer->normalize($obj, 'any')
);
}

/**
* @requires PHP 7.4
*/
Expand Down
10 changes: 10 additions & 0 deletions Tests/Normalizer/PropertyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public function testNormalizeObjectWithUninitializedProperties()
);
}

public function testNormalizeObjectWithUnsetProperties()
{
$obj = new PropertyDummy();
unset($obj->foo);
$this->assertEquals(
['bar' => null, 'camelCase' => null],
$this->normalizer->normalize($obj, 'any')
);
}

public function testDenormalize()
{
$obj = $this->normalizer->denormalize(
Expand Down

0 comments on commit 54489a9

Please sign in to comment.