Config.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Yansongda\Pay\Support;
  3. use ArrayAccess;
  4. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  5. class Config implements ArrayAccess
  6. {
  7. /**
  8. * @var array
  9. */
  10. protected $config;
  11. /**
  12. * Config constructor.
  13. *
  14. * @param array $config
  15. */
  16. public function __construct(array $config = [])
  17. {
  18. $this->config = $config;
  19. }
  20. /**
  21. * get a config.
  22. *
  23. * @author JasonYan <me@yansongda.cn>
  24. *
  25. * @param string $key
  26. * @param string $default
  27. *
  28. * @return mixed
  29. */
  30. public function get($key = null, $default = null)
  31. {
  32. $config = $this->config;
  33. if (is_null($key)) {
  34. return $config;
  35. }
  36. if (isset($config[$key])) {
  37. return $config[$key];
  38. }
  39. foreach (explode('.', $key) as $segment) {
  40. if (!is_array($config) || !array_key_exists($segment, $config)) {
  41. return $default;
  42. }
  43. $config = $config[$segment];
  44. }
  45. return $config;
  46. }
  47. /**
  48. * set a config.
  49. *
  50. * @author JasonYan <me@yansongda.cn>
  51. *
  52. * @param string $key
  53. * @param array $value
  54. */
  55. public function set(string $key, $value)
  56. {
  57. if ($key == '') {
  58. throw new InvalidArgumentException('Invalid config key.');
  59. }
  60. // 只支持三维数组,多余无意义
  61. $keys = explode('.', $key);
  62. switch (count($keys)) {
  63. case '1':
  64. $this->config[$key] = $value;
  65. break;
  66. case '2':
  67. $this->config[$keys[0]][$keys[1]] = $value;
  68. break;
  69. case '3':
  70. $this->config[$keys[0]][$keys[1]][$keys[2]] = $value;
  71. break;
  72. default:
  73. throw new InvalidArgumentException('Invalid config key.');
  74. }
  75. return $this->config;
  76. }
  77. /**
  78. * [offsetExists description].
  79. *
  80. * @author JasonYan <me@yansongda.cn>
  81. *
  82. * @param string $offset
  83. *
  84. * @return bool
  85. */
  86. public function offsetExists($offset)
  87. {
  88. return array_key_exists($offset, $this->config);
  89. }
  90. /**
  91. * [offsetGet description].
  92. *
  93. * @author JasonYan <me@yansongda.cn>
  94. *
  95. * @param string $offset
  96. *
  97. * @return mixed
  98. */
  99. public function offsetGet($offset)
  100. {
  101. return $this->get($offset);
  102. }
  103. /**
  104. * [offsetSet description].
  105. *
  106. * @author JasonYan <me@yansongda.cn>
  107. *
  108. * @param string $offset
  109. * @param string $value
  110. *
  111. * @return array
  112. */
  113. public function offsetSet($offset, $value)
  114. {
  115. $this->set($offset, $value);
  116. }
  117. /**
  118. * [offsetUnset description].
  119. *
  120. * @author JasonYan <me@yansongda.cn>
  121. *
  122. * @param string $offset
  123. *
  124. * @return array
  125. */
  126. public function offsetUnset($offset)
  127. {
  128. $this->set($offset, null);
  129. }
  130. }