Skip to content

Instantly share code, notes, and snippets.

@Herzult
Created September 8, 2011 16:11
Show Gist options
  • Save Herzult/1203779 to your computer and use it in GitHub Desktop.
Save Herzult/1203779 to your computer and use it in GitHub Desktop.
Small array wrapper
<?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