/**
* Load an assoc array into the instance of the object $property => $value
* $replace_data - replace existing data
*
* @param $input_array
* @param $replace_data
*/
public function fromStringArray($input_array, $replace_data = false)
{
foreach (static::getProperties() as $property => $meta) {
$type = $meta[self::KEY_TYPE];
$php_type = $meta[self::KEY_PHP_TYPE];
//If set and NOT replace data, continue
if (!$replace_data && isset($this->_data[$property])) {
continue;
}
if (!isset($input_array[$property])) {
$this->_data[$property] = null;
continue;
}
//Fix for an earlier assumption that the API didn't return more than
//two levels of nested objects.
//Handles Invoice > Contact > Address etc. in one build.
if (is_array($input_array[$property]) && Helpers::isAssoc($input_array[$property]) === false) {
$collection = new Collection();
$collection->addAssociatedObject($property, $this);
foreach ($input_array[$property] as $assoc_element) {
$cast = self::castFromString($type, $assoc_element, $php_type);
//Do this here so that you know it's not a static method call to ::castFromString
if ($cast instanceof Object) {
$cast->addAssociatedObject($property, $this);
}
$collection->append($cast);
}
$this->_data[$property] = $collection;
} else {
$cast = self::castFromString($type, $input_array[$property], $php_type);
//Do this here so that you know it's not a static method call to ::castFromString
if ($cast instanceof Object) {
$cast->addAssociatedObject($property, $this);
}
$this->_data[$property] = $cast;
}
}
}