1<?php
2/**
3 * PHPExcel
4 *
5 * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license		http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
25 * @version		##VERSION##, ##DATE##
26 */
27
28
29/** PHPExcel root directory */
30if (!defined('PHPEXCEL_ROOT')) {
31	/**
32	 * @ignore
33	 */
34	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36}
37
38
39/**
40 * PHPExcel_Calculation_Logical
41 *
42 * @category	PHPExcel
43 * @package		PHPExcel_Calculation
44 * @copyright	Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
45 */
46class PHPExcel_Calculation_Logical {
47
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()
63
64
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()
80
81
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;
106
107		// Loop through the arguments
108		$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
109		$argCount = -1;
110		foreach ($aArgs as $argCount => $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		}
128
129		// Return
130		if ($argCount < 0) {
131			return PHPExcel_Calculation_Functions::VALUE();
132		}
133		return $returnValue;
134	}	//	function LOGICAL_AND()
135
136
137	/**
138	 * LOGICAL_OR
139	 *
140	 * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
141	 *
142	 * Excel Function:
143	 *		=OR(logical1[,logical2[, ...]])
144	 *
145	 *		The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
146	 *			or references that contain logical values.
147	 *
148	 *		Boolean arguments are treated as True or False as appropriate
149	 *		Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
150	 *		If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
151	 *			the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
152	 *
153	 * @access	public
154	 * @category Logical Functions
155	 * @param	mixed		$arg,...		Data values
156	 * @return	boolean		The logical OR of the arguments.
157	 */
158	public static function LOGICAL_OR() {
159		// Return value
160		$returnValue = FALSE;
161
162		// Loop through the arguments
163		$aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
164		$argCount = -1;
165		foreach ($aArgs as $argCount => $arg) {
166			// Is it a boolean value?
167			if (is_bool($arg)) {
168				$returnValue = $returnValue || $arg;
169			} elseif ((is_numeric($arg)) && (!is_string($arg))) {
170				$returnValue = $returnValue || ($arg != 0);
171			} elseif (is_string($arg)) {
172				$arg = strtoupper($arg);
173				if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
174					$arg = TRUE;
175				} elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
176					$arg = FALSE;
177				} else {
178					return PHPExcel_Calculation_Functions::VALUE();
179				}
180				$returnValue = $returnValue || ($arg != 0);
181			}
182		}
183
184		// Return
185		if ($argCount < 0) {
186			return PHPExcel_Calculation_Functions::VALUE();
187		}
188		return $returnValue;
189	}	//	function LOGICAL_OR()
190
191
192	/**
193	 * NOT
194	 *
195	 * Returns the boolean inverse of the argument.
196	 *
197	 * Excel Function:
198	 *		=NOT(logical)
199	 *
200	 *		The argument must evaluate to a logical value such as TRUE or FALSE
201	 *
202	 *		Boolean arguments are treated as True or False as appropriate
203	 *		Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
204	 *		If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
205	 *			the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
206	 *
207	 * @access	public
208	 * @category Logical Functions
209	 * @param	mixed		$logical	A value or expression that can be evaluated to TRUE or FALSE
210	 * @return	boolean		The boolean inverse of the argument.
211	 */
212	public static function NOT($logical=FALSE) {
213		$logical = PHPExcel_Calculation_Functions::flattenSingleValue($logical);
214		if (is_string($logical)) {
215			$logical = strtoupper($logical);
216			if (($logical == 'TRUE') || ($logical == PHPExcel_Calculation::getTRUE())) {
217				return FALSE;
218			} elseif (($logical == 'FALSE') || ($logical == PHPExcel_Calculation::getFALSE())) {
219				return TRUE;
220			} else {
221				return PHPExcel_Calculation_Functions::VALUE();
222			}
223		}
224
225		return !$logical;
226	}	//	function NOT()
227
228	/**
229	 * STATEMENT_IF
230	 *
231	 * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
232	 *
233	 * Excel Function:
234	 *		=IF(condition[,returnIfTrue[,returnIfFalse]])
235	 *
236	 *		Condition is any value or expression that can be evaluated to TRUE or FALSE.
237	 *			For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
238	 *			the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
239	 *			This argument can use any comparison calculation operator.
240	 *		ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
241	 *			For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
242	 *			then the IF function returns the text "Within budget"
243	 *			If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
244	 *			the logical value TRUE for this argument.
245	 *			ReturnIfTrue can be another formula.
246	 *		ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
247	 *			For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
248	 *			then the IF function returns the text "Over budget".
249	 *			If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
250	 *			If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
251	 *			ReturnIfFalse can be another formula.
252	 *
253	 * @access	public
254	 * @category Logical Functions
255	 * @param	mixed	$condition		Condition to evaluate
256	 * @param	mixed	$returnIfTrue	Value to return when condition is true
257	 * @param	mixed	$returnIfFalse	Optional value to return when condition is false
258	 * @return	mixed	The value of returnIfTrue or returnIfFalse determined by condition
259	 */
260	public static function STATEMENT_IF($condition = TRUE, $returnIfTrue = 0, $returnIfFalse = FALSE) {
261		$condition		= (is_null($condition))		? TRUE :	(boolean) PHPExcel_Calculation_Functions::flattenSingleValue($condition);
262		$returnIfTrue	= (is_null($returnIfTrue))	? 0 :		PHPExcel_Calculation_Functions::flattenSingleValue($returnIfTrue);
263		$returnIfFalse	= (is_null($returnIfFalse))	? FALSE :	PHPExcel_Calculation_Functions::flattenSingleValue($returnIfFalse);
264
265		return ($condition) ? $returnIfTrue : $returnIfFalse;
266	}	//	function STATEMENT_IF()
267
268
269	/**
270	 * IFERROR
271	 *
272	 * Excel Function:
273	 *		=IFERROR(testValue,errorpart)
274	 *
275	 * @access	public
276	 * @category Logical Functions
277	 * @param	mixed	$testValue	Value to check, is also the value returned when no error
278	 * @param	mixed	$errorpart	Value to return when testValue is an error condition
279	 * @return	mixed	The value of errorpart or testValue determined by error condition
280	 */
281	public static function IFERROR($testValue = '', $errorpart = '') {
282		$testValue	= (is_null($testValue))	? '' :	PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
283		$errorpart	= (is_null($errorpart))	? '' :	PHPExcel_Calculation_Functions::flattenSingleValue($errorpart);
284
285		return self::STATEMENT_IF(PHPExcel_Calculation_Functions::IS_ERROR($testValue), $errorpart, $testValue);
286	}	//	function IFERROR()
287
288}	//	class PHPExcel_Calculation_Logical
289