8000 [Translation] refactor ArrayLoader::flatten by azjezz · Pull Request #31661 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] refactor ArrayLoader::flatten #31661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/Symfony/Component/Translation/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
}
0