-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Name converter support #13120
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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; | ||
|
||
/** | ||
* CamelCase to Underscore name converter. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface | ||
{ | ||
/** | ||
* @var array|null | ||
*/ | ||
private $attributes; | ||
/** | ||
* @var bool | ||
*/ | ||
private $lowerCamelCase; | ||
|
||
/** | ||
* @param null|array $attributes The list of attributes to rename or null for all attributes. | ||
* @param bool $lowerCamelCase Use lowerCamelCase style. | ||
*/ | ||
public function __construct(array $attributes = null, $lowerCamelCase = true) | ||
{ | ||
$this->attributes = $attributes; | ||
$this->lowerCamelCase = $lowerCamelCase; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($propertyName) | ||
{ | ||
if (null === $this->attributes || in_array($propertyName, $this->attributes)) { | ||
$snakeCasedName = ''; | ||
|
||
$len = strlen($propertyName); | ||
for ($i = 0; $i < $len; $i++) { | ||
if (ctype_upper($propertyName[$i])) { | ||
$snakeCasedName .= '_'.strtolower($propertyName[$i]); | ||
} else { | ||
$snakeCasedName .= strtolower($propertyName[$i]); | ||
} | ||
} | ||
|
||
return $snakeCasedName; | ||
} | ||
|
||
return $propertyName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($propertyName) | ||
{ | ||
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { | ||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); | ||
}, $propertyName); | ||
|
||
if ($this->lowerCamelCase) { | ||
$camelCasedName = lcfirst($camelCasedName); | ||
} | ||
|
||
if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) { | ||
return $this->lowerCamelCase ? lcfirst($camelCasedName) : $camelCasedName; | ||
} | ||
|
||
return $propertyName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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; | ||
|
||
/** | ||
* Defines the interface for property name converters. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
interface NameConverterInterface | ||
{ | ||
/** | ||
* Converts a property name to its normalized value. | ||
* | ||
* @param string $propertyName | ||
* @return string | ||
*/ | ||
public function normalize($propertyName); | ||
|
||
/** | ||
* Converts a property name to its denormalized value. | ||
* | ||
* @param string $propertyName | ||
* @return string | ||
*/ | ||
public function denormalize($propertyName); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
use Symfony\Component\Serializer\Exception\CircularReferenceException; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; | ||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
/** | ||
* Normalizer implementation. | ||
|
@@ -25,18 +27,21 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N | |
protected $circularReferenceLimit = 1; | ||
protected $circularReferenceHandler; | ||
protected $classMetadataFactory; | ||
protected $nameConverter; | ||
protected $callbacks = array(); | ||
protected $ignoredAttributes = array(); | ||
protected $camelizedAttributes = array(); | ||
|
||
/** | ||
* Sets the {@link ClassMetadataFactory} to use. | ||
* | ||
* @param ClassMetadataFactory $classMetadataFactory | ||
* @param ClassMetadataFactory|null $classMetadataFactory | ||
* @param NameConverterInterface|null $nameConverter | ||
*/ | ||
public function __construct(ClassMetadataFactory $classMetadataFactory = null) | ||
public function __construct(ClassMetadataFactory $classMetadataFactory = null, NameConverterInterface $nameConverter = null) | ||
{ | ||
$this->classMetadataFactory = $classMetadataFactory; | ||
$this->nameConverter = $nameConverter; | ||
} | ||
|
||
/** | ||
|
@@ -114,13 +119,28 @@ public function setIgnoredAttributes(array $ignoredAttributes) | |
/** | ||
* Set attributes to be camelized on denormalize. | ||
* | ||
* @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead. | ||
* | ||
* @param array $camelizedAttributes | ||
* | ||
* @return self | ||
*/ | ||
public function setCamelizedAttributes(array $camelizedAttributes) | ||
{ | ||
$this->camelizedAttributes = $camelizedAttributes; | ||
trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) { | ||
throw new \LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__)); | ||
} | ||
|
||
$attributes = array(); | ||
foreach ($camelizedAttributes as $camelizedAttribute) { | ||
$attributes[] = lcfirst(preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { | ||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); | ||
}, $camelizedAttribute)); | ||
} | ||
|
||
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break if a NameConverter was passed in the constructor (you will removed the existing one entirely) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is intended. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception added. |
||
|
||
return $this; | ||
} | ||
|
@@ -178,18 +198,17 @@ protected function handleCircularReference($object) | |
/** | ||
* Format an attribute name, for example to convert a snake_case name to camelCase. | ||
* | ||
* @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead. | ||
* | ||
* @param string $attributeName | ||
* | ||
* @return string | ||
*/ | ||
protected function formatAttribute($attributeName) | ||
{ | ||
if (in_array($attributeName, $this->camelizedAttributes)) { | ||
return preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { | ||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); | ||
}, $attributeName); | ||
} | ||
trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
return $attributeName; | ||
return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName; | ||
} | ||
|
||
/** | ||
|
@@ -272,14 +291,15 @@ protected function instantiateObject(array $data, $class, array &$context, \Refl | |
|
||
$params = array(); | ||
foreach ($constructorParameters as $constructorParameter) { | ||
$paramName = lcfirst($this->formatAttribute($constructorParameter->name)); | ||
$paramName = $constructorParameter->name; | ||
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName; | ||
|
||
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes); | ||
$ignored = in_array($paramName, $this->ignoredAttributes); | ||
if ($allowed && !$ignored && isset($data[$paramName])) { | ||
$params[] = $data[$paramName]; | ||
if ($allowed && !$ignored && isset($data[$key])) { | ||
$params[] = $data[$key]; | ||
// don't run set for a parameter passed to the constructor | ||
unset($data[$paramName]); | ||
unset($data[$key]); | ||
} elseif ($constructorParameter->isOptional()) { | ||
$params[] = $constructorParameter->getDefaultValue(); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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 Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class CamelCaseToSnakeCaseNameConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider attributeProvider | ||
*/ | ||
public function testNormalize($underscored, $lowerCamelCased) | ||
{ | ||
$nameConverter = new CamelCaseToSnakeCaseNameConverter(); | ||
$this->assertEquals($nameConverter->normalize($lowerCamelCased), $underscored); | ||
} | ||
|
||
/** | ||
* @dataProvider attributeProvider | ||
*/ | ||
public function testDenormalize($underscored, $lowerCamelCased) | ||
{ | ||
$nameConverter = new CamelCaseToSnakeCaseNameConverter(); | ||
$this->assertEquals($nameConverter->denormalize($underscored), $lowerCamelCased); | ||
} | ||
|
||
public function attributeProvider() | ||
{ | ||
return array( | ||
array('coop_tilleuls', 'coopTilleuls'), | ||
array('_kevin_dunglas', '_kevinDunglas'), | ||
array('this_is_a_test', 'thisIsATest'), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, you should submit a PR on 2.6 for this one.