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

Skip to content

Commit bef5a34

Browse files
authored
[Translation] refactor ArrayLoader::flatten
1 parent 7f40716 commit bef5a34

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

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

Lines changed: 10 additions & 17 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

@@ -40,27 +40,20 @@ public function load($resource, $locale, $domain = 'messages')
4040
* Becomes:
4141
* 'key.key2.key3' => 'value'
4242
*
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
4843
*/
49-
private function flatten(array &$messages, array $subnode = null, $path = null)
44+
private function flatten(array $messages): array
5045
{
51-
if (null === $subnode) {
52-
$subnode = &$messages;
53-
}
54-
foreach ($subnode as $key => $value) {
46+
$result = [];
47+
foreach ($messages as $key => $value) {
5548
if (\is_array($value)) {
56-
$nodePath = $path ? $path.'.'.$key : $key;
57-
$this->flatten($messages, $value, $nodePath);
58-
if (null === $path) {
59-
unset($messages[$key]);
49+
foreach($this->flatten($value) as $k => $v) {
50+
$result[$key.'.'.$k] = $v;
6051
}
61-
} elseif (null !== $path) {
62-
$messages[$path.'.'.$key] = $value;
52+
} else {
53+
$result[$key] = $value;
6354
}
6455
}
56+
57+
return $result;
6558
}
6659
}

0 commit comments

Comments
 (0)