| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace Overtrue\Socialite;
- use ArrayAccess;
- use InvalidArgumentException;
- class Config implements ArrayAccess, \JsonSerializable
- {
- /**
- * @var array
- */
- protected array $config;
- /**
- * @param array $config
- */
- public function __construct(array $config)
- {
- $this->config = $config;
- }
- /**
- * @param string $key
- * @param mixed $default
- *
- * @return mixed
- */
- public function get(string $key, $default = null)
- {
- $config = $this->config;
- if (is_null($key)) {
- return $config;
- }
- if (isset($config[$key])) {
- return $config[$key];
- }
- foreach (explode('.', $key) as $segment) {
- if (!is_array($config) || !array_key_exists($segment, $config)) {
- return $default;
- }
- $config = $config[$segment];
- }
- return $config;
- }
- /**
- * @param string $key
- * @param mixed $value
- *
- * @return array
- */
- public function set(string $key, $value)
- {
- if (is_null($key)) {
- throw new InvalidArgumentException('Invalid config key.');
- }
- $keys = explode('.', $key);
- $config = &$this->config;
- while (count($keys) > 1) {
- $key = array_shift($keys);
- if (!isset($config[$key]) || !is_array($config[$key])) {
- $config[$key] = [];
- }
- $config = &$config[$key];
- }
- $config[array_shift($keys)] = $value;
- return $config;
- }
- /**
- * @param string $key
- *
- * @return bool
- */
- public function has(string $key): bool
- {
- return (bool) $this->get($key);
- }
- public function offsetExists($offset)
- {
- return array_key_exists($offset, $this->config);
- }
- public function offsetGet($offset)
- {
- return $this->get($offset);
- }
- public function offsetSet($offset, $value)
- {
- $this->set($offset, $value);
- }
- public function offsetUnset($offset)
- {
- $this->set($offset, null);
- }
- public function jsonSerialize()
- {
- return $this->config;
- }
- public function __toString()
- {
- return \json_encode($this, \JSON_UNESCAPED_UNICODE);
- }
- }
|