ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Operations.php
Go to the documentation of this file.
1<?php
2
4
7
9{
30 public static function logicalAnd(...$args)
31 {
32 $args = Functions::flattenArray($args);
33
34 if (count($args) == 0) {
35 return Functions::VALUE();
36 }
37
38 $args = array_filter($args, function ($value) {
39 return $value !== null || (is_string($value) && trim($value) == '');
40 });
41
42 $returnValue = self::countTrueValues($args);
43 if (is_string($returnValue)) {
44 return $returnValue;
45 }
46 $argCount = count($args);
47
48 return ($returnValue > 0) && ($returnValue == $argCount);
49 }
50
71 public static function logicalOr(...$args)
72 {
73 $args = Functions::flattenArray($args);
74
75 if (count($args) == 0) {
76 return Functions::VALUE();
77 }
78
79 $args = array_filter($args, function ($value) {
80 return $value !== null || (is_string($value) && trim($value) == '');
81 });
82
83 $returnValue = self::countTrueValues($args);
84 if (is_string($returnValue)) {
85 return $returnValue;
86 }
87
88 return $returnValue > 0;
89 }
90
113 public static function logicalXor(...$args)
114 {
115 $args = Functions::flattenArray($args);
116
117 if (count($args) == 0) {
118 return Functions::VALUE();
119 }
120
121 $args = array_filter($args, function ($value) {
122 return $value !== null || (is_string($value) && trim($value) == '');
123 });
124
125 $returnValue = self::countTrueValues($args);
126 if (is_string($returnValue)) {
127 return $returnValue;
128 }
129
130 return $returnValue % 2 == 1;
131 }
132
152 public static function NOT($logical = false)
153 {
154 $logical = Functions::flattenSingleValue($logical);
155
156 if (is_string($logical)) {
157 $logical = mb_strtoupper($logical, 'UTF-8');
158 if (($logical == 'TRUE') || ($logical == Calculation::getTRUE())) {
159 return false;
160 } elseif (($logical == 'FALSE') || ($logical == Calculation::getFALSE())) {
161 return true;
162 }
163
164 return Functions::VALUE();
165 }
166
167 return !$logical;
168 }
169
173 private static function countTrueValues(array $args)
174 {
175 $trueValueCount = 0;
176
177 foreach ($args as $arg) {
178 // Is it a boolean value?
179 if (is_bool($arg)) {
180 $trueValueCount += $arg;
181 } elseif ((is_numeric($arg)) && (!is_string($arg))) {
182 $trueValueCount += ((int) $arg != 0);
183 } elseif (is_string($arg)) {
184 $arg = mb_strtoupper($arg, 'UTF-8');
185 if (($arg == 'TRUE') || ($arg == Calculation::getTRUE())) {
186 $arg = true;
187 } elseif (($arg == 'FALSE') || ($arg == Calculation::getFALSE())) {
188 $arg = false;
189 } else {
190 return Functions::VALUE();
191 }
192 $trueValueCount += ($arg != 0);
193 }
194 }
195
196 return $trueValueCount;
197 }
198}
An exception for terminatinating execution or to throw for unit testing.
static getTRUE()
Return the locale-specific translation of TRUE.
static getFALSE()
Return the locale-specific translation of FALSE.
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649