Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/translation/Util/ArrayConverter.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 4c8ae668cc8c |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
25 */ | 25 */ |
26 class ArrayConverter | 26 class ArrayConverter |
27 { | 27 { |
28 /** | 28 /** |
29 * Converts linear messages array to tree-like array. | 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')). | 30 * For example this rray('foo.bar' => 'value') will be converted to ['foo' => ['bar' => 'value']]. |
31 * | 31 * |
32 * @param array $messages Linear messages array | 32 * @param array $messages Linear messages array |
33 * | 33 * |
34 * @return array Tree-like messages array | 34 * @return array Tree-like messages array |
35 */ | 35 */ |
36 public static function expandToTree(array $messages) | 36 public static function expandToTree(array $messages) |
37 { | 37 { |
38 $tree = array(); | 38 $tree = []; |
39 | 39 |
40 foreach ($messages as $id => $value) { | 40 foreach ($messages as $id => $value) { |
41 $referenceToElement = &self::getElementByPath($tree, explode('.', $id)); | 41 $referenceToElement = &self::getElementByPath($tree, explode('.', $id)); |
42 | 42 |
43 $referenceToElement = $value; | 43 $referenceToElement = $value; |
52 { | 52 { |
53 $elem = &$tree; | 53 $elem = &$tree; |
54 $parentOfElem = null; | 54 $parentOfElem = null; |
55 | 55 |
56 foreach ($parts as $i => $part) { | 56 foreach ($parts as $i => $part) { |
57 if (isset($elem[$part]) && is_string($elem[$part])) { | 57 if (isset($elem[$part]) && \is_string($elem[$part])) { |
58 /* Process next case: | 58 /* Process next case: |
59 * 'foo': 'test1', | 59 * 'foo': 'test1', |
60 * 'foo.bar': 'test2' | 60 * 'foo.bar': 'test2' |
61 * | 61 * |
62 * $tree['foo'] was string before we found array {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'; | 63 * Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2'; |
64 */ | 64 */ |
65 $elem = &$elem[implode('.', array_slice($parts, $i))]; | 65 $elem = &$elem[implode('.', \array_slice($parts, $i))]; |
66 break; | 66 break; |
67 } | 67 } |
68 $parentOfElem = &$elem; | 68 $parentOfElem = &$elem; |
69 $elem = &$elem[$part]; | 69 $elem = &$elem[$part]; |
70 } | 70 } |
71 | 71 |
72 if (is_array($elem) && count($elem) > 0 && $parentOfElem) { | 72 if ($elem && \is_array($elem) && $parentOfElem) { |
73 /* Process next case: | 73 /* Process next case: |
74 * 'foo.bar': 'test1' | 74 * 'foo.bar': 'test1' |
75 * 'foo': 'test2' | 75 * 'foo': 'test2' |
76 * | 76 * |
77 * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`. | 77 * $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`. |
87 private static function cancelExpand(array &$tree, $prefix, array $node) | 87 private static function cancelExpand(array &$tree, $prefix, array $node) |
88 { | 88 { |
89 $prefix .= '.'; | 89 $prefix .= '.'; |
90 | 90 |
91 foreach ($node as $id => $value) { | 91 foreach ($node as $id => $value) { |
92 if (is_string($value)) { | 92 if (\is_string($value)) { |
93 $tree[$prefix.$id] = $value; | 93 $tree[$prefix.$id] = $value; |
94 } else { | 94 } else { |
95 self::cancelExpand($tree, $prefix.$id, $value); | 95 self::cancelExpand($tree, $prefix.$id, $value); |
96 } | 96 } |
97 } | 97 } |