JustSteveKing /
config
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace JustSteveKing\Config; |
||
| 6 | |||
| 7 | use ArrayAccess; |
||
| 8 | use JustSteveKing\Config\Support\Dig; |
||
| 9 | |||
| 10 | class Repository implements ArrayAccess |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Repository Constructor |
||
| 14 | * |
||
| 15 | * @param array $items |
||
| 16 | */ |
||
| 17 | private function __construct( |
||
| 18 | protected array $items = [] |
||
| 19 | ) { |
||
| 20 | $this->items = $items; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Build a new Config Repository |
||
| 25 | * |
||
| 26 | * @param array $items |
||
| 27 | * @return self |
||
| 28 | */ |
||
| 29 | public static function build(array $items): self |
||
| 30 | { |
||
| 31 | return new self( |
||
| 32 | items: $items, |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Does this key exist in the items array |
||
| 38 | * |
||
| 39 | * @param string $keys |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | public function has(string $keys): bool |
||
| 43 | { |
||
| 44 | $keys = (array) $keys; |
||
| 45 | |||
| 46 | if ( empty($this->items) || empty($keys)) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | foreach ($keys as $key) { |
||
| 51 | $subKeyArray = $this->items; |
||
| 52 | |||
| 53 | if (Dig::exists($this->items, $key)) { |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | foreach (explode('.', $key) as $segment) { |
||
| 58 | if (Dig::accessible($subKeyArray) && Dig::exists($subKeyArray, $segment)) { |
||
| 59 | $subKeyArray = $subKeyArray[$segment]; |
||
| 60 | } else { |
||
| 61 | return false; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return true; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get a specific config value |
||
| 71 | * |
||
| 72 | * @param array|string $key |
||
| 73 | * @param mixed $value |
||
| 74 | * @return array|mixed |
||
| 75 | */ |
||
| 76 | public function get($key, $value = null): mixed |
||
| 77 | { |
||
| 78 | if (is_array($key)) { |
||
| 79 | return $this->getMany($key); |
||
| 80 | } |
||
| 81 | |||
| 82 | return Dig::get($this->items, $key, $value); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get many configuration values. |
||
| 87 | * |
||
| 88 | * @param array $keys |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getMany(array $keys): array |
||
| 92 | { |
||
| 93 | $config = []; |
||
| 94 | |||
| 95 | foreach ($keys as $key => $value) { |
||
| 96 | if (is_numeric($key)) { |
||
| 97 | [$key, $value] = [$value, null]; |
||
| 98 | } |
||
| 99 | |||
| 100 | $config[$key] = Dig::get($this->items, $key, $value); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $config; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set a given configuration value. |
||
| 108 | * |
||
| 109 | * @param array|string $key |
||
| 110 | * @param mixed $value |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function set($key, $value = null): void |
||
| 114 | { |
||
| 115 | $keys = is_array($key) ? $key : [$key => $value]; |
||
| 116 | |||
| 117 | foreach ($keys as $item => $value) { |
||
| 118 | Dig::set($this->items, $item, $value); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get all of the configuration items for the application. |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function all(): array |
||
| 128 | { |
||
| 129 | return $this->items; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Determine if the given configuration option exists. |
||
| 134 | * |
||
| 135 | * @param string $key |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function offsetExists($key) |
||
| 139 | { |
||
| 140 | return $this->has($key); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get a configuration option. |
||
| 145 | * |
||
| 146 | * @param string $key |
||
| 147 | * @return array|mixed |
||
| 148 | */ |
||
| 149 | public function offsetGet($key) |
||
| 150 | { |
||
| 151 | return $this->get($key); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set a configuration option. |
||
| 156 | * |
||
| 157 | * @param string $key |
||
| 158 | * @param mixed $value |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function offsetSet($key, $value) |
||
| 163 | { |
||
| 164 | $this->set($key, $value); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Unset a configuration option. |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | public function offsetUnset($key) |
||
| 174 | { |
||
| 175 | $this->set($key, null); |
||
| 176 | } |
||
| 177 | } |
||
| 178 |