PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.9.2
Independent Analytics – WordPress Analytics Plugin v2.9.2
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / vendor / symfony / translation / Util / ArrayConverter.php
ArrayConverter.php
122 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace IAWPSCOPED\Symfony\Component\Translation\Util;
12
13 /**
14 * ArrayConverter generates tree like structure from a message catalogue.
15 * e.g. this
16 * 'foo.bar1' => 'test1',
17 * 'foo.bar2' => 'test2'
18 * converts to follows:
19 * foo:
20 * bar1: test1
21 * bar2: test2.
22 *
23 * @author Gennady Telegin <gtelegin@gmail.com>
24 * @internal
25 */
26 class ArrayConverter
27 {
28 /**
29 * Converts linear messages array to tree-like array.
30 * For example this array('foo.bar' => 'value') will be converted to ['foo' => ['bar' => 'value']].
31 *
32 * @param array $messages Linear messages array
33 *
34 * @return array
35 */
36 public static function expandToTree(array $messages)
37 {
38 $tree = [];
39 foreach ($messages as $id => $value) {
40 $referenceToElement =& self::getElementByPath($tree, self::getKeyParts($id));
41 $referenceToElement = $value;
42 unset($referenceToElement);
43 }
44 return $tree;
45 }
46 private static function &getElementByPath(array &$tree, array $parts)
47 {
48 $elem =& $tree;
49 $parentOfElem = null;
50 foreach ($parts as $i => $part) {
51 if (isset($elem[$part]) && \is_string($elem[$part])) {
52 /* Process next case:
53 * 'foo': 'test1',
54 * 'foo.bar': 'test2'
55 *
56 * $tree['foo'] was string before we found array {bar: test2}.
57 * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2';
58 */
59 $elem =& $elem[\implode('.', \array_slice($parts, $i))];
60 break;
61 }
62 $parentOfElem =& $elem;
63 $elem =& $elem[$part];
64 }
65 if ($elem && \is_array($elem) && $parentOfElem) {
66 /* Process next case:
67 * 'foo.bar': 'test1'
68 * 'foo': 'test2'
69 *
70 * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`.
71 * Cancel treating $tree['foo'] as array and cancel back it expansion,
72 * e.g. make it $tree['foo.bar'] = 'test1' again.
73 */
74 self::cancelExpand($parentOfElem, $part, $elem);
75 }
76 return $elem;
77 }
78 private static function cancelExpand(array &$tree, string $prefix, array $node)
79 {
80 $prefix .= '.';
81 foreach ($node as $id => $value) {
82 if (\is_string($value)) {
83 $tree[$prefix . $id] = $value;
84 } else {
85 self::cancelExpand($tree, $prefix . $id, $value);
86 }
87 }
88 }
89 /**
90 * @return string[]
91 */
92 private static function getKeyParts(string $key) : array
93 {
94 $parts = \explode('.', $key);
95 $partsCount = \count($parts);
96 $result = [];
97 $buffer = '';
98 foreach ($parts as $index => $part) {
99 if (0 === $index && '' === $part) {
100 $buffer = '.';
101 continue;
102 }
103 if ($index === $partsCount - 1 && '' === $part) {
104 $buffer .= '.';
105 $result[] = $buffer;
106 continue;
107 }
108 if (isset($parts[$index + 1]) && '' === $parts[$index + 1]) {
109 $buffer .= $part;
110 continue;
111 }
112 if ($buffer) {
113 $result[] = $buffer . $part;
114 $buffer = '';
115 continue;
116 }
117 $result[] = $part;
118 }
119 return $result;
120 }
121 }
122