composer package updates
[openemr.git] / vendor / symfony / translation / Util / ArrayConverter.php
blob9c0a420a8592f365306e731aa2f165530cb241b2
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Util;
14 /**
15 * ArrayConverter generates tree like structure from a message catalogue.
16 * e.g. this
17 * 'foo.bar1' => 'test1',
18 * 'foo.bar2' => 'test2'
19 * converts to follows:
20 * foo:
21 * bar1: test1
22 * bar2: test2.
24 * @author Gennady Telegin <gtelegin@gmail.com>
26 class ArrayConverter
28 /**
29 * Converts linear messages array to tree-like array.
30 * For example this rray('foo.bar' => 'value') will be converted to array('foo' => array('bar' => 'value')).
32 * @param array $messages Linear messages array
34 * @return array Tree-like messages array
36 public static function expandToTree(array $messages)
38 $tree = array();
40 foreach ($messages as $id => $value) {
41 $referenceToElement = &self::getElementByPath($tree, explode('.', $id));
43 $referenceToElement = $value;
45 unset($referenceToElement);
48 return $tree;
51 private static function &getElementByPath(array &$tree, array $parts)
53 $elem = &$tree;
54 $parentOfElem = null;
56 foreach ($parts as $i => $part) {
57 if (isset($elem[$part]) && is_string($elem[$part])) {
58 /* Process next case:
59 * 'foo': 'test1',
60 * 'foo.bar': 'test2'
62 * $tree['foo'] was string before we found array {bar: test2}.
63 * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2';
65 $elem = &$elem[implode('.', array_slice($parts, $i))];
66 break;
68 $parentOfElem = &$elem;
69 $elem = &$elem[$part];
72 if (is_array($elem) && count($elem) > 0 && $parentOfElem) {
73 /* Process next case:
74 * 'foo.bar': 'test1'
75 * 'foo': 'test2'
77 * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`.
78 * Cancel treating $tree['foo'] as array and cancel back it expansion,
79 * e.g. make it $tree['foo.bar'] = 'test1' again.
81 self::cancelExpand($parentOfElem, $part, $elem);
84 return $elem;
87 private static function cancelExpand(array &$tree, $prefix, array $node)
89 $prefix .= '.';
91 foreach ($node as $id => $value) {
92 if (is_string($value)) {
93 $tree[$prefix.$id] = $value;
94 } else {
95 self::cancelExpand($tree, $prefix.$id, $value);