ArrayConverter.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <gtelegin@gmail.com>
*/
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
*
* @return array
*/
public static function expandToTree(array $messages)
{
$tree = [];
foreach ($messages as $id => $value) {
$referenceToElement = &self::getElementByPath($tree, self::getKeyParts($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)
{
$prefix .= '.';
foreach ($node as $id => $value) {
if (\is_string($value)) {
$tree[$prefix.$id] = $value;
} else {
self::cancelExpand($tree, $prefix.$id, $value);
}
}
}
/**
* @return string[]
*/
private static function getKeyParts(string $key): array
{
$parts = explode('.', $key);
$partsCount = \count($parts);
$result = [];
$buffer = '';
foreach ($parts as $index => $part) {
if (0 === $index && '' === $part) {
$buffer = '.';
continue;
}
if ($index === $partsCount - 1 && '' === $part) {
$buffer .= '.';
$result[] = $buffer;
continue;
}
if (isset($parts[$index + 1]) && '' === $parts[$index + 1]) {
$buffer .= $part;
continue;
}
if ($buffer) {
$result[] = $buffer.$part;
$buffer = '';
continue;
}
$result[] = $part;
}
return $result;
}
}