@@ -33,23 +33,9 @@ class Operations
33
33
*/
34
34
public static function logicalAnd (...$ args )
35
35
{
36
- $ args = Functions::flattenArray ($ args );
37
-
38
- if (count ($ args ) == 0 ) {
39
- return ExcelError::VALUE ();
40
- }
41
-
42
- $ args = array_filter ($ args , function ($ value ) {
43
- return $ value !== null || (is_string ($ value ) && trim ($ value ) == '' );
36
+ return self ::countTrueValues ($ args , function (int $ trueValueCount , int $ count ): bool {
37
+ return $ trueValueCount === $ count ;
44
38
});
45
-
46
- $ returnValue = self ::countTrueValues ($ args );
47
- if (is_string ($ returnValue )) {
48
- return $ returnValue ;
49
- }
50
- $ argCount = count ($ args );
51
-
52
- return ($ returnValue > 0 ) && ($ returnValue == $ argCount );
53
39
}
54
40
55
41
/**
@@ -74,22 +60,9 @@ public static function logicalAnd(...$args)
74
60
*/
75
61
public static function logicalOr (...$ args )
76
62
{
77
- $ args = Functions::flattenArray ($ args );
78
-
79
- if (count ($ args ) == 0 ) {
80
- return ExcelError::VALUE ();
81
- }
82
-
83
- $ args = array_filter ($ args , function ($ value ) {
84
- return $ value !== null || (is_string ($ value ) && trim ($ value ) == '' );
63
+ return self ::countTrueValues ($ args , function (int $ trueValueCount ): bool {
64
+ return $ trueValueCount > 0 ;
85
65
});
86
-
87
- $ returnValue = self ::countTrueValues ($ args );
88
- if (is_string ($ returnValue )) {
89
- return $ returnValue ;
90
- }
91
-
92
- return $ returnValue > 0 ;
93
66
}
94
67
95
68
/**
@@ -116,22 +89,9 @@ public static function logicalOr(...$args)
116
89
*/
117
90
public static function logicalXor (...$ args )
118
91
{
119
- $ args = Functions::flattenArray ($ args );
120
-
121
- if (count ($ args ) == 0 ) {
122
- return ExcelError::VALUE ();
123
- }
124
-
125
- $ args = array_filter ($ args , function ($ value ) {
126
- return $ value !== null || (is_string ($ value ) && trim ($ value ) == '' );
92
+ return self ::countTrueValues ($ args , function (int $ trueValueCount ): bool {
93
+ return $ trueValueCount % 2 === 1 ;
127
94
});
128
-
129
- $ returnValue = self ::countTrueValues ($ args );
130
- if (is_string ($ returnValue )) {
131
- return $ returnValue ;
132
- }
133
-
134
- return $ returnValue % 2 == 1 ;
135
95
}
136
96
137
97
/**
@@ -177,31 +137,36 @@ public static function NOT($logical = false)
177
137
}
178
138
179
139
/**
180
- * @return int |string
140
+ * @return bool |string
181
141
*/
182
- private static function countTrueValues (array $ args )
142
+ private static function countTrueValues (array $ args, callable $ func )
183
143
{
184
144
$ trueValueCount = 0 ;
145
+ $ count = 0 ;
185
146
186
- foreach ($ args as $ arg ) {
147
+ $ aArgs = Functions::flattenArrayIndexed ($ args );
148
+ foreach ($ aArgs as $ k => $ arg ) {
149
+ ++$ count ;
187
150
// Is it a boolean value?
188
151
if (is_bool ($ arg )) {
189
152
$ trueValueCount += $ arg ;
190
- } elseif ((is_numeric ($ arg )) && (!is_string ($ arg ))) {
191
- $ trueValueCount += ((int ) $ arg != 0 );
192
153
} elseif (is_string ($ arg )) {
154
+ $ isLiteral = !Functions::isCellValue ($ k );
193
155
$ arg = mb_strtoupper ($ arg , 'UTF-8 ' );
194
- if (($ arg == 'TRUE ' ) || ( $ arg == Calculation::getTRUE ())) {
195
- $ arg = true ;
196
- } elseif (($ arg == 'FALSE ' ) || ( $ arg == Calculation::getFALSE ())) {
197
- $ arg = false ;
156
+ if ($ isLiteral && ($ arg == 'TRUE ' || $ arg == Calculation::getTRUE ())) {
157
+ ++ $ trueValueCount ;
158
+ } elseif ($ isLiteral && ($ arg == 'FALSE ' || $ arg == Calculation::getFALSE ())) {
159
+ //$trueValueCount += 0 ;
198
160
} else {
199
- return ExcelError:: VALUE () ;
161
+ -- $ count ;
200
162
}
201
- $ trueValueCount += ($ arg != 0 );
163
+ } elseif (is_int ($ arg ) || is_float ($ arg )) {
164
+ $ trueValueCount += (int ) ($ arg != 0 );
165
+ } else {
166
+ --$ count ;
202
167
}
203
168
}
204
169
205
- return $ trueValueCount ;
170
+ return ( $ count === 0 ) ? ExcelError:: VALUE () : $ func ( $ trueValueCount, $ count ) ;
206
171
}
207
172
}
0 commit comments