Keine Beschreibung

ArrayConverter.php 2.8KB

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