Created
July 19, 2013 14:33
-
-
Save pinoniq/6039523 to your computer and use it in GitHub Desktop.
Simple datamap
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 | |
/** | |
* DataMap | |
*/ | |
class DataMap { | |
/** | |
* This is the actual array that holds the data | |
* | |
* @var Array | |
*/ | |
protected $_map_data = array(); | |
/** | |
* Creates a DataMap opbject | |
*/ | |
public function __construct() {} | |
/** | |
* You can prepopulate the DataMap by calling this method | |
* | |
* @param Array $data | |
*/ | |
public static function fromArray($data) { | |
$m = new DataMap(); | |
$m->addArray($data); | |
return $m; | |
} | |
/** | |
* You can prepopulate the DataMap by calling this method | |
* | |
* @param String<JSON> $data | |
*/ | |
public static function fromJson($data) { | |
return self::fromArray(json_decode($data,true)); | |
} | |
/** | |
* Add an array | |
* | |
* @param Array $data | |
*/ | |
final public function addArray($data) { | |
if ( is_array($data) ) { | |
foreach ( $data as $key => $val ) { | |
$this->set($key,$val); | |
} | |
} | |
} | |
/** | |
* Add a JSON object | |
* | |
* @param Strin<JSON> $data | |
*/ | |
final public function addJson($data) { | |
$this->addArray(json_decode($data,true)); | |
} | |
/** | |
* Adds a key => value pair | |
* If is_array(value) -> DataMap::fromArray | |
* | |
* @param String $key | |
* @param mixed $val | |
*/ | |
public function set($key, $val) { | |
$this->_map_data[strtolower($key)] = (is_array($val)) ? DataMap::fromArray($val) : $val; | |
} | |
/** | |
* Return the value of $key | |
* if !isset($key) create a new DataMat at $key | |
* | |
* @param String $key | |
*/ | |
public function get($key) { | |
if ( !$this->is_set($key) ) { | |
$this->set($key, new DataMap()); | |
} | |
return $this->_map_data[strtolower($key)]; | |
} | |
/** | |
* pseudo for isset | |
* | |
* @param String $key | |
* @return Boolean | |
*/ | |
final public function is_set($key) { | |
return isset($this->_map_data[strtolower($key)]); | |
} | |
/** | |
* pseudo for unset | |
* | |
* @param String $key | |
* @return void | |
*/ | |
final public function un_set($key) { | |
unset($this->_map_data[strtolower($key)]); | |
} | |
/** | |
* pseudo for toJson() | |
* | |
* @return String<JSON> | |
*/ | |
public function toString() { | |
return $this->toJson(); | |
} | |
/** | |
* returns an Array representing this DataMap | |
* !recursion! | |
* | |
* @return Array | |
*/ | |
public function toArray() { | |
$a = array(); | |
foreach( $this->_map_data as $key => $value ) { | |
if ( $value instanceof DataMap ) { | |
$a[$key] = $value->toArray(); | |
} else { | |
$a[$key] = $value; | |
} | |
} | |
return $a; | |
} | |
/** | |
* returns a JSON string representing this DataMap | |
* | |
* @return String<JSON> | |
*/ | |
public function toJson() { | |
return json_encode($this->toArray()); | |
} | |
/** | |
* returns an array of keys | |
* | |
* @return Array | |
*/ | |
final public function keys() { | |
return array_keys($this->_map_data); | |
} | |
/** | |
* returns the number of keys | |
* | |
* @return int | |
*/ | |
final public function length() { | |
return count($this->_map_data); | |
} | |
/** | |
* PHsP magic methods | |
*/ | |
final public function __set($key, $val) { | |
return $this->set($key,$val); | |
} | |
final public function __get($key) { | |
return $this->get($key); | |
} | |
final public function __isset($key) { | |
return $this->is_set($key); | |
} | |
final public function __unset($key) { | |
$this->delete($key); | |
} | |
final public function __toString() { | |
return $this->toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment