ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ResultFormatter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
28 {
29  private const N_DECIMALS = 1;
30  private const SCIENTIFIC_NOTATION_UPPER = 1000000000000;
31  private const SCIENTIFIC_NOTATION_LOWER = 0.000000001;
32  private \ilLanguage $lng;
33  private \ilObjUser $user;
34 
35  public function __construct()
36  {
37  global $DIC;
38  $this->lng = $DIC->language();
39  $this->user = $DIC->user();
40  }
41 
42  public function format(Result\Result $result): string
43  {
44  return match (true) {
45  $result instanceof Result\DateResult => $this->formatDateFromString($result),
46  $result instanceof Result\IntegerResult => $this->formatScientific($result),
47  $result instanceof Result\StringResult => $this->formatString($result),
48  default => $result->getValue()
49  };
50  }
51 
52  protected function formatString(Result\StringResult $result): string
53  {
54  return $result->getValue();
55  }
56 
57  protected function formatDateFromString(Result\DateResult $result): string
58  {
59  $calculated_timestamp = (int) $result->getValue();
60  switch ($result->getFromOperator()) {
61  case Operators::SUBTRACTION:
62  $presentation = function ($value, $factor, $unit) {
63  return $this->formatScientific(
64  new IntegerResult((string) round($value / $factor, 0))
65  ) . ' ' . $this->lng->txt($unit);
66  };
67 
68  $prefix = '';
69  $value = (int) $calculated_timestamp;
70  if ($value < 0) {
71  $prefix = '-';
72  $value *= -1;
73  }
74 
75  switch (true) {
76  case $value < 60:
77  $value = $presentation($value, 1, 'seconds');
78  break;
79  case $value < 3600:
80  $value = $presentation($value, 60, 'minutes');
81  break;
82  case $value < 24 * 3600:
83  $value = $presentation($value, 3600, 'hours');
84  break;
85  default:
86  $value = $presentation($value, 24 * 3600, 'days');
87  break;
88  }
89  return $prefix . $value;
90  case Operators::ADDITION:
91  case Operators::MULTIPLICATION:
92  case Operators::DIVISION:
93  case Operators::POWER:
94  return '';
95  default:
96  switch ($result->getFromFunction()) {
97  case Functions::MAX:
98  case Functions::MIN:
99  case Functions::AVERAGE:
100  return date($this->user->getDateFormat()->toString(), $calculated_timestamp);
101  case Functions::SUM:
102  default:
103  return '';
104  }
105  }
106  }
107 
108  protected function formatScientific(Result\IntegerResult $result): string
109  {
110  $value = (int) $result->getValue();
111  if (abs($value) >= self::SCIENTIFIC_NOTATION_UPPER) {
112  return sprintf("%e", $value);
113  }
114  if (abs($value) <= self::SCIENTIFIC_NOTATION_LOWER && $value != 0) {
115  return sprintf("%e", $value);
116  }
117 
118  // format numbers bigger than 1000 with thousand separator
119  if (abs($value) >= 1000) {
120  if (is_float($value)) {
121  $decimals = self::N_DECIMALS;
122  } else {
123  $decimals = 0;
124  }
125  return number_format(
126  $value,
127  $decimals,
128  $this->lng->txt('lang_sep_decimal'),
129  $this->lng->txt('lang_sep_thousand')
130  );
131  }
132 
133  return (string) $value;
134  }
135 }
global $DIC
Definition: shib_login.php:26