Config.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. public function offsetExists($offset)
  109. {
  110. return array_key_exists($offset, $this->config);
  111. }
  112. /**
  113. * Offset to retrieve.
  114. *
  115. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  116. *
  117. * @param mixed $offset <p>
  118. * The offset to retrieve.
  119. * </p>
  120. *
  121. * @return mixed Can return all value types
  122. *
  123. * @since 5.0.0
  124. */
  125. public function offsetGet($offset)
  126. {
  127. return $this->get($offset);
  128. }
  129. /**
  130. * Offset to set.
  131. *
  132. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  133. *
  134. * @param mixed $offset <p>
  135. * The offset to assign the value to.
  136. * </p>
  137. * @param mixed $value <p>
  138. * The value to set.
  139. * </p>
  140. *
  141. * @since 5.0.0
  142. */
  143. public function offsetSet($offset, $value)
  144. {
  145. $this->set($offset, $value);
  146. }
  147. /**
  148. * Offset to unset.
  149. *
  150. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  151. *
  152. * @param mixed $offset <p>
  153. * The offset to unset.
  154. * </p>
  155. *
  156. * @since 5.0.0
  157. */
  158. public function offsetUnset($offset)
  159. {
  160. $this->set($offset, null);
  161. }
  162. }