Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add visibility context option in PropertyNormalizer #44902

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

Merged
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
---

* Add support for constructor promoted properties to `Context` attribute
* Add context option `PropertyNormalizer::NORMALIZE_VISIBILITY` with bitmask flags `PropertyNormalizer::NORMALIZE_PUBLIC`, `PropertyNormalizer::NORMALIZE_PROTECTED`, `PropertyNormalizer::NORMALIZE_PRIVATE`
* Add method `withNormalizeVisibility` to `PropertyNormalizerContextBuilder`

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;

/**
* A helper providing autocompletion for available PropertyNormalizer options.
*
* @author Mathias Arlaud <[email protected]>
*/
final class PropertyNormalizerContextBuilder extends AbstractObjectNormalizerContextBuilder
{
/**
* Configures whether fields should be output based on visibility.
*/
public function withNormalizeVisibility(int $normalizeVisibility): static
{
return $this->with(PropertyNormalizer::NORMALIZE_VISIBILITY, $normalizeVisibility);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* Converts between objects and arrays by mapping properties.
Expand All @@ -32,6 +36,24 @@
*/
class PropertyNormalizer extends AbstractObjectNormalizer
{
public const NORMALIZE_PUBLIC = 1;
public const NORMALIZE_PROTECTED = 2;
public const NORMALIZE_PRIVATE = 4;

/**
* Flag to control whether fields should be output based on visibility.
*/
public const NORMALIZE_VISIBILITY = 'normalize_visibility';

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
{
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);

if (!isset($this->defaultContext[self::NORMALIZE_VISIBILITY])) {
$this->defaultContext[self::NORMALIZE_VISIBILITY] = self::NORMALIZE_PUBLIC | self::NORMALIZE_PROTECTED | self::NORMALIZE_PRIVATE;
}
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -90,14 +112,29 @@ protected function isAllowedAttribute(object|string $classOrObject, string $attr

try {
$reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
if ($reflectionProperty->isStatic()) {
return false;
}
} catch (\ReflectionException) {
return false;
}

return true;
if ($reflectionProperty->isStatic()) {
return false;
}

$normalizeVisibility = $context[self::NORMALIZE_VISIBILITY] ?? $this->defaultContext[self::NORMALIZE_VISIBILITY];

if ((self::NORMALIZE_PUBLIC & $normalizeVisibility) && $reflectionProperty->isPublic()) {
return true;
}

if ((self::NORMALIZE_PROTECTED & $normalizeVisibility) && $reflectionProperty->isProtected()) {
return true;
}

if ((self::NORMALIZE_PRIVATE & $normalizeVisibility) && $reflectionProperty->isPrivate()) {
return true;
}

return false;
}

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

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

namespace Symfony\Component\Serializer\Tests\Context\Normalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Context\Normalizer\PropertyNormalizerContextBuilder;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;

/**
* @author Antoine Lamirault <[email protected]>
*/
class PropertyNormalizerContextBuilderTest extends TestCase
{
private PropertyNormalizerContextBuilder $contextBuilder;

protected function setUp(): void
{
$this->contextBuilder = new PropertyNormalizerContextBuilder();
}

public function testWithNormalizeVisibility()
{
$context = $this->contextBuilder
->withNormalizeVisibility(PropertyNormalizer::NORMALIZE_PUBLIC | PropertyNormalizer::NORMALIZE_PROTECTED)
->toArray();

$this->assertSame([
PropertyNormalizer::NORMALIZE_VISIBILITY => PropertyNormalizer::NORMALIZE_PUBLIC | PropertyNormalizer::NORMALIZE_PROTECTED,
], $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,54 @@ public function testNormalizeObjectWithLazyProperties()
);
}

public function testNormalizeOnlyPublic()
{
$obj = new PropertyDummy();
$obj->foo = 'foo';
$obj->setBar('bar');
$obj->setCamelCase('camelcase');
$this->assertEquals(
['foo' => 'foo'],
$this->normalizer->normalize($obj, 'any', ['normalize_visibility' => PropertyNormalizer::NORMALIZE_PUBLIC])
);
}

public function testNormalizeOnlyProtected()
{
$obj = new PropertyDummy();
$obj->foo = 'foo';
$obj->setBar('bar');
$obj->setCamelCase('camelcase');
$this->assertEquals(
['camelCase' => 'camelcase'],
$this->normalizer->normalize($obj, 'any', ['normalize_visibility' => PropertyNormalizer::NORMALIZE_PROTECTED])
);
}

public function testNormalizeOnlyPrivate()
{
$obj = new PropertyDummy();
$obj->foo = 'foo';
$obj->setBar('bar');
$obj->setCamelCase('camelcase');
$this->assertEquals(
['bar' => 'bar'],
$this->normalizer->normalize($obj, 'any', ['normalize_visibility' => PropertyNormalizer::NORMALIZE_PRIVATE])
);
}

public function testNormalizePublicAndProtected()
{
$obj = new PropertyDummy();
$obj->foo = 'foo';
$obj->setBar('bar');
$obj->setCamelCase('camelcase');
$this->assertEquals(
['foo' => 'foo', 'camelCase' => 'camelcase'],
$this->normalizer->normalize($obj, 'any', ['normalize_visibility' => PropertyNormalizer::NORMALIZE_PUBLIC | PropertyNormalizer::NORMALIZE_PROTECTED])
);
}

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