[String] wrap(): test and fix

This commit is contained in:
Gregor Harlan 2019-10-13 16:51:42 +02:00
parent a306d99297
commit 015b81a2f5
No known key found for this signature in database
GPG Key ID: A43C725D9D2AE675
3 changed files with 45 additions and 3 deletions

View File

@ -75,11 +75,9 @@ abstract class AbstractString implements \JsonSerializable
$keys = null; $keys = null;
foreach ($values as $k => $v) { foreach ($values as $k => $v) {
++$i;
if (\is_string($k) && '' !== $k && $k !== $j = (string) new static($k)) { if (\is_string($k) && '' !== $k && $k !== $j = (string) new static($k)) {
$keys = $keys ?? array_keys($values); $keys = $keys ?? array_keys($values);
array_splice($keys, $i, 1, [$j]); $keys[$i] = $j;
} }
if (\is_string($v)) { if (\is_string($v)) {
@ -87,6 +85,8 @@ abstract class AbstractString implements \JsonSerializable
} elseif (\is_array($v) && $values[$k] !== $v = static::wrap($v)) { } elseif (\is_array($v) && $values[$k] !== $v = static::wrap($v)) {
$values[$k] = $v; $values[$k] = $v;
} }
++$i;
} }
return null !== $keys ? array_combine($keys, $values) : $values; return null !== $keys ? array_combine($keys, $values) : $values;

View File

@ -34,6 +34,31 @@ abstract class AbstractAsciiTestCase extends TestCase
$this->assertTrue($instance->isEmpty()); $this->assertTrue($instance->isEmpty());
} }
/**
* @dataProvider provideWrap
*/
public function testWrap(array $expected, array $values)
{
$s = static::createFromString('');
$this->assertEquals($expected, $s::wrap($values));
}
public static function provideWrap(): array
{
return [
[[], []],
[
['abc' => static::createFromString('foo'), 1, static::createFromString('bar'), 'baz' => true],
['abc' => 'foo', 1, 'bar', 'baz' => true],
],
[
['a' => ['b' => static::createFromString('c'), [static::createFromString('d')]], static::createFromString('e')],
['a' => ['b' => 'c', ['d']], 'e'],
],
];
}
/** /**
* @dataProvider provideLength * @dataProvider provideLength
*/ */

View File

@ -21,6 +21,23 @@ class UnicodeStringTest extends AbstractUnicodeTestCase
return new UnicodeString($string); return new UnicodeString($string);
} }
public static function provideWrap(): array
{
return array_merge(
parent::provideWrap(),
[
[
['Käse' => static::createFromString('köstlich'), 'fromage' => static::createFromString('délicieux')],
["Ka\u{0308}se" => "ko\u{0308}stlich", 'fromage' => 'délicieux'],
],
[
['a' => 1, 'ä' => ['ö' => 2, 'ü' => 3]],
['a' => 1, "a\u{0308}" => ["o\u{0308}" => 2, 'ü' => 3]],
],
]
);
}
public static function provideLength(): array public static function provideLength(): array
{ {
return array_merge( return array_merge(