mirror of
https://github.com/danog/Valinor.git
synced 2024-11-30 04:39:05 +01:00
fix: handle numeric key with camel case source key modifier
Co-authored-by: Nathan Boiron <nathan.boiron@gmail.com>
This commit is contained in:
parent
b7a7d22993
commit
b8a18feadc
@ -11,15 +11,15 @@ use function is_iterable;
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @implements IteratorAggregate<string, mixed>
|
||||
* @implements IteratorAggregate<mixed>
|
||||
*/
|
||||
final class CamelCaseKeys implements IteratorAggregate
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
/** @var array<mixed> */
|
||||
private array $source;
|
||||
|
||||
/**
|
||||
* @param iterable<string, mixed> $source
|
||||
* @param iterable<mixed> $source
|
||||
*/
|
||||
public function __construct(iterable $source)
|
||||
{
|
||||
@ -27,8 +27,8 @@ final class CamelCaseKeys implements IteratorAggregate
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<string, mixed> $source
|
||||
* @return array<string, mixed>
|
||||
* @param iterable<mixed> $source
|
||||
* @return array<mixed>
|
||||
*/
|
||||
private function replace(iterable $source): array
|
||||
{
|
||||
@ -39,6 +39,11 @@ final class CamelCaseKeys implements IteratorAggregate
|
||||
$value = $this->replace($value);
|
||||
}
|
||||
|
||||
if (! is_string($key)) {
|
||||
$result[$key] = $value;
|
||||
continue;
|
||||
}
|
||||
|
||||
$camelCaseKey = $this->camelCaseKeys($key);
|
||||
|
||||
if (isset($result[$camelCaseKey])) {
|
||||
|
118
tests/Unit/Mapper/Source/Modifier/CamelCaseKeysTest.php
Normal file
118
tests/Unit/Mapper/Source/Modifier/CamelCaseKeysTest.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CuyZ\Valinor\Tests\Unit\Mapper\Source\Modifier;
|
||||
|
||||
use CuyZ\Valinor\Mapper\Source\Modifier\CamelCaseKeys;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CamelCaseKeysTest extends TestCase
|
||||
{
|
||||
public function test_replace_space(): void
|
||||
{
|
||||
$source = new CamelCaseKeys(['some key' => 'foo']);
|
||||
|
||||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_replace_dash(): void
|
||||
{
|
||||
$source = new CamelCaseKeys(['some-key' => 'foo']);
|
||||
|
||||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_replace_underscore(): void
|
||||
{
|
||||
$source = new CamelCaseKeys(['some_key' => 'foo']);
|
||||
|
||||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_root_path_is_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys(['level-one' => 'bar']);
|
||||
|
||||
self::assertSame(['levelOne' => 'bar'], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_sub_path_is_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys([
|
||||
'level-one' => [
|
||||
'level-two' => 'foo',
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
'levelOne' => [
|
||||
'levelTwo' => 'foo',
|
||||
],
|
||||
], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_root_iterable_path_is_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys([
|
||||
['level-one' => 'foo'],
|
||||
['level-one' => 'bar'],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
['levelOne' => 'foo'],
|
||||
['levelOne' => 'bar'],
|
||||
], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_sub_iterable_numeric_path_is_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys([
|
||||
'level-one' => [
|
||||
['level-two' => 'bar'],
|
||||
['level-two' => 'buz'],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
'levelOne' => [
|
||||
['levelTwo' => 'bar'],
|
||||
['levelTwo' => 'buz'],
|
||||
],
|
||||
], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_sub_iterable_string_path_is_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys([
|
||||
'level-one' => [
|
||||
'level-two-a' => ['level-three' => 'bar'],
|
||||
'level-two-b' => ['level-three' => 'buz'],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
'levelOne' => [
|
||||
'levelTwoA' => ['levelThree' => 'bar'],
|
||||
'levelTwoB' => ['levelThree' => 'buz'],
|
||||
],
|
||||
], iterator_to_array($source));
|
||||
}
|
||||
|
||||
public function test_path_with_sub_paths_are_mapped(): void
|
||||
{
|
||||
$source = new CamelCaseKeys([
|
||||
'level-one' => [
|
||||
['level-two' => 'bar'],
|
||||
['level-two' => 'buz'],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
'levelOne' => [
|
||||
['levelTwo' => 'bar'],
|
||||
['levelTwo' => 'buz'],
|
||||
],
|
||||
], iterator_to_array($source));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user