Back to index

php5  5.3.10
test.utility.php
Go to the documentation of this file.
00001 <?php
00002 
00003 function timestamp_to_soap_datetime($t) {
00004   return date('Y-m-d\TH:i:sO',$t);
00005 }
00006 
00007 function soap_datetime_to_timestamp($t) {
00008        /* Ignore Microsecconds */
00009   $iso8601 = '(-?[0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]*)?(Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
00010   if (!is_int($t)) {
00011          if (!ereg($iso8601,$t,$r)) {
00012          return false;
00013          }
00014        $t = gmmktime($r[4],$r[5],$r[6],$r[2],$r[3],$r[1]);
00015          if (!empty($r[8]) && $r[8] != 'Z') {
00016                 $op = substr($r[8],0,1);
00017               $h = substr($r[8],1,2);
00018               if (strstr($r[8],':')) {
00019                 $m = substr($r[8],4,2);
00020                 } else {
00021                 $m = substr($r[8],3,2);
00022                 }
00023               $t += (($op == "-"?1:-1) * $h * 60 + $m) * 60;
00024               }
00025   }
00026   return $t;
00027 }
00028 
00029 function date_compare($f1,$f2)
00030 {
00031        return soap_datetime_to_timestamp($f1) == soap_datetime_to_timestamp($f2);
00032 }
00033 
00034 function hex_compare($f1, $f2)
00035 {
00036   return strcasecmp($f1,$f2) == 0;
00037 }
00038 
00039 function number_compare($f1, $f2)
00040 {
00041     # figure out which has the least fractional digits
00042     preg_match('/.*?\.(.*)/',$f1,$m1);
00043     preg_match('/.*?\.(.*)/',$f2,$m2);
00044     #print_r($m1);
00045     # always use at least 2 digits of precision
00046     $d = max(min(strlen(count($m1)?$m1[1]:'0'),strlen(count($m2)?$m2[1]:'0')),2);
00047     $f1 = round($f1, $d);
00048     $f2 = round($f2, $d);
00049     return $f1 == $f2;
00050 //    return bccomp($f1, $f2, $d) == 0;
00051 }
00052 
00053 function boolean_compare($f1, $f2)
00054 {
00055     if (($f1 == 'true' || $f1 === TRUE || $f1 != 0) &&
00056         ($f2 == 'true' || $f2 === TRUE || $f2 != 0)) return TRUE;
00057     if (($f1 == 'false' || $f1 === FALSE || $f1 == 0) &&
00058         ($f2 == 'false' || $f2 === FALSE || $f2 == 0)) return TRUE;
00059     return FALSE;
00060 }
00061 
00062 function string_compare($e1, $e2)
00063 {
00064     if (is_numeric($e1) && is_numeric($e2)) {
00065         return number_compare($e1, $e2);
00066     }
00067     # handle dateTime comparison
00068     $e1_type = gettype($e1);
00069     $e2_type = gettype($e2);
00070     $ok = FALSE;
00071     if ($e1_type == "string") {
00072 //        $dt = new SOAP_Type_dateTime();
00073 //        $ok = $dt->compare($e1, $e2) == 0;
00074         $oj = false;
00075     }
00076     return $ok || $e1 == $e2 || strcasecmp(trim($e1), trim($e2)) == 0;
00077 }
00078 
00079 function array_compare(&$ar1, &$ar2) {
00080   if (gettype($ar1) != 'array' || gettype($ar2) != 'array') return FALSE;
00081   if (count($ar1) != count($ar2)) return FALSE;
00082   foreach ($ar1 as $k => $v) {
00083     if (!array_key_exists($k,$ar2)) return FALSE;
00084     if (!compare($v,$ar2[$k])) return FALSE;
00085   }
00086   return TRUE;
00087 }
00088 
00089 function object_compare(&$obj1, &$obj2) {
00090   if (gettype($obj1) != 'object' || gettype($obj2) != 'object') return FALSE;
00091 //  if (class_name(obj1) != class_name(obj2)) return FALSE;
00092   $ar1 = (array)$obj1;
00093   $ar2 = (array)$obj2;
00094   return array_compare($ar1,$ar2);
00095 }
00096 
00097 function compare(&$x,&$y) {
00098   $ok = 0;
00099   $x_type = gettype($x);
00100   $y_type = gettype($y);
00101   if ($x_type == $y_type) {
00102     if ($x_type == "array") {
00103       $ok = array_compare($x, $y);
00104     } else if ($x_type == "object") {
00105       $ok = object_compare($x, $y);
00106     } else if ($x_type == "double") {
00107       $ok = number_compare($x, $y);
00108 //    } else if ($x_type == 'boolean') {
00109 //      $ok = boolean_compare($x, $y);
00110     } else {
00111        $ok = ($x == $y);
00112 //      $ok = string_compare($expect, $result);
00113     }
00114   }
00115   return $ok;
00116 }
00117 
00118 
00119 function parseMessage($msg)
00120 {
00121     # strip line endings
00122     #$msg = preg_replace('/\r|\n/', ' ', $msg);
00123     $response = new SOAP_Parser($msg);
00124     if ($response->fault) {
00125         return $response->fault->getFault();
00126     }
00127     $return = $response->getResponse();
00128     $v = $response->decode($return);
00129     if (gettype($v) == 'array' && count($v)==1) {
00130         return array_shift($v);
00131     }
00132     return $v;
00133 }
00134 
00135 function var_dump_str($var) {
00136   ob_start();
00137   var_dump($var);
00138   $res = ob_get_contents();
00139   ob_end_clean();
00140   return $res;
00141 }
00142 
00143 ?>