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

Skip to content

[Serializer] Add ScalarNormalizer to allow custom values for boolean #58473

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

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ public function normalize(mixed $object, ?string $format = null, array $context
foreach ($stack as $attribute => $attributeValue) {
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);

if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
continue;
}

if (!$this->serializer instanceof NormalizerInterface) {
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Symfony\Component\Serializer\Normalizer;

/**
* @method array getSupportedTypes(?string $format)
*/
class ScalarNormalizer implements NormalizerInterface
{
public const TRUE_VALUE_KEY = 'true_value';
public const FALSE_VALUE_KEY = 'false_value';

/**
* @inheritDoc
*/
public function normalize(mixed $object, ?string $format = null, array $context = [])
{
if (true === $object && isset($context[self::TRUE_VALUE_KEY])) {
return $context[self::TRUE_VALUE_KEY];
}
if (false === $object && isset($context[self::FALSE_VALUE_KEY])) {
return $context[self::FALSE_VALUE_KEY];
}

return $object;
}

/**
* @inheritDoc
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
if (true === $data) {
return isset($context[self::TRUE_VALUE_KEY]);
}
if (false === $data) {
return isset($context[self::FALSE_VALUE_KEY]);
}
return is_scalar($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ScalarNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class ScalarNormalizerTest extends TestCase
{
protected SerializerInterface&NormalizerInterface $serializer;

protected function setUp(): void
{
parent::setUp();
$this->createNormalizer();
}


protected function createNormalizer(): void
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$scalarNormalizer = new ScalarNormalizer();
$xmlEncoder = new XmlEncoder();
$this->serializer = new Serializer([$normalizer, $scalarNormalizer], [$xmlEncoder]);
}

public function testBoolWithoutContext()
{
self::assertXmlStringEqualsXmlString('<?xml version="1.0"?>
<response><fooTrue>1</fooTrue><fooFalse>0</fooFalse></response>',
$this->serializer->serialize(new ObjectWithBool(), 'xml'));
}

public function testBoolWithContext()
{
self::assertXmlStringEqualsXmlString('<?xml version="1.0"?>
<response><fooTrue>true</fooTrue><fooFalse>false</fooFalse></response>',
$this->serializer->serialize(new ObjectWithBool(), 'xml', [
ScalarNormalizer::TRUE_VALUE_KEY => 'true',
ScalarNormalizer::FALSE_VALUE_KEY => 'false'
]));
}
}


class ObjectWithBool
{
public bool $fooTrue = true;
public bool $fooFalse = false;
}
Loading