Logical.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation;
  3. class Logical
  4. {
  5. /**
  6. * TRUE.
  7. *
  8. * Returns the boolean TRUE.
  9. *
  10. * Excel Function:
  11. * =TRUE()
  12. *
  13. * @category Logical Functions
  14. *
  15. * @return bool True
  16. */
  17. public static function true()
  18. {
  19. return true;
  20. }
  21. /**
  22. * FALSE.
  23. *
  24. * Returns the boolean FALSE.
  25. *
  26. * Excel Function:
  27. * =FALSE()
  28. *
  29. * @category Logical Functions
  30. *
  31. * @return bool False
  32. */
  33. public static function false()
  34. {
  35. return false;
  36. }
  37. /**
  38. * LOGICAL_AND.
  39. *
  40. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  41. *
  42. * Excel Function:
  43. * =AND(logical1[,logical2[, ...]])
  44. *
  45. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  46. * or references that contain logical values.
  47. *
  48. * Boolean arguments are treated as True or False as appropriate
  49. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  50. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  51. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  52. *
  53. * @category Logical Functions
  54. *
  55. * @param mixed ...$args Data values
  56. *
  57. * @return bool|string the logical AND of the arguments
  58. */
  59. public static function logicalAnd(...$args)
  60. {
  61. // Return value
  62. $returnValue = true;
  63. // Loop through the arguments
  64. $aArgs = Functions::flattenArray($args);
  65. $argCount = -1;
  66. foreach ($aArgs as $argCount => $arg) {
  67. // Is it a boolean value?
  68. if (is_bool($arg)) {
  69. $returnValue = $returnValue && $arg;
  70. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  71. $returnValue = $returnValue && ($arg != 0);
  72. } elseif (is_string($arg)) {
  73. $arg = strtoupper($arg);
  74. if (($arg == 'TRUE') || ($arg == Calculation::getTRUE())) {
  75. $arg = true;
  76. } elseif (($arg == 'FALSE') || ($arg == Calculation::getFALSE())) {
  77. $arg = false;
  78. } else {
  79. return Functions::VALUE();
  80. }
  81. $returnValue = $returnValue && ($arg != 0);
  82. }
  83. }
  84. // Return
  85. if ($argCount < 0) {
  86. return Functions::VALUE();
  87. }
  88. return $returnValue;
  89. }
  90. /**
  91. * LOGICAL_OR.
  92. *
  93. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
  94. *
  95. * Excel Function:
  96. * =OR(logical1[,logical2[, ...]])
  97. *
  98. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  99. * or references that contain logical values.
  100. *
  101. * Boolean arguments are treated as True or False as appropriate
  102. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  103. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  104. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  105. *
  106. * @category Logical Functions
  107. *
  108. * @param mixed $args Data values
  109. *
  110. * @return bool|string the logical OR of the arguments
  111. */
  112. public static function logicalOr(...$args)
  113. {
  114. // Return value
  115. $returnValue = false;
  116. // Loop through the arguments
  117. $aArgs = Functions::flattenArray($args);
  118. $argCount = -1;
  119. foreach ($aArgs as $argCount => $arg) {
  120. // Is it a boolean value?
  121. if (is_bool($arg)) {
  122. $returnValue = $returnValue || $arg;
  123. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  124. $returnValue = $returnValue || ($arg != 0);
  125. } elseif (is_string($arg)) {
  126. $arg = strtoupper($arg);
  127. if (($arg == 'TRUE') || ($arg == Calculation::getTRUE())) {
  128. $arg = true;
  129. } elseif (($arg == 'FALSE') || ($arg == Calculation::getFALSE())) {
  130. $arg = false;
  131. } else {
  132. return Functions::VALUE();
  133. }
  134. $returnValue = $returnValue || ($arg != 0);
  135. }
  136. }
  137. // Return
  138. if ($argCount < 0) {
  139. return Functions::VALUE();
  140. }
  141. return $returnValue;
  142. }
  143. /**
  144. * NOT.
  145. *
  146. * Returns the boolean inverse of the argument.
  147. *
  148. * Excel Function:
  149. * =NOT(logical)
  150. *
  151. * The argument must evaluate to a logical value such as TRUE or FALSE
  152. *
  153. * Boolean arguments are treated as True or False as appropriate
  154. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  155. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  156. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  157. *
  158. * @category Logical Functions
  159. *
  160. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
  161. *
  162. * @return bool|string the boolean inverse of the argument
  163. */
  164. public static function NOT($logical = false)
  165. {
  166. $logical = Functions::flattenSingleValue($logical);
  167. if (is_string($logical)) {
  168. $logical = strtoupper($logical);
  169. if (($logical == 'TRUE') || ($logical == Calculation::getTRUE())) {
  170. return false;
  171. } elseif (($logical == 'FALSE') || ($logical == Calculation::getFALSE())) {
  172. return true;
  173. }
  174. return Functions::VALUE();
  175. }
  176. return !$logical;
  177. }
  178. /**
  179. * STATEMENT_IF.
  180. *
  181. * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
  182. *
  183. * Excel Function:
  184. * =IF(condition[,returnIfTrue[,returnIfFalse]])
  185. *
  186. * Condition is any value or expression that can be evaluated to TRUE or FALSE.
  187. * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
  188. * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
  189. * This argument can use any comparison calculation operator.
  190. * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
  191. * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
  192. * then the IF function returns the text "Within budget"
  193. * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
  194. * the logical value TRUE for this argument.
  195. * ReturnIfTrue can be another formula.
  196. * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
  197. * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
  198. * then the IF function returns the text "Over budget".
  199. * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
  200. * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
  201. * ReturnIfFalse can be another formula.
  202. *
  203. * @category Logical Functions
  204. *
  205. * @param mixed $condition Condition to evaluate
  206. * @param mixed $returnIfTrue Value to return when condition is true
  207. * @param mixed $returnIfFalse Optional value to return when condition is false
  208. *
  209. * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
  210. */
  211. public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false)
  212. {
  213. $condition = ($condition === null) ? true : (bool) Functions::flattenSingleValue($condition);
  214. $returnIfTrue = ($returnIfTrue === null) ? 0 : Functions::flattenSingleValue($returnIfTrue);
  215. $returnIfFalse = ($returnIfFalse === null) ? false : Functions::flattenSingleValue($returnIfFalse);
  216. return ($condition) ? $returnIfTrue : $returnIfFalse;
  217. }
  218. /**
  219. * IFERROR.
  220. *
  221. * Excel Function:
  222. * =IFERROR(testValue,errorpart)
  223. *
  224. * @category Logical Functions
  225. *
  226. * @param mixed $testValue Value to check, is also the value returned when no error
  227. * @param mixed $errorpart Value to return when testValue is an error condition
  228. *
  229. * @return mixed The value of errorpart or testValue determined by error condition
  230. */
  231. public static function IFERROR($testValue = '', $errorpart = '')
  232. {
  233. $testValue = ($testValue === null) ? '' : Functions::flattenSingleValue($testValue);
  234. $errorpart = ($errorpart === null) ? '' : Functions::flattenSingleValue($errorpart);
  235. return self::statementIf(Functions::isError($testValue), $errorpart, $testValue);
  236. }
  237. }