Operations.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\Logical;
  3. use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  7. class Operations
  8. {
  9. use ArrayEnabled;
  10. /**
  11. * LOGICAL_AND.
  12. *
  13. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  14. *
  15. * Excel Function:
  16. * =AND(logical1[,logical2[, ...]])
  17. *
  18. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  19. * or references that contain logical values.
  20. *
  21. * Boolean arguments are treated as True or False as appropriate
  22. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  23. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string
  24. * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  25. *
  26. * @param mixed ...$args Data values
  27. *
  28. * @return bool|string the logical AND of the arguments
  29. */
  30. public static function logicalAnd(...$args)
  31. {
  32. $args = Functions::flattenArray($args);
  33. if (count($args) == 0) {
  34. return ExcelError::VALUE();
  35. }
  36. $args = array_filter($args, function ($value) {
  37. return $value !== null || (is_string($value) && trim($value) == '');
  38. });
  39. $returnValue = self::countTrueValues($args);
  40. if (is_string($returnValue)) {
  41. return $returnValue;
  42. }
  43. $argCount = count($args);
  44. return ($returnValue > 0) && ($returnValue == $argCount);
  45. }
  46. /**
  47. * LOGICAL_OR.
  48. *
  49. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
  50. *
  51. * Excel Function:
  52. * =OR(logical1[,logical2[, ...]])
  53. *
  54. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  55. * or references that contain logical values.
  56. *
  57. * Boolean arguments are treated as True or False as appropriate
  58. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  59. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string
  60. * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  61. *
  62. * @param mixed $args Data values
  63. *
  64. * @return bool|string the logical OR of the arguments
  65. */
  66. public static function logicalOr(...$args)
  67. {
  68. $args = Functions::flattenArray($args);
  69. if (count($args) == 0) {
  70. return ExcelError::VALUE();
  71. }
  72. $args = array_filter($args, function ($value) {
  73. return $value !== null || (is_string($value) && trim($value) == '');
  74. });
  75. $returnValue = self::countTrueValues($args);
  76. if (is_string($returnValue)) {
  77. return $returnValue;
  78. }
  79. return $returnValue > 0;
  80. }
  81. /**
  82. * LOGICAL_XOR.
  83. *
  84. * Returns the Exclusive Or logical operation for one or more supplied conditions.
  85. * i.e. the Xor function returns TRUE if an odd number of the supplied conditions evaluate to TRUE,
  86. * and FALSE otherwise.
  87. *
  88. * Excel Function:
  89. * =XOR(logical1[,logical2[, ...]])
  90. *
  91. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  92. * or references that contain logical values.
  93. *
  94. * Boolean arguments are treated as True or False as appropriate
  95. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  96. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string
  97. * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  98. *
  99. * @param mixed $args Data values
  100. *
  101. * @return bool|string the logical XOR of the arguments
  102. */
  103. public static function logicalXor(...$args)
  104. {
  105. $args = Functions::flattenArray($args);
  106. if (count($args) == 0) {
  107. return ExcelError::VALUE();
  108. }
  109. $args = array_filter($args, function ($value) {
  110. return $value !== null || (is_string($value) && trim($value) == '');
  111. });
  112. $returnValue = self::countTrueValues($args);
  113. if (is_string($returnValue)) {
  114. return $returnValue;
  115. }
  116. return $returnValue % 2 == 1;
  117. }
  118. /**
  119. * NOT.
  120. *
  121. * Returns the boolean inverse of the argument.
  122. *
  123. * Excel Function:
  124. * =NOT(logical)
  125. *
  126. * The argument must evaluate to a logical value such as TRUE or FALSE
  127. *
  128. * Boolean arguments are treated as True or False as appropriate
  129. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  130. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string
  131. * holds the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  132. *
  133. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
  134. * Or can be an array of values
  135. *
  136. * @return array|bool|string the boolean inverse of the argument
  137. * If an array of values is passed as an argument, then the returned result will also be an array
  138. * with the same dimensions
  139. */
  140. public static function NOT($logical = false)
  141. {
  142. if (is_array($logical)) {
  143. return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $logical);
  144. }
  145. if (is_string($logical)) {
  146. $logical = mb_strtoupper($logical, 'UTF-8');
  147. if (($logical == 'TRUE') || ($logical == Calculation::getTRUE())) {
  148. return false;
  149. } elseif (($logical == 'FALSE') || ($logical == Calculation::getFALSE())) {
  150. return true;
  151. }
  152. return ExcelError::VALUE();
  153. }
  154. return !$logical;
  155. }
  156. /**
  157. * @return int|string
  158. */
  159. private static function countTrueValues(array $args)
  160. {
  161. $trueValueCount = 0;
  162. foreach ($args as $arg) {
  163. // Is it a boolean value?
  164. if (is_bool($arg)) {
  165. $trueValueCount += $arg;
  166. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  167. $trueValueCount += ((int) $arg != 0);
  168. } elseif (is_string($arg)) {
  169. $arg = mb_strtoupper($arg, 'UTF-8');
  170. if (($arg == 'TRUE') || ($arg == Calculation::getTRUE())) {
  171. $arg = true;
  172. } elseif (($arg == 'FALSE') || ($arg == Calculation::getFALSE())) {
  173. $arg = false;
  174. } else {
  175. return ExcelError::VALUE();
  176. }
  177. $trueValueCount += ($arg != 0);
  178. }
  179. }
  180. return $trueValueCount;
  181. }
  182. }