From bc87667a8a55dd3feb8b6d2c42d25e81c14ca3dc Mon Sep 17 00:00:00 2001 From: Manuele Menozzi Date: Fri, 22 Nov 2024 16:51:26 +0100 Subject: [PATCH] Revert "Replace deprecated utf8_encode with mb_convert_encoding" This reverts commit d498e1e59f929e101108454469789df4f0e4a37f. --- .github/workflows/ci.yaml | 2 +- src/NonUtf8Cleaner.php | 29 +++++------------------------ 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7e5cc62..dd14317 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -68,7 +68,7 @@ jobs: run: composer install --no-interaction - name: Composer Require Checker - run: composer-require-checker --config-file=composer-require-checker.json + run: composer-require-checker - name: Run Easy Coding Standard run: vendor/bin/ecs check diff --git a/src/NonUtf8Cleaner.php b/src/NonUtf8Cleaner.php index b1e296d..88a9b1b 100644 --- a/src/NonUtf8Cleaner.php +++ b/src/NonUtf8Cleaner.php @@ -4,6 +4,8 @@ namespace Webgriffe\Esb; +use Monolog\Utils; + /** * @internal */ @@ -15,34 +17,13 @@ class NonUtf8Cleaner */ public static function clean(array $data): array { - array_walk_recursive($data, [__CLASS__, 'cleanString']); + array_walk_recursive($data, [Utils::class, 'detectAndCleanUtf8']); return $data; } public static function cleanString(string $data): string { - // Implementation borrowed from Monolog\Utils::detectAndCleanUtf8() which is no longer a public method as of version 2 - - if (preg_match('//u', $data)) { - return $data; - } - - $data = preg_replace_callback( - '/[\x80-\xFF]+/', - function ($m) { - return function_exists('mb_convert_encoding') ? mb_convert_encoding($m[0], 'UTF-8', 'ISO-8859-1') : utf8_encode($m[0]); - }, - $data - ); - - if (!is_string($data)) { - throw new \RuntimeException('Failed to preg_replace_callback: ' . preg_last_error()); - } - - return str_replace( - ['¤', '¦', '¨', '´', '¸', '¼', '½', '¾'], - ['€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'], - $data - ); + Utils::detectAndCleanUtf8($data); + return $data; } }