Skip to content

[Serializer] add xml name converter to use objects instead of arrays #35087

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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Serializer\NameConverter;

final class XmlAttributesConverter implements NameConverterInterface
{
const ATTRIBUTE_PREFIX = 'attr';
const NODE_VALUE_ATTRIBUTE_NAME = 'value';

private $attributePrefix;
private $nodeValueAttributeName;

public function __construct(string $attributePrefix = self::ATTRIBUTE_PREFIX, string $nodeValueAttributeName = self::NODE_VALUE_ATTRIBUTE_NAME)
{
$this->attributePrefix = $attributePrefix;
$this->nodeValueAttributeName = $nodeValueAttributeName;
}

/**
* {@inheritdoc}
*/
public function normalize($propertyName): string
{
if (0 === strncmp($this->nodeValueAttributeName, $propertyName, \strlen($this->nodeValueAttributeName))) {
return '#';
}

if (0 === strncmp($this->attributePrefix, $propertyName, \strlen($this->attributePrefix))) {
return '@'.substr($propertyName, \strlen($this->attributePrefix));
}

return $propertyName;
}

/**
* {@inheritdoc}
*/
public function denormalize($propertyName): string
{
$propertyName = (string) $propertyName;

if (0 === strpos($propertyName, '#')) {
return $this->nodeValueAttributeName;
}

if (0 === strpos($propertyName, '@')) {
return $this->attributePrefix.substr($propertyName, 1);
}

return $propertyName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Serializer\Tests\NameConverter;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\NameConverter\XmlAttributesConverter;

class XmlAttributesConverterTest extends TestCase
{
/**
* @dataProvider normalizeDataProvider
*/
public function testNormalize(?string $attributePrefix, ?string $nodeValueAttributeName, string $propertyName, string $expectedPropertyName): void
{
$xmlAttributeConverter = $this->createXmlAttributesConverter($attributePrefix, $nodeValueAttributeName);
$result = $xmlAttributeConverter->normalize($propertyName);
$this->assertEquals($expectedPropertyName, $result);
}

/**
* @dataProvider denormalizeDataProvider
*/
public function testDenormalize(?string $attributePrefix, ?string $nodeValueAttributeName, string $propertyName, string $expectedPropertyName): void
{
$xmlAttributeConverter = $this->createXmlAttributesConverter($attributePrefix, $nodeValueAttributeName);
$result = $xmlAttributeConverter->denormalize($propertyName);
$this->assertEquals($expectedPropertyName, $result);
}

public function normalizeDataProvider()
{
return [
'defaults to attr extra attribute' => [null, null, 'attrOwnerID', '@OwnerID'],
'no extra attributes' => [null, null, 'someOtherParam', 'someOtherParam'],
'node value' => [null, null, 'value', '#'],
'custom extra attribute prefix' => ['someAttributePrefix', 'nodeValue', 'someAttributePrefixOwnerID', '@OwnerID'],
'custom node value attribute' => ['someAttributePrefix', 'nodeValue', 'nodeValue', '#'],
];
}

public function denormalizeDataProvider()
{
return [
'defaults to attr extra attribute' => [null, null, '@OwnerID', 'attrOwnerID'],
'no extra attributes' => [null, null, 'SomeOtherParam', 'SomeOtherParam'],
'no extra attributes lowercase' => [null, null, 'someOtherParam', 'someOtherParam'],
'node value' => [null, null, '#', 'value'],
'custom extra attribute prefix' => ['someAttributePrefix', 'nodeValue', '@OwnerID', 'someAttributePrefixOwnerID'],
'custom node value attribute' => ['someAttributePrefix', 'nodeValue', '#', 'nodeValue'],
];
}

private function createXmlAttributesConverter(?string $attributePrefix, ?string $nodeValueAttributeName): XmlAttributesConverter
{
if (null === $attributePrefix && null === $nodeValueAttributeName) {
return new XmlAttributesConverter();
}

return new XmlAttributesConverter($attributePrefix, $nodeValueAttributeName);
}
}