', $nam);
}
public function fieldValue($nam, $val) {
return sprintf('<%s>%s%s>', $nam, $val, $nam);
}
public function fieldSeparator() {
return '';
}
public function fieldSuffix($nam) {
return sprintf('%s>', $nam);
}
public function arrayPrefix($nam) {
return sprintf('<%s>', $nam);
}
public function arrayValue($val) {
return $val;
}
public function arraySeparator() {
return '';
}
public function arraySuffix($nam) {
return sprintf('%s>', $nam);
}
}
class Util {
private static function Object2Anything($o, $fmt) {
$clz = strtolower(get_class($o));
if(substr($clz, -5) == 'array' && isset($o->array)) {
$res = $fmt->arrayPrefix($clz);
foreach($o->array as $elm) {
if(strlen($res) > 1) $res .= $fmt->arraySeparator();
$res .= $fmt->arrayValue(Util::Object2Anything($elm, $fmt));
}
$res .= $fmt->arraySuffix($clz);
return $res;
} else {
$res = $fmt->fieldPrefix($clz);
foreach(get_object_vars($o) as $nam => $val) {
if(strlen($res) > 1) $res .= $fmt->fieldSeparator();
$res .= $fmt->fieldValue($nam, is_object($val) ? Util::Object2Anything($val, $fmt) : $val);
}
$res .= $fmt->fieldSuffix($clz);
return $res;
}
}
public static function Object2JSON($o) {
return Util::Object2Anything($o, new JSONFormatter());
}
public static function Object2XML($o) {
return Util::Object2Anything($o, new XMLFormatter());
}
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);
}
}
private static function mapFields(&$fromobj, &$toobj) {
foreach(get_object_vars($toobj) as $nam => $val) {
if(is_object($toobj->$nam) || is_array($toobj->$nam)) {
Util::map($fromobj->$nam, $toobj->$nam);
} else {
if(is_int($toobj->$nam)) {
$toobj->$nam = (int)$fromobj->$nam;
} else {
$toobj->$nam = (string)$fromobj->$nam;
}
}
}
}
private static function mapArray(&$fromarr, &$toarr, $arrelmclz) {
foreach($fromarr as $elm) {
$temp = new $arrelmclz();
Util::map($elm, $temp);
$toarr[] = $temp;
}
}
private static function map(&$fromobj, &$toobj) {
$clz = strtolower(get_class($toobj));
if(substr($clz, -5) == 'array' && isset($toobj->array)) {
$arrelmclz = substr(get_class($toobj), 0 , -5);
$temp = $fromobj;
if(isset($fromobj->array)) $temp = $fromobj->array;
$f1 = strtolower($arrelmclz . 'array');
$f2 = strtolower($arrelmclz);
if(isset($fromobj->$f1)) $temp = $fromobj->$f1->$f2;
Util::mapArray($temp, $toobj->array, $arrelmclz);
} else {
Util::mapFields($fromobj, $toobj);
}
}
public static function XML2Object($clz, $xml) {
$temp = simplexml_load_string($xml);
$res = new $clz();
Util::map($temp, $res);
return $res;
}
public static function JSON2Object($clz, $json) {
$temp = json_decode($json);
$res = new $clz();
Util::map($temp, $res);
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";
echo Util::Object2XML(new Extra(new T1Array(array(new T1(1, 'A'), new T1(2, 'BB'), new T1(3, 'CCC'))), 'Test')) . "\r\n";
echo Util::Object2JSON(new Extra(new T1Array(array(new T1(1, 'A'), new T1(2, 'BB'), new T1(3, 'CCC'))), 'Test')) . "\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"}]'));
print_r(Util::XML2Object('Extra', '1A2BB3CCCTest'));
print_r(Util::JSON2Object('Extra', '{"data":{"array":[{"f1":1,"f2":"A"},{"f1":2,"f2":"BB"},{"f1":3,"f2":"CCC"}]},"msg":"Test"}'));
?>