Repository.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Ollyo\PaymentHub\Config; |
| 4 | |
| 5 | use ArrayAccess; |
| 6 | use Ollyo\PaymentHub\Contracts\Config\RepositoryContract; |
| 7 | |
| 8 | class Repository implements ArrayAccess, RepositoryContract |
| 9 | { |
| 10 | /** |
| 11 | * All the configuration items |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $items = []; |
| 16 | |
| 17 | public function __construct(array $items = []) |
| 18 | { |
| 19 | $this->items = $items; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Determine if a key exists in the repository |
| 24 | * |
| 25 | * @param mixed $key |
| 26 | * @return boolean |
| 27 | */ |
| 28 | public function has($key): bool |
| 29 | { |
| 30 | return isset($this->items[$key]); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the value from the repository using a key, if not found then return the default value. |
| 35 | * |
| 36 | * @param mixed $key |
| 37 | * @param mixed $default |
| 38 | * @return mixed |
| 39 | */ |
| 40 | public function get($key, $default = null) |
| 41 | { |
| 42 | if (is_array($key)) { |
| 43 | return $this->getMany($key); |
| 44 | } |
| 45 | |
| 46 | if (!$this->has($key)) { |
| 47 | return $default; |
| 48 | } |
| 49 | |
| 50 | return $this->items[$key]; |
| 51 | } |
| 52 | |
| 53 | public function getMany(array $keys) |
| 54 | { |
| 55 | $config = []; |
| 56 | |
| 57 | foreach ($keys as $key => $default) { |
| 58 | if (is_numeric($key)) { |
| 59 | [$key, $default] = [$default, null]; |
| 60 | } |
| 61 | |
| 62 | $config[$key] = $this->get($key, $default); |
| 63 | } |
| 64 | |
| 65 | return $config; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set a value to the repository by the key. This is for updating an existing value or add new. |
| 70 | * |
| 71 | * @param mixed $key |
| 72 | * @param mixed $value |
| 73 | * @return void |
| 74 | */ |
| 75 | public function set($key, $value = null) |
| 76 | { |
| 77 | $keys = is_array($key) ? $key: [$key => $value]; |
| 78 | |
| 79 | foreach ($keys as $key => $value) { |
| 80 | $this->items[$key] = $value; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get all the values |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public function all() |
| 90 | { |
| 91 | return $this->items; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Prepend to the repository |
| 96 | * |
| 97 | * @param mixed $key |
| 98 | * @param mixed $value |
| 99 | * @return void |
| 100 | */ |
| 101 | public function prepend($key, $value) |
| 102 | { |
| 103 | $array = $this->get($key, []); |
| 104 | |
| 105 | array_unshift($array, $value); |
| 106 | |
| 107 | $this->set($key, $array); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Append to the repository |
| 112 | * |
| 113 | * @param mixed $key |
| 114 | * @param mixed $value |
| 115 | * @return void |
| 116 | */ |
| 117 | public function push($key, $value) |
| 118 | { |
| 119 | $array = $this->get($key, []); |
| 120 | |
| 121 | array_push($array, $value); |
| 122 | |
| 123 | $this->set($key, $array); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check if the configuration option exists. |
| 128 | * |
| 129 | * @param string $key |
| 130 | * @return bool |
| 131 | */ |
| 132 | public function offsetExists($key): bool |
| 133 | { |
| 134 | return $this->has($key); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the value by the key |
| 139 | * |
| 140 | * @param string $key |
| 141 | * @return mixed |
| 142 | */ |
| 143 | public function offsetGet($key): mixed |
| 144 | { |
| 145 | return $this->get($key); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set value by offset key |
| 150 | * |
| 151 | * @param string $key |
| 152 | * @param mixed $value |
| 153 | * @return void |
| 154 | */ |
| 155 | public function offsetSet($key, $value): void |
| 156 | { |
| 157 | $this->set($key, $value); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Unset a value by setting it by null |
| 162 | * |
| 163 | * @param string $key |
| 164 | * @return void |
| 165 | */ |
| 166 | public function offsetUnset($key): void |
| 167 | { |
| 168 | $this->set($key, null); |
| 169 | } |
| 170 | } |