| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 7 |
|
| 8 |
class BinaryComparison |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Epsilon Precision used for comparisons in calculations. |
| 12 |
*/ |
| 13 |
private const DELTA = 0.1e-12; |
| 14 |
|
| 15 |
/** |
| 16 |
* Compare two strings in the same way as strcmp() except that lowercase come before uppercase letters. |
| 17 |
* |
| 18 |
* @param mixed $str1 First string value for the comparison, expect ?string |
| 19 |
* @param mixed $str2 Second string value for the comparison, expect ?string |
| 20 |
*/ |
| 21 |
private static function strcmpLowercaseFirst($str1, $str2): int |
| 22 |
{ |
| 23 |
$str1 = StringHelper::convertToString($str1); |
| 24 |
$str2 = StringHelper::convertToString($str2); |
| 25 |
$inversedStr1 = StringHelper::strCaseReverse($str1); |
| 26 |
$inversedStr2 = StringHelper::strCaseReverse($str2); |
| 27 |
|
| 28 |
return strcmp($inversedStr1, $inversedStr2); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* PHP8.1 deprecates passing null to strcmp. |
| 33 |
* |
| 34 |
* @param mixed $str1 First string value for the comparison, expect ?string |
| 35 |
* @param mixed $str2 Second string value for the comparison, expect ?string |
| 36 |
*/ |
| 37 |
private static function strcmpAllowNull($str1, $str2): int |
| 38 |
{ |
| 39 |
$str1 = StringHelper::convertToString($str1); |
| 40 |
$str2 = StringHelper::convertToString($str2); |
| 41 |
|
| 42 |
return strcmp($str1, $str2); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @return bool|string |
| 47 |
* @param mixed $operand1 |
| 48 |
* @param mixed $operand2 |
| 49 |
*/ |
| 50 |
public static function compare($operand1, $operand2, string $operator) |
| 51 |
{ |
| 52 |
// Simple validate the two operands if they are string values |
| 53 |
if (is_string($operand1) && $operand1 > '' && $operand1[0] == Calculation::FORMULA_STRING_QUOTE) { |
| 54 |
$operand1 = Calculation::unwrapResult($operand1); |
| 55 |
} |
| 56 |
if (ErrorValue::isError($operand1, true)) { |
| 57 |
/** @var string $operand1 */ |
| 58 |
return $operand1; |
| 59 |
} |
| 60 |
if (is_string($operand2) && $operand2 > '' && $operand2[0] == Calculation::FORMULA_STRING_QUOTE) { |
| 61 |
$operand2 = Calculation::unwrapResult($operand2); |
| 62 |
} |
| 63 |
if (ErrorValue::isError($operand2, true)) { |
| 64 |
/** @var string $operand2 */ |
| 65 |
return $operand2; |
| 66 |
} |
| 67 |
|
| 68 |
// Use case-insensitive comparison if not OpenOffice mode |
| 69 |
if (Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) { |
| 70 |
if (is_string($operand1)) { |
| 71 |
$operand1 = StringHelper::strToUpper($operand1); |
| 72 |
} |
| 73 |
if (is_string($operand2)) { |
| 74 |
$operand2 = StringHelper::strToUpper($operand2); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$useLowercaseFirstComparison = is_string($operand1) |
| 79 |
&& is_string($operand2) |
| 80 |
&& Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE; |
| 81 |
|
| 82 |
return self::evaluateComparison($operand1, $operand2, $operator, $useLowercaseFirstComparison); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param mixed $operand1 |
| 87 |
* @param mixed $operand2 |
| 88 |
*/ |
| 89 |
private static function evaluateComparison($operand1, $operand2, string $operator, bool $useLowercaseFirstComparison): bool |
| 90 |
{ |
| 91 |
switch ($operator) { |
| 92 |
case '=': |
| 93 |
return self::equal($operand1, $operand2); |
| 94 |
case '>': |
| 95 |
return self::greaterThan($operand1, $operand2, $useLowercaseFirstComparison); |
| 96 |
case '<': |
| 97 |
return self::lessThan($operand1, $operand2, $useLowercaseFirstComparison); |
| 98 |
case '>=': |
| 99 |
return self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison); |
| 100 |
case '<=': |
| 101 |
return self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison); |
| 102 |
case '<>': |
| 103 |
return self::notEqual($operand1, $operand2); |
| 104 |
default: |
| 105 |
throw new Exception('Unsupported binary comparison operator'); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param mixed $operand1 |
| 111 |
* @param mixed $operand2 |
| 112 |
*/ |
| 113 |
private static function equal($operand1, $operand2): bool |
| 114 |
{ |
| 115 |
if (is_numeric($operand1) && is_numeric($operand2)) { |
| 116 |
$result = (abs($operand1 - $operand2) < self::DELTA); |
| 117 |
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
| 118 |
$result = $operand1 == $operand2; |
| 119 |
} else { |
| 120 |
$result = self::strcmpAllowNull($operand1, $operand2) == 0; |
| 121 |
} |
| 122 |
|
| 123 |
return $result; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param mixed $operand1 |
| 128 |
* @param mixed $operand2 |
| 129 |
*/ |
| 130 |
private static function greaterThanOrEqual($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
| 131 |
{ |
| 132 |
if (is_numeric($operand1) && is_numeric($operand2)) { |
| 133 |
$result = ((abs($operand1 - $operand2) < self::DELTA) || ($operand1 > $operand2)); |
| 134 |
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
| 135 |
$result = $operand1 >= $operand2; |
| 136 |
} elseif ($useLowercaseFirstComparison) { |
| 137 |
$result = self::strcmpLowercaseFirst($operand1, $operand2) >= 0; |
| 138 |
} else { |
| 139 |
$result = self::strcmpAllowNull($operand1, $operand2) >= 0; |
| 140 |
} |
| 141 |
|
| 142 |
return $result; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @param mixed $operand1 |
| 147 |
* @param mixed $operand2 |
| 148 |
*/ |
| 149 |
private static function lessThanOrEqual($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
| 150 |
{ |
| 151 |
if (is_numeric($operand1) && is_numeric($operand2)) { |
| 152 |
$result = ((abs($operand1 - $operand2) < self::DELTA) || ($operand1 < $operand2)); |
| 153 |
} elseif (($operand1 === null && is_numeric($operand2)) || ($operand2 === null && is_numeric($operand1))) { |
| 154 |
$result = $operand1 <= $operand2; |
| 155 |
} elseif ($useLowercaseFirstComparison) { |
| 156 |
$result = self::strcmpLowercaseFirst($operand1, $operand2) <= 0; |
| 157 |
} else { |
| 158 |
$result = self::strcmpAllowNull($operand1, $operand2) <= 0; |
| 159 |
} |
| 160 |
|
| 161 |
return $result; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @param mixed $operand1 |
| 166 |
* @param mixed $operand2 |
| 167 |
*/ |
| 168 |
private static function greaterThan($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
| 169 |
{ |
| 170 |
return self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison) !== true; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param mixed $operand1 |
| 175 |
* @param mixed $operand2 |
| 176 |
*/ |
| 177 |
private static function lessThan($operand1, $operand2, bool $useLowercaseFirstComparison): bool |
| 178 |
{ |
| 179 |
return self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison) !== true; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @param mixed $operand1 |
| 184 |
* @param mixed $operand2 |
| 185 |
*/ |
| 186 |
private static function notEqual($operand1, $operand2): bool |
| 187 |
{ |
| 188 |
return self::equal($operand1, $operand2) !== true; |
| 189 |
} |
| 190 |
} |
| 191 |
|