Skip to content

Commit 8684a5e

Browse files
committed
[Mbstring] The first arg of mb_convert_encoding can be an array
1 parent 418ec75 commit 8684a5e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Mbstring/Mbstring.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ final class Mbstring
8282

8383
public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
8484
{
85+
if (\is_array($s)) {
86+
if (PHP_VERSION_ID < 70200) {
87+
trigger_error('mb_convert_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
88+
89+
return null;
90+
}
91+
92+
$r = [];
93+
foreach ($s as $str) {
94+
$r[] = self::mb_convert_encoding($str, $toEncoding, $fromEncoding);
95+
}
96+
97+
return $r;
98+
}
99+
85100
if (\is_array($fromEncoding) || (null !== $fromEncoding && false !== strpos($fromEncoding, ','))) {
86101
$fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
87102
} else {

tests/Mbstring/MbstringTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ public function testConvertEncoding()
7575
$this->assertSame('déjà', mb_convert_encoding('d&eacute;j&#224;', 'Utf-8', 'Html-entities'));
7676
$this->assertSame('déjà', mb_convert_encoding(utf8_decode('déjà'), 'Utf-8', 'ASCII,ISO-2022-JP,UTF-8,ISO-8859-1'));
7777
$this->assertSame('déjà', mb_convert_encoding(utf8_decode('déjà'), 'Utf-8', ['ASCII', 'ISO-2022-JP', 'UTF-8', 'ISO-8859-1']));
78+
$this->assertSame(['déjà', ''], mb_convert_encoding(['d&eacute;j&#224;', 'l&#224;'], 'Utf-8', 'Html-entities'));
79+
}
80+
81+
/**
82+
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_convert_encoding
83+
*
84+
* @requires PHP < 7.2
85+
*/
86+
public function testConvertEncodingWithArrayValueForPhpLessThan72()
87+
{
88+
$errorMessage = null;
89+
set_error_handler(function ($type, $msg, $file, $line) use (&$errorMessage) { $errorMessage = \E_USER_WARNING === $type || \E_WARNING === $type ? $msg : null; });
90+
$this->assertNull(mb_convert_encoding(['d&eacute;j&#224;', 'l&#224;'], 'Utf-8', 'Html-entities'));
91+
restore_error_handler();
92+
$this->assertSame('mb_convert_encoding() expects parameter 1 to be string, array given', $errorMessage);
7893
}
7994

8095
/**

0 commit comments

Comments
 (0)