Config.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  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\Socialite;
  11. use ArrayAccess;
  12. use InvalidArgumentException;
  13. /**
  14. * Class Config.
  15. */
  16. class Config implements ArrayAccess
  17. {
  18. /**
  19. * @var array
  20. */
  21. protected $config;
  22. /**
  23. * Config constructor.
  24. *
  25. * @param array $config
  26. */
  27. public function __construct(array $config)
  28. {
  29. $this->config = $config;
  30. }
  31. /**
  32. * Get an item from an array using "dot" notation.
  33. *
  34. * @param string $key
  35. * @param mixed $default
  36. *
  37. * @return mixed
  38. */
  39. public function get($key, $default = null)
  40. {
  41. $config = $this->config;
  42. if (is_null($key)) {
  43. return $config;
  44. }
  45. if (isset($config[$key])) {
  46. return $config[$key];
  47. }
  48. foreach (explode('.', $key) as $segment) {
  49. if (!is_array($config) || !array_key_exists($segment, $config)) {
  50. return $default;
  51. }
  52. $config = $config[$segment];
  53. }
  54. return $config;
  55. }
  56. /**
  57. * Set an array item to a given value using "dot" notation.
  58. *
  59. * @param string $key
  60. * @param mixed $value
  61. *
  62. * @return array
  63. */
  64. public function set($key, $value)
  65. {
  66. if (is_null($key)) {
  67. throw new InvalidArgumentException('Invalid config key.');
  68. }
  69. $keys = explode('.', $key);
  70. $config = &$this->config;
  71. while (count($keys) > 1) {
  72. $key = array_shift($keys);
  73. if (!isset($config[$key]) || !is_array($config[$key])) {
  74. $config[$key] = [];
  75. }
  76. $config = &$config[$key];
  77. }
  78. $config[array_shift($keys)] = $value;
  79. return $config;
  80. }
  81. /**
  82. * Determine if the given configuration value exists.
  83. *
  84. * @param string $key
  85. *
  86. * @return bool
  87. */
  88. public function has($key)
  89. {
  90. return (bool) $this->get($key);
  91. }
  92. /**
  93. * Whether a offset exists.
  94. *
  95. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  96. *
  97. * @param mixed $offset <p>
  98. * An offset to check for.
  99. * </p>
  100. *
  101. * @return bool true on success or false on failure.
  102. * </p>
  103. * <p>
  104. * The return value will be casted to boolean if non-boolean was returned
  105. *
  106. * @since 5.0.0
  107. */
  108. #[\ReturnTypeWillChange]
  109. public function offsetExists($offset)
  110. {
  111. return array_key_exists($offset, $this->config);
  112. }
  113. /**
  114. * Offset to retrieve.
  115. *
  116. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  117. *
  118. * @param mixed $offset <p>
  119. * The offset to retrieve.
  120. * </p>
  121. *
  122. * @return mixed Can return all value types
  123. *
  124. * @since 5.0.0
  125. */
  126. #[\ReturnTypeWillChange]
  127. public function offsetGet($offset)
  128. {
  129. return $this->get($offset);
  130. }
  131. /**
  132. * Offset to set.
  133. *
  134. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  135. *
  136. * @param mixed $offset <p>
  137. * The offset to assign the value to.
  138. * </p>
  139. * @param mixed $value <p>
  140. * The value to set.
  141. * </p>
  142. *
  143. * @since 5.0.0
  144. */
  145. #[\ReturnTypeWillChange]
  146. public function offsetSet($offset, $value)
  147. {
  148. $this->set($offset, $value);
  149. }
  150. /**
  151. * Offset to unset.
  152. *
  153. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  154. *
  155. * @param mixed $offset <p>
  156. * The offset to unset.
  157. * </p>
  158. *
  159. * @since 5.0.0
  160. */
  161. #[\ReturnTypeWillChange]
  162. public function offsetUnset($offset)
  163. {
  164. $this->set($offset, null);
  165. }
  166. }