Skip to content

[String] wrap(): test and fix #33970

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

Merged
merged 1 commit into from
Oct 14, 2019
Merged
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
6 changes: 3 additions & 3 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ public static function wrap(array $values): array
$keys = null;

foreach ($values as $k => $v) {
++$i;

if (\is_string($k) && '' !== $k && $k !== $j = (string) new static($k)) {
$keys = $keys ?? array_keys($values);
array_splice($keys, $i, 1, [$j]);
$keys[$i] = $j;
}

if (\is_string($v)) {
$values[$k] = new static($v);
} elseif (\is_array($v) && $values[$k] !== $v = static::wrap($v)) {
$values[$k] = $v;
}

++$i;
}

return null !== $keys ? array_combine($keys, $values) : $values;
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ public function testCreateFromEmptyString()
$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
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/String/Tests/UnicodeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ protected static function createFromString(string $string): AbstractString
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
{
return array_merge(
Expand Down