File Source for Logical.php

PHPExcel_Calculation

# Source for file Logical.php

Documentation is available at Logical.php

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. */
29. /** PHPExcel root directory */
30. if (! defined('PHPEXCEL_ROOT')) {
31. /**
32. * @ignore
33. */
34. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36. }
39. /**
40. * PHPExcel_Calculation_Logical
41. *
42. * @category PHPExcel
43. * @package PHPExcel_Calculation
44. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
45. */
46. class PHPExcel_Calculation_Logical {
48. /**
49. * TRUE
50. *
51. * Returns the boolean TRUE.
52. *
53. * Excel Function:
54. * =TRUE()
55. *
56. * @access public
57. * @category Logical Functions
58. * @return boolean True
59. */
60. public static function TRUE() {
61. return true;
62. } // function TRUE()
65. /**
66. * FALSE
67. *
68. * Returns the boolean FALSE.
69. *
70. * Excel Function:
71. * =FALSE()
72. *
73. * @access public
74. * @category Logical Functions
75. * @return boolean False
76. */
77. public static function FALSE() {
78. return false;
79. } // function FALSE()
82. /**
83. * LOGICAL_AND
84. *
85. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
86. *
87. * Excel Function:
88. * =AND(logical1[,logical2[, ...]])
89. *
90. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
91. * or references that contain logical values.
92. *
93. * Boolean arguments are treated as True or False as appropriate
94. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
95. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
96. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
97. *
98. * @access public
99. * @category Logical Functions
100. * @param mixed $arg,... Data values
101. * @return boolean The logical AND of the arguments.
102. */
103. public static function LOGICAL_AND() {
104. // Return value
105. $returnValue = True;
107. // Loop through the arguments
108. $aArgs = PHPExcel_Calculation_Functions:: flattenArray( func_get_args());
109. $argCount = 0;
110. foreach ($aArgs as $arg) {
111. // Is it a boolean value?
112. if ( is_bool($arg)) {
113. $returnValue = $returnValue && $arg;
114. } elseif (( is_numeric($arg)) && (! is_string($arg))) {
115. $returnValue = $returnValue && ($arg != 0);
116. } elseif ( is_string($arg)) {
117. $arg = strtoupper($arg);
118. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation:: getTRUE())) {
119. $arg = true;
120. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation:: getFALSE())) {
121. $arg = false;
122. } else {
123. return PHPExcel_Calculation_Functions:: VALUE();
124. }
125. $returnValue = $returnValue && ($arg != 0);
126. }
127. ++$argCount;
128. }
130. // Return
131. if ($argCount == 0) {
132. return PHPExcel_Calculation_Functions:: VALUE();
133. }
134. return $returnValue;
135. } // function LOGICAL_AND()
138. /**
139. * LOGICAL_OR
140. *
141. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
142. *
143. * Excel Function:
144. * =OR(logical1[,logical2[, ...]])
145. *
146. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
147. * or references that contain logical values.
148. *
149. * Boolean arguments are treated as True or False as appropriate
150. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
151. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
152. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
153. *
154. * @access public
155. * @category Logical Functions
156. * @param mixed $arg,... Data values
157. * @return boolean The logical OR of the arguments.
158. */
159. public static function LOGICAL_OR() {
160. // Return value
161. $returnValue = False;
163. // Loop through the arguments
164. $aArgs = PHPExcel_Calculation_Functions:: flattenArray( func_get_args());
165. $argCount = 0;
166. foreach ($aArgs as $arg) {
167. // Is it a boolean value?
168. if ( is_bool($arg)) {
169. $returnValue = $returnValue || $arg;
170. } elseif (( is_numeric($arg)) && (! is_string($arg))) {
171. $returnValue = $returnValue || ($arg != 0);
172. } elseif ( is_string($arg)) {
173. $arg = strtoupper($arg);
174. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation:: getTRUE())) {
175. $arg = true;
176. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation:: getFALSE())) {
177. $arg = false;
178. } else {
179. return PHPExcel_Calculation_Functions:: VALUE();
180. }
181. $returnValue = $returnValue || ($arg != 0);
182. }
183. ++$argCount;
184. }
186. // Return
187. if ($argCount == 0) {
188. return PHPExcel_Calculation_Functions:: VALUE();
189. }
190. return $returnValue;
191. } // function LOGICAL_OR()
194. /**
195. * NOT
196. *
197. * Returns the boolean inverse of the argument.
198. *
199. * Excel Function:
200. * =NOT(logical)
201. *
202. * The argument must evaluate to a logical value such as TRUE or FALSE
203. *
204. * Boolean arguments are treated as True or False as appropriate
205. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
206. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
207. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
208. *
209. * @access public
210. * @category Logical Functions
211. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
212. * @return boolean The boolean inverse of the argument.
213. */
214. public static function NOT($logical) {
215. $logical = PHPExcel_Calculation_Functions:: flattenSingleValue($logical);
216. if ( is_string($logical)) {
217. $logical = strtoupper($logical);
218. if (($logical == 'TRUE') || ($logical == PHPExcel_Calculation:: getTRUE())) {
219. return false;
220. } elseif (($logical == 'FALSE') || ($logical == PHPExcel_Calculation:: getFALSE())) {
221. return true;
222. } else {
223. return PHPExcel_Calculation_Functions:: VALUE();
224. }
225. }
227. return !$logical;
228. } // function NOT()
230. /**
231. * STATEMENT_IF
232. *
233. * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
234. *
235. * Excel Function:
236. * =IF(condition[,returnIfTrue[,returnIfFalse]])
237. *
238. * Condition is any value or expression that can be evaluated to TRUE or FALSE.
239. * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
240. * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
241. * This argument can use any comparison calculation operator.
242. * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
243. * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
244. * then the IF function returns the text "Within budget"
245. * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
246. * the logical value TRUE for this argument.
247. * ReturnIfTrue can be another formula.
248. * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
249. * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
250. * then the IF function returns the text "Over budget".
251. * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
252. * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
253. * ReturnIfFalse can be another formula.
254. *
255. * @access public
256. * @category Logical Functions
257. * @param mixed $condition Condition to evaluate
258. * @param mixed $returnIfTrue Value to return when condition is true
259. * @param mixed $returnIfFalse Optional value to return when condition is false
260. * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
261. */
262. public static function STATEMENT_IF($condition = true, $returnIfTrue = 0, $returnIfFalse = False) {
263. $condition = ( is_null($condition)) ? True : (boolean) PHPExcel_Calculation_Functions:: flattenSingleValue($condition);
264. $returnIfTrue = ( is_null($returnIfTrue)) ? 0 : PHPExcel_Calculation_Functions:: flattenSingleValue($returnIfTrue);
265. $returnIfFalse = ( is_null($returnIfFalse)) ? False : PHPExcel_Calculation_Functions:: flattenSingleValue($returnIfFalse);
267. return ($condition ? $returnIfTrue : $returnIfFalse);
268. } // function STATEMENT_IF()
271. /**
272. * IFERROR
273. *
274. * Excel Function:
275. * =IFERROR(testValue,errorpart)
276. *
277. * @access public
278. * @category Logical Functions
279. * @param mixed $testValue Value to check, is also the value returned when no error
280. * @param mixed $errorpart Value to return when testValue is an error condition
281. * @return mixed The value of errorpart or testValue determined by error condition
282. */
283. public static function IFERROR($testValue = '', $errorpart = '') {
284. $testValue = ( is_null($testValue)) ? '' : PHPExcel_Calculation_Functions:: flattenSingleValue($testValue);
285. $errorpart = ( is_null($errorpart)) ? '' : PHPExcel_Calculation_Functions:: flattenSingleValue($errorpart);
287. return self:: STATEMENT_IF( PHPExcel_Calculation_Functions:: IS_ERROR($testValue), $errorpart, $testValue);
288. } // function IFERROR()
290. } // class PHPExcel_Calculation_Logical

Documentation generated on Sun, 27 Feb 2011 16:32:30 -0800 by phpDocumentor 1.4.3