contributions/instancereference/lib/helpers/instancereferenceserializer.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Helper to turn an isntance into an array or string and vice versa 00004 */ 00005 class InstanceReferenceSerializier { 00006 /** 00007 * Convert an instance to an array (containing type and keys) 00008 * 00009 * @param IDataObject $inst 00010 * @return array 00011 */ 00012 public static function instance_to_array(IDataObject $inst) { 00013 $ret = array($inst->get_table_name()); 00014 foreach($inst->get_table_keys() as $key => $field) { 00015 /* @var $field DBField */ 00016 $ret[] = $inst->$key; 00017 } 00018 return $ret; 00019 } 00020 00021 /** 00022 * Convert an an array (containing type and keys) to an instance 00023 * 00024 * @return IDataObject 00025 * @param array $arr_inst 00026 */ 00027 public static function array_to_instance($arr_inst) { 00028 $ret = false; 00029 $table = array_shift($arr_inst); 00030 $dao = DB::create($table); 00031 $params = array(); 00032 if ($dao) { 00033 foreach($dao->get_table_keys() as $key => $field) { 00034 /* @var $field DBField */ 00035 $params[$key] = array_shift($arr_inst); 00036 } 00037 } 00038 return DB::get_item_multi($table, $params); 00039 } 00040 00041 /** 00042 * Convert an instance to a string (containing type and keys, concated by '/') 00043 * 00044 * @param IDataObject $inst 00045 * @return string 00046 */ 00047 public static function instance_to_string(IDataObject $inst) { 00048 $ret = ''; 00049 $arr = self::instance_to_array($inst); 00050 if (is_array($arr)) { 00051 $ret = implode('/', $arr); 00052 } 00053 return $ret; 00054 } 00055 00056 /** 00057 * Convert a string (containing type and keys, concated by '/') to an instance 00058 * 00059 * @param string $s 00060 * @return IDataObject $inst 00061 */ 00062 public static function string_to_instance($s) { 00063 $arr = explode('/', $s); 00064 return self::array_to_instance($arr); 00065 } 00066 }