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