/** * Converts a response from the Maestrano API to the corresponding PHP object. * * @param array $resp The response from the Maestrano API. * @param string $apiToken * @return Maestrano_Api_Object|array */ public static function convertToMaestranoObject($resp, $apiToken) { $types = array('account_bill' => 'Maestrano_Account_Bill', 'account_recurring_bill' => 'Maestrano_Account_RecurringBill', 'account_group' => 'Maestrano_Account_Group', 'account_user' => 'Maestrano_Account_User'); if (self::isList($resp)) { $mapped = array(); foreach ($resp as $i) { array_push($mapped, self::convertToMaestranoObject($i, $apiToken)); } return $mapped; } else { if (is_array($resp)) { if (isset($resp['object']) && is_string($resp['object']) && isset($types[$resp['object']])) { $class = $types[$resp['object']]; } else { $class = 'Maestrano_Api_Object'; } return Maestrano_Api_Object::scopedConstructFrom($class, $resp, $apiToken); } else { // Automatically convert dates if (preg_match('/\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}/', $resp)) { return new DateTime($resp); } else { return $resp; } } } }
/** * Refreshes this object using the provided values. * * @param array $values * @param string $preset * @param boolean $partial Defaults to false. */ public function refreshFrom($values, $preset = null, $partial = false) { $this->_preset = $preset; // Wipe old state before setting new. This is useful for e.g. updating a // customer, where there is no persistent card parameter. Mark those values // which don't persist as transient if ($partial) { $removed = new Maestrano_Util_Set(); } else { $removed = array_diff(array_keys($this->_values), array_keys($values)); } foreach ($removed as $k) { if (self::$permanentAttributes->includes($k)) { continue; } unset($this->{$k}); } foreach ($values as $k => $v) { if (self::$permanentAttributes->includes($k) && isset($this[$k])) { continue; } if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) { $this->_values[$k] = Maestrano_Api_Object::scopedConstructFrom('Maestrano_AttachedObject', $v, $preset); } else { $this->_values[$k] = Maestrano_Api_Util::convertToMaestranoObject($v, $preset); } $this->_transientValues->discard($k); $this->_unsavedValues->discard($k); } }