BinaryComparison.php 6.4 KB

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