Config.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms\Support;
  11. use ArrayAccess;
  12. /**
  13. * Class Config.
  14. */
  15. class Config implements ArrayAccess
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $config;
  21. /**
  22. * Config constructor.
  23. *
  24. * @param array $config
  25. */
  26. public function __construct(array $config = [])
  27. {
  28. $this->config = $config;
  29. }
  30. /**
  31. * Get an item from an array using "dot" notation.
  32. *
  33. * @param string $key
  34. * @param mixed $default
  35. *
  36. * @return mixed
  37. */
  38. public function get($key, $default = null)
  39. {
  40. $config = $this->config;
  41. if (isset($config[$key])) {
  42. return $config[$key];
  43. }
  44. if (false === strpos($key, '.')) {
  45. return $default;
  46. }
  47. foreach (explode('.', $key) as $segment) {
  48. if (!is_array($config) || !array_key_exists($segment, $config)) {
  49. return $default;
  50. }
  51. $config = $config[$segment];
  52. }
  53. return $config;
  54. }
  55. /**
  56. * Whether a offset exists.
  57. *
  58. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  59. *
  60. * @param mixed $offset <p>
  61. * An offset to check for.
  62. * </p>
  63. *
  64. * @return bool true on success or false on failure.
  65. * </p>
  66. * <p>
  67. * The return value will be casted to boolean if non-boolean was returned
  68. *
  69. * @since 5.0.0
  70. */
  71. #[\ReturnTypeWillChange]
  72. public function offsetExists($offset)
  73. {
  74. return array_key_exists($offset, $this->config);
  75. }
  76. /**
  77. * Offset to retrieve.
  78. *
  79. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  80. *
  81. * @param mixed $offset <p>
  82. * The offset to retrieve.
  83. * </p>
  84. *
  85. * @return mixed Can return all value types
  86. *
  87. * @since 5.0.0
  88. */
  89. #[\ReturnTypeWillChange]
  90. public function offsetGet($offset)
  91. {
  92. return $this->get($offset);
  93. }
  94. /**
  95. * Offset to set.
  96. *
  97. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  98. *
  99. * @param mixed $offset <p>
  100. * The offset to assign the value to.
  101. * </p>
  102. * @param mixed $value <p>
  103. * The value to set.
  104. * </p>
  105. *
  106. * @since 5.0.0
  107. */
  108. #[\ReturnTypeWillChange]
  109. public function offsetSet($offset, $value)
  110. {
  111. if (isset($this->config[$offset])) {
  112. $this->config[$offset] = $value;
  113. }
  114. }
  115. /**
  116. * Offset to unset.
  117. *
  118. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  119. *
  120. * @param mixed $offset <p>
  121. * The offset to unset.
  122. * </p>
  123. *
  124. * @since 5.0.0
  125. */
  126. #[\ReturnTypeWillChange]
  127. public function offsetUnset($offset)
  128. {
  129. if (isset($this->config[$offset])) {
  130. unset($this->config[$offset]);
  131. }
  132. }
  133. }