Converter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Configuration data converter. Converts associative array to tree array
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Config\Scope;
  9. class Converter implements \Magento\Framework\Config\ConverterInterface
  10. {
  11. /**
  12. * Convert config data
  13. *
  14. * @param array $source
  15. * @return array
  16. */
  17. public function convert($source)
  18. {
  19. $output = [];
  20. foreach ($source as $key => $value) {
  21. $this->_setArrayValue($output, $key, $value);
  22. }
  23. return $output;
  24. }
  25. /**
  26. * Set array value by path
  27. *
  28. * @param array &$container
  29. * @param string $path
  30. * @param string $value
  31. * @return void
  32. */
  33. protected function _setArrayValue(array &$container, $path, $value)
  34. {
  35. $segments = explode('/', $path);
  36. $currentPointer = & $container;
  37. foreach ($segments as $segment) {
  38. if (!isset($currentPointer[$segment])) {
  39. $currentPointer[$segment] = [];
  40. }
  41. $currentPointer = & $currentPointer[$segment];
  42. }
  43. $currentPointer = $value;
  44. }
  45. }