PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/codeplex/PHPExcel/Calculation/Logical.php

https://github.com/testlinkjp/testlink-japanese-localization
PHP | 290 lines | 92 code | 24 blank | 174 comment | 44 complexity | fca7444266f36f76d31414beeae024a2 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Calculation
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Calculation_Logical
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Calculation_Logical {
  43. /**
  44. * TRUE
  45. *
  46. * Returns the boolean TRUE.
  47. *
  48. * Excel Function:
  49. * =TRUE()
  50. *
  51. * @access public
  52. * @category Logical Functions
  53. * @return boolean True
  54. */
  55. public static function TRUE() {
  56. return true;
  57. } // function TRUE()
  58. /**
  59. * FALSE
  60. *
  61. * Returns the boolean FALSE.
  62. *
  63. * Excel Function:
  64. * =FALSE()
  65. *
  66. * @access public
  67. * @category Logical Functions
  68. * @return boolean False
  69. */
  70. public static function FALSE() {
  71. return false;
  72. } // function FALSE()
  73. /**
  74. * LOGICAL_AND
  75. *
  76. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  77. *
  78. * Excel Function:
  79. * =AND(logical1[,logical2[, ...]])
  80. *
  81. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  82. * or references that contain logical values.
  83. *
  84. * Boolean arguments are treated as True or False as appropriate
  85. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  86. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  87. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  88. *
  89. * @access public
  90. * @category Logical Functions
  91. * @param mixed $arg,... Data values
  92. * @return boolean The logical AND of the arguments.
  93. */
  94. public static function LOGICAL_AND() {
  95. // Return value
  96. $returnValue = True;
  97. // Loop through the arguments
  98. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  99. $argCount = 0;
  100. foreach ($aArgs as $arg) {
  101. // Is it a boolean value?
  102. if (is_bool($arg)) {
  103. $returnValue = $returnValue && $arg;
  104. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  105. $returnValue = $returnValue && ($arg != 0);
  106. } elseif (is_string($arg)) {
  107. $arg = strtoupper($arg);
  108. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
  109. $arg = true;
  110. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
  111. $arg = false;
  112. } else {
  113. return PHPExcel_Calculation_Functions::VALUE();
  114. }
  115. $returnValue = $returnValue && ($arg != 0);
  116. }
  117. ++$argCount;
  118. }
  119. // Return
  120. if ($argCount == 0) {
  121. return PHPExcel_Calculation_Functions::VALUE();
  122. }
  123. return $returnValue;
  124. } // function LOGICAL_AND()
  125. /**
  126. * LOGICAL_OR
  127. *
  128. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
  129. *
  130. * Excel Function:
  131. * =OR(logical1[,logical2[, ...]])
  132. *
  133. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  134. * or references that contain logical values.
  135. *
  136. * Boolean arguments are treated as True or False as appropriate
  137. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  138. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  139. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  140. *
  141. * @access public
  142. * @category Logical Functions
  143. * @param mixed $arg,... Data values
  144. * @return boolean The logical OR of the arguments.
  145. */
  146. public static function LOGICAL_OR() {
  147. // Return value
  148. $returnValue = False;
  149. // Loop through the arguments
  150. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  151. $argCount = 0;
  152. foreach ($aArgs as $arg) {
  153. // Is it a boolean value?
  154. if (is_bool($arg)) {
  155. $returnValue = $returnValue || $arg;
  156. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  157. $returnValue = $returnValue || ($arg != 0);
  158. } elseif (is_string($arg)) {
  159. $arg = strtoupper($arg);
  160. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
  161. $arg = true;
  162. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
  163. $arg = false;
  164. } else {
  165. return PHPExcel_Calculation_Functions::VALUE();
  166. }
  167. $returnValue = $returnValue || ($arg != 0);
  168. }
  169. ++$argCount;
  170. }
  171. // Return
  172. if ($argCount == 0) {
  173. return PHPExcel_Calculation_Functions::VALUE();
  174. }
  175. return $returnValue;
  176. } // function LOGICAL_OR()
  177. /**
  178. * NOT
  179. *
  180. * Returns the boolean inverse of the argument.
  181. *
  182. * Excel Function:
  183. * =NOT(logical)
  184. *
  185. * The argument must evaluate to a logical value such as TRUE or FALSE
  186. *
  187. * Boolean arguments are treated as True or False as appropriate
  188. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  189. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  190. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  191. *
  192. * @access public
  193. * @category Logical Functions
  194. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
  195. * @return boolean The boolean inverse of the argument.
  196. */
  197. public static function NOT($logical) {
  198. $logical = PHPExcel_Calculation_Functions::flattenSingleValue($logical);
  199. if (is_string($logical)) {
  200. $logical = strtoupper($logical);
  201. if (($logical == 'TRUE') || ($logical == PHPExcel_Calculation::getTRUE())) {
  202. return false;
  203. } elseif (($logical == 'FALSE') || ($logical == PHPExcel_Calculation::getFALSE())) {
  204. return true;
  205. } else {
  206. return PHPExcel_Calculation_Functions::VALUE();
  207. }
  208. }
  209. return !$logical;
  210. } // function NOT()
  211. /**
  212. * STATEMENT_IF
  213. *
  214. * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
  215. *
  216. * Excel Function:
  217. * =IF(condition[,returnIfTrue[,returnIfFalse]])
  218. *
  219. * Condition is any value or expression that can be evaluated to TRUE or FALSE.
  220. * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
  221. * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
  222. * This argument can use any comparison calculation operator.
  223. * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
  224. * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
  225. * then the IF function returns the text "Within budget"
  226. * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
  227. * the logical value TRUE for this argument.
  228. * ReturnIfTrue can be another formula.
  229. * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
  230. * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
  231. * then the IF function returns the text "Over budget".
  232. * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
  233. * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
  234. * ReturnIfFalse can be another formula.
  235. *
  236. * @access public
  237. * @category Logical Functions
  238. * @param mixed $condition Condition to evaluate
  239. * @param mixed $returnIfTrue Value to return when condition is true
  240. * @param mixed $returnIfFalse Optional value to return when condition is false
  241. * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
  242. */
  243. public static function STATEMENT_IF($condition = true, $returnIfTrue = 0, $returnIfFalse = False) {
  244. $condition = (is_null($condition)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($condition);
  245. $returnIfTrue = (is_null($returnIfTrue)) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($returnIfTrue);
  246. $returnIfFalse = (is_null($returnIfFalse)) ? False : PHPExcel_Calculation_Functions::flattenSingleValue($returnIfFalse);
  247. return ($condition ? $returnIfTrue : $returnIfFalse);
  248. } // function STATEMENT_IF()
  249. /**
  250. * IFERROR
  251. *
  252. * Excel Function:
  253. * =IFERROR(testValue,errorpart)
  254. *
  255. * @access public
  256. * @category Logical Functions
  257. * @param mixed $testValue Value to check, is also the value returned when no error
  258. * @param mixed $errorpart Value to return when testValue is an error condition
  259. * @return mixed The value of errorpart or testValue determined by error condition
  260. */
  261. public static function IFERROR($testValue = '', $errorpart = '') {
  262. $testValue = (is_null($testValue)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  263. $errorpart = (is_null($errorpart)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($errorpart);
  264. return self::STATEMENT_IF(PHPExcel_Calculation_Functions::IS_ERROR($testValue), $errorpart, $testValue);
  265. } // function IFERROR()
  266. } // class PHPExcel_Calculation_Logical