Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05d6d31

Browse files
committed
minor #31661 [Translation] refactor ArrayLoader::flatten (azjezz)
This PR was squashed before being merged into the 4.4 branch (closes #31661). Discussion ---------- [Translation] refactor ArrayLoader::flatten | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Commits ------- 5b05446 [Translation] refactor ArrayLoader::flatten
2 parents a2b79e1 + 5b05446 commit 05d6d31

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/Symfony/Component/Translation/Loader/ArrayLoader.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ArrayLoader implements LoaderInterface
2525
*/
2626
public function load($resource, $locale, $domain = 'messages')
2727
{
28-
$this->flatten($resource);
28+
$resource = $this->flatten($resource);
2929
$catalogue = new MessageCatalogue($locale);
3030
$catalogue->add($resource, $domain);
3131

@@ -39,28 +39,20 @@ public function load($resource, $locale, $domain = 'messages')
3939
* 'key' => ['key2' => ['key3' => 'value']]
4040
* Becomes:
4141
* 'key.key2.key3' => 'value'
42-
*
43-
* This function takes an array by reference and will modify it
44-
*
45-
* @param array &$messages The array that will be flattened
46-
* @param array $subnode Current subnode being parsed, used internally for recursive calls
47-
* @param string $path Current path being parsed, used internally for recursive calls
4842
*/
49-
private function flatten(array &$messages, array $subnode = null, $path = null)
43+
private function flatten(array $messages): array
5044
{
51-
if (null === $subnode) {
52-
$subnode = &$messages;
53-
}
54-
foreach ($subnode as $key => $value) {
45+
$result = [];
46+
foreach ($messages as $key => $value) {
5547
if (\is_array($value)) {
56-
$nodePath = $path ? $path.'.'.$key : $key;
57-
$this->flatten($messages, $value, $nodePath);
58-
if (null === $path) {
59-
unset($messages[$key]);
48+
foreach ($this->flatten($value) as $k => $v) {
49+
$result[$key.'.'.$k] = $v;
6050
}
61-
} elseif (null !== $path) {
62-
$messages[$path.'.'.$key] = $value;
51+
} else {
52+
$result[$key] = $value;
6353
}
6454
}
55+
56+
return $result;
6557
}
6658
}

0 commit comments

Comments
 (0)