diff --git a/src/Mbstring/Mbstring.php b/src/Mbstring/Mbstring.php index bce5c4a84..e47f3ef83 100644 --- a/src/Mbstring/Mbstring.php +++ b/src/Mbstring/Mbstring.php @@ -406,6 +406,12 @@ public static function mb_encoding_aliases($encoding) public static function mb_check_encoding($var = null, $encoding = null) { + if (PHP_VERSION_ID < 70200 && \is_array($var)) { + trigger_error('mb_check_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING); + + return null; + } + if (null === $encoding) { if (null === $var) { return false; @@ -413,7 +419,21 @@ public static function mb_check_encoding($var = null, $encoding = null) $encoding = self::$internalEncoding; } - return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var); + if (!\is_array($var)) { + return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var); + } + + foreach ($var as $key => $value) { + if (!self::mb_check_encoding($key, $encoding)) { + return false; + } + if (!self::mb_check_encoding($value, $encoding)) { + return false; + } + } + + return true; + } public static function mb_detect_encoding($str, $encodingList = null, $strict = false) diff --git a/tests/Mbstring/MbstringTest.php b/tests/Mbstring/MbstringTest.php index 0118d1099..077c297ad 100644 --- a/tests/Mbstring/MbstringTest.php +++ b/tests/Mbstring/MbstringTest.php @@ -466,6 +466,53 @@ public function testCheckEncoding() $this->assertTrue(mb_check_encoding("\xE9", 'Windows-1252')); } + /** + * @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_check_encoding + * + * @requires PHP 7.2 + */ + public function testCheckEncodingWithArrayValue() + { + $this->assertTrue(mb_check_encoding(['aςσb'], 'UTF8')); + $this->assertTrue(mb_check_encoding(['abc'], 'ASCII')); + $this->assertTrue(mb_check_encoding(["\xE9"], 'Windows-1252')); + + $this->assertTrue(mb_check_encoding(['aςσb', 'abc'], 'UTF8')); + $this->assertTrue(mb_check_encoding(["\xE9", 'abc'], 'Windows-1252')); + + $this->assertFalse(mb_check_encoding(['aςσb', "\xE9"], 'UTF8')); + $this->assertFalse(mb_check_encoding(['abc', "\xE9"], 'ASCII')); + $this->assertFalse(mb_check_encoding(['abc', 'aςσb'], 'ASCII')); + + $this->assertTrue(mb_check_encoding(["\xE9" => "\xE9", 'abc' => 'abc'], 'Windows-1252')); + $this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => 'abc'], 'UTF8')); + + $this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', "\xE9" => 'abc'], 'UTF8')); + + + $this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc', 'aςσb']], 'UTF8')); + $this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', 'aςσb' => 'aςσb']], 'UTF8')); + + + $this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', 'aςσb' => "\xE9"]], 'UTF8')); + $this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', "\xE9" => 'aςσb']], 'UTF8')); + } + + /** + * @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_check_encoding + * + * @requires PHP < 7.2 + */ + public function testCheckEncodingWithArrayValueForPhpLessThan72() + { + $errorMessage = null; + set_error_handler(function ($type, $msg, $file, $line) use (&$errorMessage) { $errorMessage = \E_USER_WARNING === $type || \E_WARNING === $type ? $msg : null; }); + $this->assertNull(mb_check_encoding(['aςσb'], 'UTF8')); + restore_error_handler(); + $this->assertSame('mb_check_encoding() expects parameter 1 to be string, array given', $errorMessage); + } + + /** * @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_detect_encoding */