Created
June 13, 2011 16:59
-
-
Save samkeen/1023184 to your computer and use it in GitHub Desktop.
Generic data object with alias sync
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 | |
/** | |
* AliasSyncDataObject | |
* This is a data object that allows models to be marshaled from Doctrine to Si | |
* and back again. | |
* It is mostly just coordinating field alias names and keeping their values in sync. | |
*/ | |
class AliasSyncDataObject { | |
private $doctrine_record_name = null; | |
// array(Doctrine_name => Si_name, ...) | |
private $attribute_alias_mappings = array(); | |
private $attribute_names = array(); | |
private $attribute_alias_names = array(); | |
// array('attribute1_name' => value, ...) | |
private $attribute_value_map = array(); | |
public function __construct(Doctrine_record $doctrine_record) | |
{ | |
$this->doctrine_record_name = get_class($doctrine_record); | |
$this->attribute_value_map = $doctrine_record->toArray(); | |
$this->attribute_names = array_keys($this->attribute_value_map); | |
$this->attribute_alias_mappings = $doctrine_record->si_field_alias_mappings(); | |
$this->attribute_alias_names = array_values($this->attribute_alias_mappings); | |
} | |
public function __get($attribute_name) | |
{ | |
$this->verify_attribute_exists($attribute_name); | |
return $this->attribute_value_map[$this->get_attribute_name($attribute_name)]; | |
} | |
public function __set($attribute_name, $value) | |
{ | |
$this->verify_attribute_exists($attribute_name); | |
$this->set_attribute($this->get_attribute_and_alias($attribute_name), $value); | |
} | |
public function add_attribute($attribute_name, $attribute_value) | |
{ | |
$this->attribute_value_map[$attribute_name] = $attribute_value; | |
array_push($this->attribute_names, $attribute_name); | |
} | |
/** | |
* Sets the value provided to this attribute. Also takes aliasses into account. | |
* If one exists, it assures that the attribute and its aliases values are kept in sync . | |
* | |
* @param array $attribute_and_alias_names Not all attributes have aliases so it is | |
* possible that there is no alias and this is just an array with the attribute name only. | |
* @param $value | |
* @return void | |
*/ | |
private function set_attribute(array $attribute_and_alias_names, $value) | |
{ | |
foreach($attribute_and_alias_names as $name ) | |
{ | |
$this->attribute_value_map[$name]=$value; | |
} | |
} | |
/** | |
* @throws InvalidArgumentException | |
* @param $attribute_name | |
* @return string | |
*/ | |
private function verify_attribute_exists($attribute_name) | |
{ | |
if( ! $this->is_an_attribute($attribute_name) | |
&& | |
! $this->is_an_attribute_alias($attribute_name)) | |
{ | |
throw new InvalidArgumentException("Proxy Model {$this->doctrine_record_name}" | |
." does NOT have attribute {$attribute_name} defined"); | |
} | |
} | |
private function get_attribute_and_alias($attribute_name) | |
{ | |
$attribute_and_alias = array($attribute_name); | |
if($this->is_an_attribute_alias($attribute_name)) | |
{ | |
$attribute_and_alias[] = $this->attribute_name_for_alias($attribute_name); | |
} | |
else if($this->attribute_has_an_alias($attribute_name)) | |
{ | |
$attribute_and_alias[] = $this->alias_name_for_attribute($attribute_name); | |
} | |
return $attribute_and_alias; | |
} | |
private function get_attribute_name($name) | |
{ | |
if($this->is_an_attribute_alias($name)) | |
{ | |
return $this->attribute_name_for_alias($name); | |
} | |
return $name; | |
} | |
private function is_an_attribute_alias($attribute_name) | |
{ | |
return in_array($attribute_name, $this->attribute_alias_names); | |
} | |
private function attribute_has_an_alias($attribute_name) | |
{ | |
return array_key_exists($attribute_name, $this->attribute_alias_mappings); | |
} | |
private function is_an_attribute($attribute_name) | |
{ | |
return in_array($attribute_name, $this->attribute_names); | |
} | |
private function attribute_name_for_alias($alias_name) | |
{ | |
return array_search($alias_name, $this->attribute_alias_mappings); | |
} | |
private function alias_name_for_attribute($attribute_name) | |
{ | |
return $this->attribute_alias_mappings[$attribute_name]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment