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