test.utility.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. function timestamp_to_soap_datetime($t) {
  3. return date('Y-m-d\TH:i:sO',$t);
  4. }
  5. function soap_datetime_to_timestamp($t) {
  6. /* Ignore Microsecconds */
  7. $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})?';
  8. if (!is_int($t)) {
  9. if (!ereg($iso8601,$t,$r)) {
  10. return false;
  11. }
  12. $t = gmmktime($r[4],$r[5],$r[6],$r[2],$r[3],$r[1]);
  13. if (!empty($r[8]) && $r[8] != 'Z') {
  14. $op = substr($r[8],0,1);
  15. $h = substr($r[8],1,2);
  16. if (strstr($r[8],':')) {
  17. $m = substr($r[8],4,2);
  18. } else {
  19. $m = substr($r[8],3,2);
  20. }
  21. $t += (($op == "-"?1:-1) * $h * 60 + $m) * 60;
  22. }
  23. }
  24. return $t;
  25. }
  26. function date_compare($f1,$f2)
  27. {
  28. return soap_datetime_to_timestamp($f1) == soap_datetime_to_timestamp($f2);
  29. }
  30. function hex_compare($f1, $f2)
  31. {
  32. return strcasecmp($f1,$f2) == 0;
  33. }
  34. function number_compare($f1, $f2)
  35. {
  36. # figure out which has the least fractional digits
  37. preg_match('/.*?\.(.*)/',$f1,$m1);
  38. preg_match('/.*?\.(.*)/',$f2,$m2);
  39. #print_r($m1);
  40. # always use at least 2 digits of precision
  41. $d = max(min(strlen(count($m1)?$m1[1]:'0'),strlen(count($m2)?$m2[1]:'0')),2);
  42. $f1 = round($f1, $d);
  43. $f2 = round($f2, $d);
  44. return $f1 == $f2;
  45. // return bccomp($f1, $f2, $d) == 0;
  46. }
  47. function boolean_compare($f1, $f2)
  48. {
  49. if (($f1 == 'true' || $f1 === TRUE || $f1 != 0) &&
  50. ($f2 == 'true' || $f2 === TRUE || $f2 != 0)) return TRUE;
  51. if (($f1 == 'false' || $f1 === FALSE || $f1 == 0) &&
  52. ($f2 == 'false' || $f2 === FALSE || $f2 == 0)) return TRUE;
  53. return FALSE;
  54. }
  55. function string_compare($e1, $e2)
  56. {
  57. if (is_numeric($e1) && is_numeric($e2)) {
  58. return number_compare($e1, $e2);
  59. }
  60. # handle dateTime comparison
  61. $e1_type = gettype($e1);
  62. $e2_type = gettype($e2);
  63. $ok = FALSE;
  64. if ($e1_type == "string") {
  65. // $dt = new SOAP_Type_dateTime();
  66. // $ok = $dt->compare($e1, $e2) == 0;
  67. $oj = false;
  68. }
  69. return $ok || $e1 == $e2 || strcasecmp(trim($e1), trim($e2)) == 0;
  70. }
  71. function array_compare(&$ar1, &$ar2) {
  72. if (gettype($ar1) != 'array' || gettype($ar2) != 'array') return FALSE;
  73. if (count($ar1) != count($ar2)) return FALSE;
  74. foreach ($ar1 as $k => $v) {
  75. if (!array_key_exists($k,$ar2)) return FALSE;
  76. if (!compare($v,$ar2[$k])) return FALSE;
  77. }
  78. return TRUE;
  79. }
  80. function object_compare(&$obj1, &$obj2) {
  81. if (gettype($obj1) != 'object' || gettype($obj2) != 'object') return FALSE;
  82. // if (class_name(obj1) != class_name(obj2)) return FALSE;
  83. $ar1 = (array)$obj1;
  84. $ar2 = (array)$obj2;
  85. return array_compare($ar1,$ar2);
  86. }
  87. function compare(&$x,&$y) {
  88. $ok = 0;
  89. $x_type = gettype($x);
  90. $y_type = gettype($y);
  91. if ($x_type == $y_type) {
  92. if ($x_type == "array") {
  93. $ok = array_compare($x, $y);
  94. } else if ($x_type == "object") {
  95. $ok = object_compare($x, $y);
  96. } else if ($x_type == "double") {
  97. $ok = number_compare($x, $y);
  98. // } else if ($x_type == 'boolean') {
  99. // $ok = boolean_compare($x, $y);
  100. } else {
  101. $ok = ($x == $y);
  102. // $ok = string_compare($expect, $result);
  103. }
  104. }
  105. return $ok;
  106. }
  107. function parseMessage($msg)
  108. {
  109. # strip line endings
  110. #$msg = preg_replace('/\r|\n/', ' ', $msg);
  111. $response = new SOAP_Parser($msg);
  112. if ($response->fault) {
  113. return $response->fault->getFault();
  114. }
  115. $return = $response->getResponse();
  116. $v = $response->decode($return);
  117. if (gettype($v) == 'array' && count($v)==1) {
  118. return array_shift($v);
  119. }
  120. return $v;
  121. }
  122. function var_dump_str($var) {
  123. ob_start();
  124. var_dump($var);
  125. $res = ob_get_contents();
  126. ob_end_clean();
  127. return $res;
  128. }
  129. ?>