Created
September 8, 2011 16:11
-
-
Save Herzult/1203779 to your computer and use it in GitHub Desktop.
Small array wrapper
This file contains 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 | |
class ArrayWrapper | |
{ | |
private $array; | |
/** | |
* Constructor | |
* | |
* @param array $array | |
*/ | |
public function __construct(array $array) | |
{ | |
$this->array = $array; | |
} | |
/** | |
* Returns the value at the specified path or the default value when it | |
* does not exist | |
* | |
* @param array $path | |
* @param mixed $default | |
* | |
* @return mixed | |
*/ | |
public function get($path, $default = null) | |
{ | |
$data = $this->array; | |
foreach (explode('.', $path) as $part) { | |
if (array_key_exists($part, $data)) { | |
$data = $data[$part]; | |
} else { | |
return $default; | |
} | |
} | |
return $data; | |
} | |
/** | |
* Indicates whether the specified path exists | |
* | |
* @param array $path | |
* | |
* @return Boolean | |
*/ | |
public function has($path) | |
{ | |
$data = $this->array; | |
foreach (explode('.', $path) as $part) { | |
if (array_key_exists($part, $data)) { | |
$data = $data[$part]; | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment