Config.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. while (count($keys) > 1) {
  71. $key = array_shift($keys);
  72. if (!isset($this->config[$key]) || !is_array($this->config[$key])) {
  73. $this->config[$key] = [];
  74. }
  75. $this->config = &$this->config[$key];
  76. }
  77. $this->config[array_shift($keys)] = $value;
  78. return $this->config;
  79. }
  80. /**
  81. * Determine if the given configuration value exists.
  82. *
  83. * @param string $key
  84. *
  85. * @return bool
  86. */
  87. public function has($key)
  88. {
  89. return (bool) $this->get($key);
  90. }
  91. /**
  92. * Whether a offset exists.
  93. *
  94. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  95. *
  96. * @param mixed $offset <p>
  97. * An offset to check for.
  98. * </p>
  99. *
  100. * @return bool true on success or false on failure.
  101. * </p>
  102. * <p>
  103. * The return value will be casted to boolean if non-boolean was returned
  104. *
  105. * @since 5.0.0
  106. */
  107. public function offsetExists($offset)
  108. {
  109. return array_key_exists($offset, $this->config);
  110. }
  111. /**
  112. * Offset to retrieve.
  113. *
  114. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  115. *
  116. * @param mixed $offset <p>
  117. * The offset to retrieve.
  118. * </p>
  119. *
  120. * @return mixed Can return all value types
  121. *
  122. * @since 5.0.0
  123. */
  124. public function offsetGet($offset)
  125. {
  126. return $this->get($offset);
  127. }
  128. /**
  129. * Offset to set.
  130. *
  131. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  132. *
  133. * @param mixed $offset <p>
  134. * The offset to assign the value to.
  135. * </p>
  136. * @param mixed $value <p>
  137. * The value to set.
  138. * </p>
  139. *
  140. * @since 5.0.0
  141. */
  142. public function offsetSet($offset, $value)
  143. {
  144. $this->set($offset, $value);
  145. }
  146. /**
  147. * Offset to unset.
  148. *
  149. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  150. *
  151. * @param mixed $offset <p>
  152. * The offset to unset.
  153. * </p>
  154. *
  155. * @since 5.0.0
  156. */
  157. public function offsetUnset($offset)
  158. {
  159. $this->set($offset, null);
  160. }
  161. }