Last active
August 29, 2015 14:01
-
-
Save dhrrgn/90cf954b74aba7b63b22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Slightly modified array helpers from Laravel. | |
* | |
* Changes: Uses '/' as a key seperator instead of '.' so that keys can have dots. | |
*/ | |
function array_get($array, $key, $default = null) | |
{ | |
if (is_null($key)) { | |
return $array; | |
} | |
if (isset($array[$key])) { | |
return $array[$key]; | |
} | |
foreach (explode('/', $key) as $segment) { | |
if ( ! is_array($array) || ! array_key_exists($segment, $array)) { | |
return $default; | |
} | |
$array = $array[$segment]; | |
} | |
return $array; | |
} | |
function array_set(&$array, $key, $value) | |
{ | |
if (is_null($key)) { | |
return $array = $value; | |
} | |
$keys = explode('/', $key); | |
while (count($keys) > 1) { | |
$key = array_shift($keys); | |
// If the key doesn't exist at this depth, we will just create an empty array | |
// to hold the next value, allowing us to create the arrays to hold final | |
// values at the correct depth. Then we'll keep digging into the array. | |
if ( ! isset($array[$key]) || ! is_array($array[$key])) { | |
$array[$key] = array(); | |
} | |
$array =& $array[$key]; | |
} | |
$array[array_shift($keys)] = $value; | |
return $array; | |
} | |
function array_forget(&$array, $key) | |
{ | |
$keys = explode('/', $key); | |
while (count($keys) > 1) { | |
$key = array_shift($keys); | |
if ( ! isset($array[$key]) || ! is_array($array[$key])) { | |
return; | |
} | |
$array =& $array[$key]; | |
} | |
unset($array[array_shift($keys)]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use ArrayIterator; | |
/** | |
* Implements ArrayAccess, Countable, IteratorAggregate | |
*/ | |
trait ParameterBagTrait | |
{ | |
/** | |
* @var array | |
*/ | |
protected $parameters = []; | |
/** | |
* Checks if the bag has the given key. | |
* @param string $key The key to check. | |
* @return boolean | |
*/ | |
public function has($key) | |
{ | |
$default = microtime(true); | |
return $this->get($key, $default) !== $default; | |
} | |
/** | |
* Gets the given key from the bag, or returns the default if it does not | |
* exist. | |
* @param string $key The key to get. | |
* @param mixed $default Default value to return. | |
* @return mixed | |
*/ | |
public function get($key, $default = null) | |
{ | |
$value = array_get($this->parameters, $key, $default); | |
return ($value instanceof Closure) ? $value() : $value; | |
} | |
/** | |
* Sets the given key in the bag. | |
* @param string $key The key to set | |
* @param mixed $value The value. | |
*/ | |
public function set($key, $value) | |
{ | |
return array_set($this->parameters, $key, $value); | |
} | |
/** | |
* Forgets (removes) the given key from the bag. | |
* @param string $key The key to forget. | |
* @return boolean | |
*/ | |
public function forget($key) | |
{ | |
return array_forget($this->parameters, $key); | |
} | |
/** | |
* Magic method to allow object-type semantics for the bag. | |
* @param string $key The key to get. | |
* @return mixed | |
*/ | |
public function __get($key) | |
{ | |
return $this->get($key); | |
} | |
/** | |
* Magic method to allow object-type semantics for the bag. | |
* @param string $key The key to set. | |
* @param mixed $value Value to set | |
* @return mixed | |
*/ | |
public function __set($key, $value) | |
{ | |
return $this->set($key, $value); | |
} | |
/** | |
* IteratorAggregate: Gets an Iterator for the bag. | |
* @return \ArrayIterator | |
*/ | |
public function getIterator() | |
{ | |
return new ArrayIterator($this->parameters); | |
} | |
/** | |
* Countable: Gets the number of items in the bag. | |
* @return int | |
*/ | |
public function count() | |
{ | |
return count($this->parameters); | |
} | |
/** | |
* ArrayAccess: Checks if the key exists. | |
* @param string $key The key to check. | |
* @return boolean | |
*/ | |
public function offsetExists($key) | |
{ | |
return $this->has($key); | |
} | |
/** | |
* ArrayAccess: Unsets the given key. | |
* @param string $key The key to unset. | |
* @return boolean | |
*/ | |
public function offsetUnset($key) | |
{ | |
$this->forget($key); | |
} | |
/** | |
* ArrayAccess: Gets the given key. | |
* @param string $key The key to get. | |
* @return mixed | |
*/ | |
public function offsetGet($key) | |
{ | |
return $this->get($key); | |
} | |
/** | |
* ArrayAccess: Sets the given offset. | |
* @param string $key The key to set. | |
* @param mixed $value The value to set. | |
* @return void | |
*/ | |
public function offsetSet($key, $value) | |
{ | |
$this->set($key, $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment