Пример #1
0
function string_compare($e1, $e2)
{
    if (!is_string($e1) || !is_string($e2)) {
        return false;
    }
    $e1 = trim(str_replace(array("\r", "\n"), '', $e1));
    $e2 = trim(str_replace(array("\r", "\n"), '', $e2));
    if (is_numeric($e1) && is_numeric($e2)) {
        return number_compare($e1, $e2);
    }
    // handle dateTime comparison
    $e1_type = gettype($e1);
    $e2_type = gettype($e2);
    $ok = false;
    if ($e1_type == "string") {
        require_once 'SOAP/Type/dateTime.php';
        $dt = new SOAP_Type_dateTime();
        $ok = $dt->compare($e1, $e2) == 0;
    }
    return $ok || $e1 == $e2 || strcasecmp($e1, $e2) == 0;
}
function array_compare(&$ar1, &$ar2)
{
    if (gettype($ar1) != 'array' || gettype($ar2) != 'array') {
        return FALSE;
    }
    # first a shallow diff
    if (count($ar1) != count($ar2)) {
        return FALSE;
    }
    $diff = array_diff($ar1, $ar2);
    if (count($diff) == 0) {
        return TRUE;
    }
    # diff failed, do a full check of the array
    foreach ($ar1 as $k => $v) {
        #print "comparing $v == $ar2[$k]\n";
        if (gettype($v) == "array") {
            if (!array_compare($v, $ar2[$k])) {
                return FALSE;
            }
        } else {
            if (!string_compare($v, $ar2[$k])) {
                return FALSE;
            }
            if ($type == 'float') {
                # we'll only compare to 3 digits of precision
                $ok = number_compare($expect, $result, 3);
            }
            if ($type == 'boolean') {
                $ok = boolean_compare($expect, $result);
            } else {
                $ok = string_compare($expect, $result);
            }
        }
    }
    return TRUE;
}
Пример #3
0
 /**
  * Compares two PHP types for a match.
  *
  * @param mixed $expect
  * @param mixed $test_result
  *
  * @return boolean
  */
 function compareResult(&$expect, &$result, $type = null)
 {
     $expect_type = gettype($expect);
     $result_type = gettype($result);
     if ($expect_type == 'array' && $result_type == 'array') {
         // compare arrays
         return array_compare($expect, $result);
     }
     if ($type == 'float') {
         // We'll only compare to 3 digits of precision.
         return number_compare($expect, $result);
     }
     if ($type == 'boolean') {
         return boolean_compare($expect, $result);
     }
     return string_compare($expect, $result);
 }
Пример #4
0
function compare(&$x, &$y)
{
    $ok = 0;
    $x_type = gettype($x);
    $y_type = gettype($y);
    if ($x_type == $y_type) {
        if ($x_type == "array") {
            $ok = array_compare($x, $y);
        } else {
            if ($x_type == "object") {
                $ok = object_compare($x, $y);
            } else {
                if ($x_type == "double") {
                    $ok = number_compare($x, $y);
                    //    } else if ($x_type == 'boolean') {
                    //      $ok = boolean_compare($x, $y);
                } else {
                    $ok = $x == $y;
                    //      $ok = string_compare($expect, $result);
                }
            }
        }
    }
    return $ok;
}