Skip to content

Commit 2357270

Browse files
bug #479 [Mbstring] The first arg of mb_convert_encoding can be an array (alanpoulain)
This PR was merged into the 1.x branch. Discussion ---------- [Mbstring] The first arg of mb_convert_encoding can be an array As the [documentation](https://www.php.net/manual/en/function.mb-convert-encoding.php) says, since PHP 7.2 the first argument of `mb_convert_encoding` can be an array. Commits ------- b1a64e5 [Mbstring] The first arg of mb_convert_encoding can be an array
2 parents 8a4bd23 + b1a64e5 commit 2357270

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ public function testConvertEncoding()
7777
$this->assertSame('déjà', mb_convert_encoding(utf8_decode('déjà'), 'Utf-8', ['ASCII', 'ISO-2022-JP', 'UTF-8', 'ISO-8859-1']));
7878
}
7979

80+
/**
81+
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_convert_encoding
82+
*
83+
* @requires PHP 7.2
84+
*/
85+
public function testConvertEncodingWithArrayValue()
86+
{
87+
$this->assertSame(['déjà', ''], mb_convert_encoding(['d&eacute;j&#224;', 'l&#224;'], 'Utf-8', 'Html-entities'));
88+
}
89+
90+
/**
91+
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_convert_encoding
92+
*
93+
* @requires PHP < 7.2
94+
*/
95+
public function testConvertEncodingWithArrayValueForPhpLessThan72()
96+
{
97+
$errorMessage = null;
98+
set_error_handler(function ($type, $msg, $file, $line) use (&$errorMessage) { $errorMessage = \E_USER_WARNING === $type || \E_WARNING === $type ? $msg : null; });
99+
$this->assertNull(mb_convert_encoding(['d&eacute;j&#224;', 'l&#224;'], 'Utf-8', 'Html-entities'));
100+
restore_error_handler();
101+
$this->assertSame('mb_convert_encoding() expects parameter 1 to be string, array given', $errorMessage);
102+
}
103+
80104
/**
81105
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_decode_numericentity
82106
*/

0 commit comments

Comments
 (0)