: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @author Jesus A. Domingo <jesus.domingo@gmail.com>
* @author Hassan Khan <contact@hassankhan.me>
* @link https://github.com/noodlehaus/config
abstract class AbstractConfig implements ArrayAccess, ConfigInterface, Iterator
* Stores the configuration data
* Caches the configuration data
* Constructor method and sets default options, if any
public function __construct(array $data)
$this->data = array_merge($this->getDefaults(), $data);
* Override this method in your own subclass to provide an array of default
protected function getDefaults()
* ConfigInterface Methods
public function get($key, $default = null)
return $this->cache[$key];
public function set($key, $value)
$segs = explode('.', $key);
// Look for the key, creating nested keys if needed
while ($part = array_shift($segs)) {
if (!isset($root[$part]) && count($segs)) {
//Unset all old nested cache
if (isset($this->cache[$cacheKey])) {
unset($this->cache[$cacheKey]);
//Unset all old nested cache in case of array
foreach ($this->cache as $cacheLocalKey => $cacheValue) {
if (substr($cacheLocalKey, 0, strlen($cacheKey)) === $cacheKey) {
unset($this->cache[$cacheLocalKey]);
// Assign value at target node
$this->cache[$key] = $root = $value;
public function has($key)
// Check if already cached
if (isset($this->cache[$key])) {
$segments = explode('.', $key);
foreach ($segments as $segment) {
if (array_key_exists($segment, $root)) {
// Set cache for the given key
$this->cache[$key] = $root;
* Merge config from another instance
* @param ConfigInterface $config
* @return ConfigInterface
public function merge(ConfigInterface $config)
$this->data = array_replace_recursive($this->data, $config->all());
* Gets a value using the offset as a key
public function offsetGet($offset)
return $this->get($offset);
public function offsetExists($offset)
return $this->has($offset);
* Sets a value using the offset as a key
public function offsetSet($offset, $value)
$this->set($offset, $value);
* Deletes a key and its value
public function offsetUnset($offset)
$this->set($offset, null);
* Returns the data array element referenced by its internal cursor
* @return mixed The element referenced by the data array's internal cursor.
* If the array is empty or there is no element at the cursor, the
* function returns false. If the array is undefined, the function
public function current()
return (is_array($this->data) ? current($this->data) : null);
* Returns the data array index referenced by its internal cursor
* @return mixed The index referenced by the data array's internal cursor.
* If the array is empty or undefined or there is no element at the
* cursor, the function returns null
return (is_array($this->data) ? key($this->data) : null);
* Moves the data array's internal cursor forward one element
* @return mixed The element referenced by the data array's internal cursor
* after the move is completed. If there are no more elements in the
* array after the move, the function returns false. If the data array
* is undefined, the function returns null
return (is_array($this->data) ? next($this->data) : null);
* Moves the data array's internal cursor to the first element
* @return mixed The element referenced by the data array's internal cursor
* after the move is completed. If the data array is empty, the function
* returns false. If the data array is undefined, the function returns
return (is_array($this->data) ? reset($this->data) : null);
* Tests whether the iterator's current index is valid
* @return bool True if the current index is valid; false otherwise
return (is_array($this->data) ? key($this->data) !== null : false);
* Remove a value using the offset as a key
public function remove($key)
$this->offsetUnset($key);