Data.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Configuration data container
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Config;
  9. class Data implements DataInterface
  10. {
  11. /**
  12. * Config data
  13. *
  14. * @var array
  15. */
  16. protected $_data = [];
  17. /**
  18. * Config source data
  19. *
  20. * @var array
  21. */
  22. protected $_source = [];
  23. /**
  24. * @param MetadataProcessor $processor
  25. * @param array $data
  26. */
  27. public function __construct(MetadataProcessor $processor, array $data)
  28. {
  29. /** Clone the array to work around a kink in php7 that modifies the argument by reference */
  30. $this->_data = $processor->process($this->arrayClone($data));
  31. $this->_source = $data;
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function getSource()
  37. {
  38. return $this->_source;
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function getValue($path = null)
  44. {
  45. if ($path === null) {
  46. return $this->_data;
  47. }
  48. $keys = explode('/', $path);
  49. $data = $this->_data;
  50. foreach ($keys as $key) {
  51. if (is_array($data) && array_key_exists($key, $data)) {
  52. $data = $data[$key];
  53. } else {
  54. return null;
  55. }
  56. }
  57. return $data;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function setValue($path, $value)
  63. {
  64. $keys = explode('/', $path);
  65. $lastKey = array_pop($keys);
  66. $currentElement = & $this->_data;
  67. foreach ($keys as $key) {
  68. if (!isset($currentElement[$key])) {
  69. $currentElement[$key] = [];
  70. }
  71. $currentElement = & $currentElement[$key];
  72. }
  73. $currentElement[$lastKey] = $value;
  74. }
  75. /**
  76. * Copy array by value
  77. *
  78. * @param array $data
  79. * @return array
  80. */
  81. private function arrayClone(array $data)
  82. {
  83. $clone = [];
  84. foreach ($data as $key => $value) {
  85. $clone[$key]= $value;
  86. }
  87. return $clone;
  88. }
  89. }