array);
default:
throw new Exception('Unknown type: ' . $clz);
}
}
public static function Object2XML($o) {
$clz = get_class($o);
$res = sprintf('<%s>', strtolower($clz));
switch($clz) {
// simple data classes
case 'T1':
foreach(get_object_vars($o) as $nam => $val) {
$res .= sprintf('<%s>%s%s>', $nam, $val, $nam);
}
break;
// array wrapper classes
case 'T1Array':
foreach($o->array as $elm) {
$res .= Util::Object2XML($elm);
}
break;
default:
throw new Exception('Unknown type: ' . $clz);
}
$res .= sprintf('%s>', strtolower($clz));
return $res;
}
public static function Object2String($o, $typ) {
if($typ == 'application/xml') {
return Util::Object2XML($o);
} else if($typ == 'application/json') {
return Util::Object2JSON($o);
} else {
throw new Exception('Unknown type: ' . $typ);
}
}
public static function XML2Object($clz, $xml) {
$temp = simplexml_load_string($xml);
$res = new $clz();
switch($clz) {
// simple data classes
case 'T1':
foreach(get_object_vars($res) as $nam => $val) {
if(is_int($res->$nam)) {
$res->$nam = (int)$temp->$nam;
} else {
$res->$nam = (string)$temp->$nam;
}
}
break;
// array wrapper classes
case 'T1Array':
$elmclz = substr($clz, 0, -5);
foreach($temp as $elm) {
$o = new $elmclz();
foreach(get_object_vars($o) as $nam => $val) {
if(is_int($o->$nam)) {
$o->$nam = (int)$elm->$nam;
} else {
$o->$nam = (string)$elm->$nam;
}
}
$res->array[] = $o;
}
break;
default:
throw new Exception('Unknown type: ' . $clz);
}
return $res;
}
public static function JSON2Object($clz, $json) {
$temp = json_decode($json);
$res = new $clz();
switch($clz) {
// simple data classes
case 'T1':
foreach(get_object_vars($res) as $nam => $val) {
$res->$nam = $temp->$nam;
}
break;
// array wrapper classes
case 'T1Array':
$elmclz = substr($clz, 0, -5);
foreach($temp as $elm) {
$o = new $elmclz();
foreach(get_object_vars($o) as $nam => $val) {
$o->$nam = $elm->$nam;
}
$res->array[] = $o;
}
break;
default:
throw new Exception('Unknown type: ' . $clz);
}
return $res;
}
public static function String2Object($clz, $s, $typ) {
if($typ == 'application/xml') {
return Util::XML2Object($clz, $s);
} else if($typ == 'application/json') {
return Util::JSON2Object($clz, $s);
} else {
throw new Exception('Unknown type: ' . $typ);
}
}
}
class T1 {
public $f1;
public $f2;
public function __construct($f1 = 0, $f2 = '') {
$this->f1 = $f1;
$this->f2 = $f2;
}
public function __toString() {
return sprintf('{%s,%s}', $this->f1, $this->f2);
}
}
class T1Array {
public $array;
public function __construct($array = array()) {
$this->array = $array;
}
}
class Extra {
public $data;
public $msg;
public function __construct($data = null, $msg = '') {
$this->data = $data != null ? $data : new T1Array();
$this->msg = $msg;
}
}
echo Util::Object2XML(new T1(123, 'ABC')) . "\r\n";
echo Util::Object2JSON(new T1(123, 'ABC')) . "\r\n";
echo Util::Object2XML(new T1Array(array(new T1(1, 'A'), new T1(2, 'BB'), new T1(3, 'CCC')))) . "\r\n";
echo Util::Object2JSON(new T1Array(array(new T1(1, 'A'), new T1(2, 'BB'), new T1(3, 'CCC')))) . "\r\n";
print_r(Util::XML2Object('T1', '123getOne'));
print_r(Util::JSON2Object('T1', '{"f1":123,"f2":"getOne"}'));
print_r(Util::XML2Object('T1Array', '1A2BB3CCC'));
print_r(Util::JSON2Object('T1Array', '[{"f1":1,"f2":"A"},{"f1":2,"f2":"BB"},{"f1":3,"f2":"CCC"}]'));
?>