function cast($val, $type) { $return = ''; switch ($type) { case 'int': $return = (int) $val; break; case 'boolean': $return = is_true_val($val); break; case 'array': $return = is_serialized_str($val) ? unserialize($val) : array(); break; default: $return = $val; } return $return; }
/** * Retrieves an archived value * * @access public * @param int The record ID associated with the archive * @param int The version of the archive to retrieve (optional) * @param boolean Determines whether to return all of the archives fields or just the data field value (optional) * @return array */ public function get_archive($ref_id, $version = NULL, $all_data = FALSE) { $CI =& get_instance(); $CI->load->module_model(FUEL_FOLDER, 'fuel_archives_model'); $CI->load->helper('date'); // best to use ref_id and version because it is more secure $where = array('table_name' => $this->table_name, 'ref_id' => $ref_id, 'version' => $version); $archive = $CI->fuel_archives_model->find_one_array($where); $return = $archive; $return['data'] = array(); if (!empty($archive)) { // check for serialization for backwards compatibility $data = is_serialized_str($archive['data']) ? @unserialize($archive['data']) : json_decode($archive['data'], TRUE); if (!empty($data) and is_array($data)) { foreach ($data as $key => $val) { // reformat dates if (is_date_format($val)) { $date_ts = strtotime($val); $return['data'][$key] = english_date($val); $return['data'][$key . '_hour'] = date('h', $date_ts); $return['data'][$key . '_min'] = date('i', $date_ts); $return['data'][$key . '_ampm'] = date('a', $date_ts); } else { $return['data'][$key] = $val; } } } } return $all_data ? $return : $return['data']; }
/** * Used by on_before_clean model hook to determine the type of variable (int, string, array) * * @access public * @param array The values to be determined * @return string */ public function determine_type($value) { if (is_array($value) or is_serialized_str($value)) { return 'array'; } return 'string'; }
/** * Unserialize a saved value * * @access public * @param string the array value to unserialize * @param string the unserialization method. If none is specified it will default to the default setting of the model which is 'json' * @return array */ public function unserialize_value($val, $method = NULL) { // if not a valid field name, then we look for a key value with that name in the serialized_fields array $invalid_field_names = array('json', 'serialize'); if (!in_array($method, $invalid_field_names)) { foreach ($this->serialized_fields as $m => $field) { if ($field == $method) { $method = $m; break; } } } if (empty($method)) { $method = $this->default_serialization_method; } if ($method == 'serialize') { if (is_serialized_str($val)) { $val = unserialize($val); } } else { if (is_json_str($val)) { $val = json_decode($val, TRUE); } } return $val; }