Skip to content

Instantly share code, notes, and snippets.

@jpiche
Created February 15, 2013 15:15
Show Gist options
  • Select an option

  • Save jpiche/4960972 to your computer and use it in GitHub Desktop.

Select an option

Save jpiche/4960972 to your computer and use it in GitHub Desktop.
<?php
class Box
{
private $_private_data;
private $_changed_keys = array();
public function __construct(Array $data)
{
$this->_private_data = $data;
}
public function __get($key)
{
if (array_key_exists($key, $this->_private_data)) {
return $this->_private_data[$key];
} else {
return NULL;
}
}
public function asArray($key)
{
if (array_key_exists($key, $this->_private_data)) {
return $this->_private_data[$key];
} else {
return array();
}
}
public function inArray($key, $val)
{
//var_dump('is val `'.$val.'` in array for `'.$key.'`', $val, $key);
if (array_key_exists($key, $this->_private_data)
&& is_array($this->_private_data[$key])
) {
// Code Ignighter DB likes to "help" by making some values into integers
return in_array(is_int($val) ? (string)$val : $val, $this->_private_data[$key], TRUE);
} else {
return FALSE;
}
}
public function __set($name, $value)
{
$this->_private_data[$name] = $value;
$this->_changed_keys[$name] = TRUE;
}
public function getData()
{
return $this->_private_data;
}
public function getChanged()
{
return array_keys($this->_changed_keys);
}
public function merge(Array $new)
{
foreach ($new as $k => $v) {
if ( ! array_key_exists($k, $this->_private_data)) {
if ($v === '' OR $v === FALSE OR $v === NULL) {
continue;
}
$this->__set($k, $v);
} else if ($v !== $this->_private_data[$k]) {
$this->__set($k, $v);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment