8000 add context to force snake_case · symfony/symfony@796cf00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 796cf00

Browse files
add context to force snake_case
1 parent bc2d994 commit 796cf00

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@
1111

1212
namespace Symfony\Component\Serializer\NameConverter;
1313

14+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
15+
1416
/**
1517
* CamelCase to Underscore name converter.
1618
*
1719
* @author Kévin Dunglas <dunglas@gmail.com>
20+
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
1821
*/
19-
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
22+
class CamelCaseToSnakeCaseNameConverter implements AdvancedNameConverterInterface
2023
{
24+
/**
25+
* Require all properties to be written in snake_case.
26+
*/
27+
public const REQUIRE_SNAKE_CASE_PROPERTIES = 'require_snake_case_properties';
28+
2129
/**
2230
* @param array|null $attributes The list of attributes to rename or null for all attributes
2331
* @param bool $lowerCamelCase Use lowerCamelCase style
@@ -28,7 +36,7 @@ public function __construct(
2836
) {
2937
}
3038

31-
public function normalize(string $propertyName): string
39+
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3240
{
3341
if (null === $this->attributes || \in_array($propertyName, $this->attributes, true)) {
3442
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
@@ -37,8 +45,12 @@ public function normalize(string $propertyName): string
3745
return $propertyName;
3846
}
3947

40-
public function denormalize(string $propertyName): string
48+
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
4149
{
50+
if (($context[self::REQUIRE_SNAKE_CASE_PROPERTIES] ?? false) && $propertyName !== $this->normalize($propertyName, $class, $format, $context)) {
51+
throw new NotNormalizableValueException('Could not denormalize property, snake case format accepted only.');
52+
}
53+
4254
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', fn ($match) => ('.' === $match[1] ? '_' : '').strtoupper($match[2]), $propertyName);
4355

4456
if ($this->lowerCamelCase) {

src/Symfony/Component/Serializer/Tests/NameConverter/CamelCaseToSnakeCaseNameConverterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace Symfony\Component\Serializer\Tests\NameConverter;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1516
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1617
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1718

1819
/**
1920
* @author Kévin Dunglas <dunglas@gmail.com>
21+
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
2022
*/
2123
class CamelCaseToSnakeCaseNameConverterTest extends TestCase
2224
{
@@ -55,4 +57,20 @@ public static function attributeProvider()
5557
['this_is_a_test', 'ThisIsATest', false],
5658
];
5759
}
60+
61+
public function testDenormalizeWithContext()
62+
{
63+
$nameConverter = new CamelCaseToSnakeCaseNameConverter(null, true);
64+
$denormalizedValue = $nameConverter->denormalize('last_name', context: [CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES]);
65+
66+
$this->assertSame($denormalizedValue, 'lastName');
67+
}
68+
69+
public function testErrorDenormalizeWithContext()
70+
{
71+
$nameConverter = new CamelCaseToSnakeCaseNameConverter(null, true);
72+
$nameConverter->denormalize('lastName', context: [CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES => true]);
73+
74+
$this->expectException(NotNormalizableValueException::class);
75+
}
5876
}

0 commit comments

Comments
 (0)
0