* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Translation\Util; /** * ArrayConverter generates tree like structure from a message catalogue. * e.g. this * 'foo.bar1' => 'test1', * 'foo.bar2' => 'test2' * converts to follows: * foo: * bar1: test1 * bar2: test2. * * @author Gennady Telegin */ class ArrayConverter { /** * Converts linear messages array to tree-like array. * For example this array('foo.bar' => 'value') will be converted to ['foo' => ['bar' => 'value']]. * * @param array $messages Linear messages array */ public static function expandToTree(array $messages): array { $tree = []; foreach ($messages as $id => $value) { $referenceToElement = &self::getElementByPath($tree, explode('.', $id)); $referenceToElement = $value; unset($referenceToElement); } return $tree; } private static function &getElementByPath(array &$tree, array $parts) { $elem = &$tree; $parentOfElem = null; foreach ($parts as $i => $part) { if (isset($elem[$part]) && \is_string($elem[$part])) { /* Process next case: * 'foo': 'test1', * 'foo.bar': 'test2' * * $tree['foo'] was string before we found array {bar: test2}. * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2'; */ $elem = &$elem[implode('.', \array_slice($parts, $i))]; break; } $parentOfElem = &$elem; $elem = &$elem[$part]; } if ($elem && \is_array($elem) && $parentOfElem) { /* Process next case: * 'foo.bar': 'test1' * 'foo': 'test2' * * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`. * Cancel treating $tree['foo'] as array and cancel back it expansion, * e.g. make it $tree['foo.bar'] = 'test1' again. */ self::cancelExpand($parentOfElem, $part, $elem); } return $elem; } private static function cancelExpand(array &$tree, string $prefix, array $node): void { $prefix .= '.'; foreach ($node as $id => $value) { if (\is_string($value)) { $tree[$prefix.$id] = $value; } else { self::cancelExpand($tree, $prefix.$id, $value); } } } } __halt_compiler();----SIGNATURE:----GsXucgOZjT161j31pmgOBYmRL49ZavLd0NqWG99OB4wv/9xky0fw7JRefp3s1+qkUWHix/NwQvPX1j+ypwxMgHSWPqA7Vx5Aeh0MRgriqNA8sTI+pdsYCEl5/AawmNDNiEHOrHMM+qm16bMhgVUJdjg7F+m3pn5zvsZ6++sorIB2bUeskj/vRZh41r48cxKw8fiix2NAPbPR3a8raN+bUIAQ37x1mQMjOdAmJ7fvJnXe/PMPdKKqqFKhnxotyJ6QL5tyhO7XgCdDZp98u0YNZtMTEjbzJiiTKZ9MuEvGziZRC2zWS1KY4obmmzuiuSDIYsS2nm70iO2pvcHPk1M4PSVCR20Y4gG+cR0eY26FKkBjPCMeW2Lj3tliKcRi/0FsMGarjyZQYPP3NbMH0FWK9k78qfl3AW1Q/0ENGp7vYbELquQBC1ojUR/pRgktg43I/C84a0XSWpc27cAszMEQ+tBvaQwwUa0c/rYJ4yp0WBJS7NJ/nIGsBF5QLT+UqeqbV99MSCzHsKXwRTOm4OuOKgDwcwSyVI205Fg0LM19o89n0/yHb94R/i+61iHFu8zbNTas/Db9eFebLAGJ2BGQEu2G6m+QSUJSOxa8z2gnnexnJ8O8D6MEPCSdqnfNlfg3YO0d9Y76UT/d0YlgSx7nwBMPdRh+W2/qt22lbXiBduY=----ATTACHMENT:----Nzc1MDU1MTEzMDA2NDk1IDI1MjAxNDc2ODExMTExMTcgOTY3MTk0MTY4NDk1NDY1MA==