ConfigData.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Data;
  7. /**
  8. * Data transfer object to store config data for config options
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class ConfigData
  13. {
  14. /**
  15. * File key
  16. *
  17. * @var string
  18. */
  19. private $fileKey;
  20. /**
  21. * Data
  22. *
  23. * @var array
  24. */
  25. private $data = [];
  26. /**
  27. * Override previous config options when save
  28. *
  29. * @var bool
  30. */
  31. private $overrideWhenSave = false;
  32. /**
  33. * Constructor
  34. *
  35. * @param string $fileKey
  36. */
  37. public function __construct($fileKey)
  38. {
  39. $this->fileKey = $fileKey;
  40. }
  41. /**
  42. * Gets File Key
  43. *
  44. * @return string
  45. */
  46. public function getFileKey()
  47. {
  48. return $this->fileKey;
  49. }
  50. /**
  51. * Gets Data
  52. *
  53. * @return array
  54. */
  55. public function getData()
  56. {
  57. return $this->data;
  58. }
  59. /**
  60. * Sets override when save flag
  61. *
  62. * @param bool $overrideWhenSave
  63. * @return void
  64. * @since 100.0.5
  65. */
  66. public function setOverrideWhenSave($overrideWhenSave)
  67. {
  68. $this->overrideWhenSave = $overrideWhenSave;
  69. }
  70. /**
  71. * Gets override when save flag
  72. *
  73. * @return bool
  74. * @since 100.0.5
  75. */
  76. public function isOverrideWhenSave()
  77. {
  78. return $this->overrideWhenSave;
  79. }
  80. /**
  81. * Updates a value in ConfigData configuration by specified path
  82. *
  83. * @param string $path
  84. * @param mixed $value
  85. * @return void
  86. */
  87. public function set($path, $value)
  88. {
  89. $chunks = $this->expand($path);
  90. $data = [];
  91. $element = &$data;
  92. while ($chunks) {
  93. $key = array_shift($chunks);
  94. if ($chunks) {
  95. $element[$key] = [];
  96. $element = &$element[$key];
  97. } else {
  98. $element[$key] = $value;
  99. }
  100. }
  101. $this->data = array_replace_recursive($this->data, $data);
  102. }
  103. /**
  104. * Expands a path into chunks
  105. *
  106. * All chunks must be not empty and there must be at least two.
  107. *
  108. * @param string $path
  109. * @return string[]
  110. * @throws \InvalidArgumentException
  111. */
  112. private function expand($path)
  113. {
  114. $chunks = explode('/', $path);
  115. foreach ($chunks as $chunk) {
  116. if ('' == $chunk) {
  117. throw new \InvalidArgumentException(
  118. "Path '$path' is invalid. It cannot be empty nor start or end with '/'"
  119. );
  120. }
  121. }
  122. return $chunks;
  123. }
  124. }