Skip to content

Commit 970d030

Browse files
jeremyFreeAgentnicolas-grekas
authored andcommitted
add mb_check_encoding with array value
1 parent 3794858 commit 970d030

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Mbstring/Mbstring.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,34 @@ public static function mb_encoding_aliases($encoding)
406406

407407
public static function mb_check_encoding($var = null, $encoding = null)
408408
{
409+
if (PHP_VERSION_ID < 70200 && \is_array($var)) {
410+
trigger_error('mb_check_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
411+
412+
return null;
413+
}
414+
409415
if (null === $encoding) {
410416
if (null === $var) {
411417
return false;
412418
}
413419
$encoding = self::$internalEncoding;
414420
}
415421

416-
return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var);
422+
if (!\is_array($var)) {
423+
return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var);
424+
}
425+
426+
foreach ($var as $key => $value) {
427+
if (!self::mb_check_encoding($key, $encoding)) {
428+
return false;
429+
}
430+
if (!self::mb_check_encoding($value, $encoding)) {
431+
return false;
432+
}
433+
}
434+
435+
return true;
436+
417437
}
418438

419439
public static function mb_detect_encoding($str, $encodingList = null, $strict = false)

tests/Mbstring/MbstringTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,53 @@ public function testCheckEncoding()
466466
$this->assertTrue(mb_check_encoding("\xE9", 'Windows-1252'));
467467
}
468468

469+
/**
470+
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_check_encoding
471+
*
472+
* @requires PHP 7.2
473+
*/
474+
public function testCheckEncodingWithArrayValue()
475+
{
476+
$this->assertTrue(mb_check_encoding(['aςσb'], 'UTF8'));
477+
$this->assertTrue(mb_check_encoding(['abc'], 'ASCII'));
478+
$this->assertTrue(mb_check_encoding(["\xE9"], 'Windows-1252'));
479+
480+
$this->assertTrue(mb_check_encoding(['aςσb', 'abc'], 'UTF8'));
481+
$this->assertTrue(mb_check_encoding(["\xE9", 'abc'], 'Windows-1252'));
482+
483+
$this->assertFalse(mb_check_encoding(['aςσb', "\xE9"], 'UTF8'));
484+
$this->assertFalse(mb_check_encoding(['abc', "\xE9"], 'ASCII'));
485+
$this->assertFalse(mb_check_encoding(['abc', 'aςσb'], 'ASCII'));
486+
487+
$this->assertTrue(mb_check_encoding(["\xE9" => "\xE9", 'abc' => 'abc'], 'Windows-1252'));
488+
$this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => 'abc'], 'UTF8'));
489+
490+
$this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', "\xE9" => 'abc'], 'UTF8'));
491+
492+
493+
$this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc', 'aςσb']], 'UTF8'));
494+
$this->assertTrue(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', 'aςσb' => 'aςσb']], 'UTF8'));
495+
496+
497+
$this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', 'aςσb' => "\xE9"]], 'UTF8'));
498+
$this->assertFalse(mb_check_encoding(['aςσb' => 'aςσb', 'abc' => ['abc' => 'abc', "\xE9" => 'aςσb']], 'UTF8'));
499+
}
500+
501+
/**
502+
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_check_encoding
503+
*
504+
* @requires PHP < 7.2
505+
*/
506+
public function testCheckEncodingWithArrayValueForPhpLessThan72()
507+
{
508+
$errorMessage = null;
509+
set_error_handler(function ($type, $msg, $file, $line) use (&$errorMessage) { $errorMessage = \E_USER_WARNING === $type || \E_WARNING === $type ? $msg : null; });
510+
$this->assertNull(mb_check_encoding(['aςσb'], 'UTF8'));
511+
restore_error_handler();
512+
$this->assertSame('mb_check_encoding() expects parameter 1 to be string, array given', $errorMessage);
513+
}
514+
515+
469516
/**
470517
* @covers \Symfony\Polyfill\Mbstring\Mbstring::mb_detect_encoding
471518
*/

0 commit comments

Comments
 (0)