Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\Util; Chris@0: Chris@0: /** Chris@0: * ArrayConverter generates tree like structure from a message catalogue. Chris@0: * e.g. this Chris@0: * 'foo.bar1' => 'test1', Chris@0: * 'foo.bar2' => 'test2' Chris@0: * converts to follows: Chris@0: * foo: Chris@0: * bar1: test1 Chris@0: * bar2: test2. Chris@0: * Chris@0: * @author Gennady Telegin Chris@0: */ Chris@0: class ArrayConverter Chris@0: { Chris@0: /** Chris@0: * Converts linear messages array to tree-like array. Chris@18: * For example this array('foo.bar' => 'value') will be converted to ['foo' => ['bar' => 'value']]. Chris@0: * Chris@0: * @param array $messages Linear messages array Chris@0: * Chris@0: * @return array Tree-like messages array Chris@0: */ Chris@0: public static function expandToTree(array $messages) Chris@0: { Chris@17: $tree = []; Chris@0: Chris@0: foreach ($messages as $id => $value) { Chris@0: $referenceToElement = &self::getElementByPath($tree, explode('.', $id)); Chris@0: Chris@0: $referenceToElement = $value; Chris@0: Chris@0: unset($referenceToElement); Chris@0: } Chris@0: Chris@0: return $tree; Chris@0: } Chris@0: Chris@0: private static function &getElementByPath(array &$tree, array $parts) Chris@0: { Chris@0: $elem = &$tree; Chris@0: $parentOfElem = null; Chris@0: Chris@0: foreach ($parts as $i => $part) { Chris@17: if (isset($elem[$part]) && \is_string($elem[$part])) { Chris@0: /* Process next case: Chris@0: * 'foo': 'test1', Chris@0: * 'foo.bar': 'test2' Chris@0: * Chris@0: * $tree['foo'] was string before we found array {bar: test2}. Chris@0: * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2'; Chris@0: */ Chris@17: $elem = &$elem[implode('.', \array_slice($parts, $i))]; Chris@0: break; Chris@0: } Chris@0: $parentOfElem = &$elem; Chris@0: $elem = &$elem[$part]; Chris@0: } Chris@0: Chris@17: if ($elem && \is_array($elem) && $parentOfElem) { Chris@0: /* Process next case: Chris@0: * 'foo.bar': 'test1' Chris@0: * 'foo': 'test2' Chris@0: * Chris@0: * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`. Chris@0: * Cancel treating $tree['foo'] as array and cancel back it expansion, Chris@0: * e.g. make it $tree['foo.bar'] = 'test1' again. Chris@0: */ Chris@0: self::cancelExpand($parentOfElem, $part, $elem); Chris@0: } Chris@0: Chris@0: return $elem; Chris@0: } Chris@0: Chris@0: private static function cancelExpand(array &$tree, $prefix, array $node) Chris@0: { Chris@0: $prefix .= '.'; Chris@0: Chris@0: foreach ($node as $id => $value) { Chris@17: if (\is_string($value)) { Chris@0: $tree[$prefix.$id] = $value; Chris@0: } else { Chris@0: self::cancelExpand($tree, $prefix.$id, $value); Chris@0: } Chris@0: } Chris@0: } Chris@0: }