ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sum.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class Sum
8 {
21  public static function sumIgnoringStrings(...$args)
22  {
23  $returnValue = 0;
24 
25  // Loop through the arguments
26  foreach (Functions::flattenArray($args) as $arg) {
27  // Is it a numeric value?
28  if (is_numeric($arg)) {
29  $returnValue += $arg;
30  } elseif (Functions::isError($arg)) {
31  return $arg;
32  }
33  }
34 
35  return $returnValue;
36  }
37 
50  public static function sumErroringStrings(...$args)
51  {
52  $returnValue = 0;
53  // Loop through the arguments
54  $aArgs = Functions::flattenArrayIndexed($args);
55  foreach ($aArgs as $k => $arg) {
56  // Is it a numeric value?
57  if (is_numeric($arg) || empty($arg)) {
58  if (is_string($arg)) {
59  $arg = (int) $arg;
60  }
61  $returnValue += $arg;
62  } elseif (is_bool($arg)) {
63  $returnValue += (int) $arg;
64  } elseif (Functions::isError($arg)) {
65  return $arg;
66  // ignore non-numerics from cell, but fail as literals (except null)
67  } elseif ($arg !== null && !Functions::isCellValue($k)) {
68  return Functions::VALUE();
69  }
70  }
71 
72  return $returnValue;
73  }
74 
85  public static function product(...$args)
86  {
87  $arrayList = $args;
88 
89  $wrkArray = Functions::flattenArray(array_shift($arrayList));
90  $wrkCellCount = count($wrkArray);
91 
92  for ($i = 0; $i < $wrkCellCount; ++$i) {
93  if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
94  $wrkArray[$i] = 0;
95  }
96  }
97 
98  foreach ($arrayList as $matrixData) {
99  $array2 = Functions::flattenArray($matrixData);
100  $count = count($array2);
101  if ($wrkCellCount != $count) {
102  return Functions::VALUE();
103  }
104 
105  foreach ($array2 as $i => $val) {
106  if ((!is_numeric($val)) || (is_string($val))) {
107  $val = 0;
108  }
109  $wrkArray[$i] *= $val;
110  }
111  }
112 
113  return array_sum($wrkArray);
114  }
115 }
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static flattenArrayIndexed($array)
Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing...
Definition: Functions.php:616
static sumErroringStrings(... $args)
SUM, returning error for non-numeric strings.
Definition: Sum.php:50
$i
Definition: disco.tpl.php:19
static product(... $args)
SUMPRODUCT.
Definition: Sum.php:85
static sumIgnoringStrings(... $args)
SUM, ignoring non-numeric non-error strings.
Definition: Sum.php:21