| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Config; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class Config implements \ArrayAccess |
| 8 |
{ |
| 9 |
/** |
| 10 |
* All array items from all files from /config directory |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
protected $repository = array(); |
| 14 |
|
| 15 |
/** |
| 16 |
* Initiate the instance |
| 17 |
* @param array $repository |
| 18 |
*/ |
| 19 |
public function __construct($repository = array()) |
| 20 |
{ |
| 21 |
$this->repository = $repository; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Determine if the given configuration value exists. |
| 26 |
* |
| 27 |
* @param string $key |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function has($key) |
| 31 |
{ |
| 32 |
return ArrayHelper::has($this->repository, $key); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get all of the configuration items for the application. |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public function all() |
| 41 |
{ |
| 42 |
return $this->repository; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the specified configuration value. |
| 47 |
* |
| 48 |
* @param array|string $key |
| 49 |
* @param mixed $default |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function get($key = null, $default = null) |
| 53 |
{ |
| 54 |
return ArrayHelper::get($this->repository, $key, $default); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Set a given configuration value. |
| 59 |
* |
| 60 |
* @param array|string $key |
| 61 |
* @param mixed $value |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function set($key, $value) |
| 65 |
{ |
| 66 |
$keys = is_array($key) ? $key : [$key => $value]; |
| 67 |
|
| 68 |
foreach ($keys as $key => $value) { |
| 69 |
return ArrayHelper::set($this->repository, $key, $value); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Dynamic Getter |
| 75 |
* @param string $key |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public function __get($key = null) |
| 79 |
{ |
| 80 |
return $this->get($key); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Dynamic Setter |
| 85 |
* @param string $key |
| 86 |
* @param mixed $key |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function __set($key, $value) |
| 90 |
{ |
| 91 |
return $this->set($key, $value); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Determine if the given item exists. |
| 96 |
* |
| 97 |
* @param string $key |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function offsetExists($key) |
| 101 |
{ |
| 102 |
return $this->has($key); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get an item. |
| 107 |
* |
| 108 |
* @param string $key |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public function offsetGet($key) |
| 112 |
{ |
| 113 |
return $this->get($key); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Set an item. |
| 118 |
* |
| 119 |
* @param string $key |
| 120 |
* @param mixed $value |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function offsetSet($key, $value) |
| 124 |
{ |
| 125 |
$this->set($key, $value); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Unset an item. |
| 130 |
* |
| 131 |
* @param string $key |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function offsetUnset($key) |
| 135 |
{ |
| 136 |
$this->set($key, null); |
| 137 |
} |
| 138 |
} |