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