From 5b0544691e5606e16632a5b185cc81cbdeb4ad81 Mon Sep 17 00:00:00 2001 From: Saif Eddin G <29315886+azjezz@users.noreply.github.com> Date: Tue, 28 May 2019 19:14:45 +0100 Subject: [PATCH] [Translation] refactor ArrayLoader::flatten --- .../Translation/Loader/ArrayLoader.php | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/Translation/Loader/ArrayLoader.php b/src/Symfony/Component/Translation/Loader/ArrayLoader.php index 0a6f9f089d5b7..2e9a4285ec97f 100644 --- a/src/Symfony/Component/Translation/Loader/ArrayLoader.php +++ b/src/Symfony/Component/Translation/Loader/ArrayLoader.php @@ -25,7 +25,7 @@ class ArrayLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { - $this->flatten($resource); + $resource = $this->flatten($resource); $catalogue = new MessageCatalogue($locale); $catalogue->add($resource, $domain); @@ -39,28 +39,20 @@ public function load($resource, $locale, $domain = 'messages') * 'key' => ['key2' => ['key3' => 'value']] * Becomes: * 'key.key2.key3' => 'value' - * - * This function takes an array by reference and will modify it - * - * @param array &$messages The array that will be flattened - * @param array $subnode Current subnode being parsed, used internally for recursive calls - * @param string $path Current path being parsed, used internally for recursive calls */ - private function flatten(array &$messages, array $subnode = null, $path = null) + private function flatten(array $messages): array { - if (null === $subnode) { - $subnode = &$messages; - } - foreach ($subnode as $key => $value) { + $result = []; + foreach ($messages as $key => $value) { if (\is_array($value)) { - $nodePath = $path ? $path.'.'.$key : $key; - $this->flatten($messages, $value, $nodePath); - if (null === $path) { - unset($messages[$key]); + foreach ($this->flatten($value) as $k => $v) { + $result[$key.'.'.$k] = $v; } - } elseif (null !== $path) { - $messages[$path.'.'.$key] = $value; + } else { + $result[$key] = $value; } } + + return $result; } }