Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Util/ArrayConverter.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation\Util;
13
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.
23 *
24 * @author Gennady Telegin <[email protected]>
25 */
26 class ArrayConverter
27 {
28 /**
29 * Converts linear messages array to tree-like array.
30 * For example: ['foo.bar' => 'value'] will be converted to ['foo' => ['bar' => 'value']].
31 *
32 * @param array $messages Linear messages array
33 */
34 public static function expandToTree(array $messages): array
35 {
36 $tree = [];
37
38 foreach ($messages as $id => $value) {
39 $referenceToElement = &self::getElementByPath($tree, self::getKeyParts($id));
40
41 $referenceToElement = $value;
42
43 unset($referenceToElement);
44 }
45
46 return $tree;
47 }
48
49 private static function &getElementByPath(array &$tree, array $parts): mixed
50 {
51 $elem = &$tree;
52 $parentOfElem = null;
53
54 foreach ($parts as $i => $part) {
55 if (isset($elem[$part]) && \is_string($elem[$part])) {
56 /* Process next case:
57 * 'foo': 'test1',
58 * 'foo.bar': 'test2'
59 *
60 * $tree['foo'] was string before we found array {bar: test2}.
61 * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2';
62 */
63 $elem = &$elem[implode('.', \array_slice($parts, $i))];
64 break;
65 }
66
67 $parentOfElem = &$elem;
68 $elem = &$elem[$part];
69 }
70
71 if ($elem && \is_array($elem) && $parentOfElem) {
72 /* Process next case:
73 * 'foo.bar': 'test1'
74 * 'foo': 'test2'
75 *
76 * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`.
77 * Cancel treating $tree['foo'] as array and cancel back it expansion,
78 * e.g. make it $tree['foo.bar'] = 'test1' again.
79 */
80 self::cancelExpand($parentOfElem, $part, $elem);
81 }
82
83 return $elem;
84 }
85
86 private static function cancelExpand(array &$tree, string $prefix, array $node): void
87 {
88 $prefix .= '.';
89
90 foreach ($node as $id => $value) {
91 if (\is_string($value)) {
92 $tree[$prefix.$id] = $value;
93 } else {
94 self::cancelExpand($tree, $prefix.$id, $value);
95 }
96 }
97 }
98
99 /**
100 * @return string[]
101 */
102 private static function getKeyParts(string $key): array
103 {
104 $parts = explode('.', $key);
105 $partsCount = \count($parts);
106
107 $result = [];
108 $buffer = '';
109
110 foreach ($parts as $index => $part) {
111 if (0 === $index && '' === $part) {
112 $buffer = '.';
113
114 continue;
115 }
116
117 if ($index === $partsCount - 1 && '' === $part) {
118 $buffer .= '.';
119 $result[] = $buffer;
120
121 continue;
122 }
123
124 if (isset($parts[$index + 1]) && '' === $parts[$index + 1]) {
125 $buffer .= $part;
126
127 continue;
128 }
129
130 if ($buffer) {
131 $result[] = $buffer.$part;
132 $buffer = '';
133
134 continue;
135 }
136
137 $result[] = $part;
138 }
139
140 return $result;
141 }
142 }