ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Format.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
13 class Format
14 {
26  public static function DOLLAR($value = 0, $decimals = 2): string
27  {
28  $value = Functions::flattenSingleValue($value);
29  $decimals = $decimals === null ? 2 : Functions::flattenSingleValue($decimals);
30 
31  // Validate parameters
32  if (!is_numeric($value) || !is_numeric($decimals)) {
33  return Functions::VALUE();
34  }
35  $decimals = (int) $decimals;
36 
37  $mask = '$#,##0';
38  if ($decimals > 0) {
39  $mask .= '.' . str_repeat('0', $decimals);
40  } else {
41  $round = 10 ** abs($decimals);
42  if ($value < 0) {
43  $round = 0 - $round;
44  }
45  $value = MathTrig\Round::multiple($value, $round);
46  }
47  $mask = "{$mask};-{$mask}";
48 
49  return NumberFormat::toFormattedString($value, $mask);
50  }
51 
59  public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false): string
60  {
61  $value = Functions::flattenSingleValue($value);
62  $decimals = $decimals === null ? 2 : Functions::flattenSingleValue($decimals);
63  $noCommas = Functions::flattenSingleValue($noCommas);
64 
65  // Validate parameters
66  if (!is_numeric($value) || !is_numeric($decimals)) {
67  return Functions::VALUE();
68  }
69  $decimals = (float) $decimals;
70  $value = (float) $value;
71  $decimals = (int) floor($decimals);
72 
73  $valueResult = round($value, $decimals);
74  if ($decimals < 0) {
75  $decimals = 0;
76  }
77  if ($noCommas === false) {
78  $valueResult = number_format(
79  $valueResult,
80  $decimals,
83  );
84  }
85 
86  return (string) $valueResult;
87  }
88 
95  public static function TEXTFORMAT($value, $format): string
96  {
97  $value = Functions::flattenSingleValue($value);
99 
100  if ((is_string($value)) && (!is_numeric($value)) && Date::isDateTimeFormatCode($format)) {
101  $value = DateTimeExcel\DateValue::fromString($value);
102  }
103 
104  return (string) NumberFormat::toFormattedString($value, $format);
105  }
106 
114  public static function VALUE($value = '')
115  {
116  $value = Functions::flattenSingleValue($value);
117 
118  if (!is_numeric($value)) {
119  $numberValue = str_replace(
121  '',
122  trim($value, " \t\n\r\0\x0B" . StringHelper::getCurrencyCode())
123  );
124  if (is_numeric($numberValue)) {
125  return (float) $numberValue;
126  }
127 
128  $dateSetting = Functions::getReturnDateType();
130 
131  if (strpos($value, ':') !== false) {
132  $timeValue = DateTimeExcel\TimeValue::fromString($value);
133  if ($timeValue !== Functions::VALUE()) {
134  Functions::setReturnDateType($dateSetting);
135 
136  return $timeValue;
137  }
138  }
139  $dateValue = DateTimeExcel\DateValue::fromString($value);
140  if ($dateValue !== Functions::VALUE()) {
141  Functions::setReturnDateType($dateSetting);
142 
143  return $dateValue;
144  }
145  Functions::setReturnDateType($dateSetting);
146 
147  return Functions::VALUE();
148  }
149 
150  return (float) $value;
151  }
152 
162  public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null)
163  {
164  $value = Functions::flattenSingleValue($value);
165  $decimalSeparator = Functions::flattenSingleValue($decimalSeparator);
166  $groupSeparator = Functions::flattenSingleValue($groupSeparator);
167 
168  if (!is_numeric($value)) {
169  $decimalSeparator = empty($decimalSeparator) ? StringHelper::getDecimalSeparator() : $decimalSeparator;
170  $groupSeparator = empty($groupSeparator) ? StringHelper::getThousandsSeparator() : $groupSeparator;
171 
172  $decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator) . '/', $value, $matches, PREG_OFFSET_CAPTURE);
173  if ($decimalPositions > 1) {
174  return Functions::VALUE();
175  }
176  $decimalOffset = array_pop($matches[0])[1];
177  if (strpos($value, $groupSeparator, $decimalOffset) !== false) {
178  return Functions::VALUE();
179  }
180 
181  $value = str_replace([$groupSeparator, $decimalSeparator], ['', '.'], $value);
182 
183  // Handle the special case of trailing % signs
184  $percentageString = rtrim($value, '%');
185  if (!is_numeric($percentageString)) {
186  return Functions::VALUE();
187  }
188 
189  $percentageAdjustment = strlen($value) - strlen($percentageString);
190  if ($percentageAdjustment) {
191  $value = (float) $percentageString;
192  $value /= 10 ** ($percentageAdjustment * 2);
193  }
194  }
195 
196  return (float) $value;
197  }
198 }
static DOLLAR($value=0, $decimals=2)
DOLLAR.
Definition: Format.php:26
static NUMBERVALUE($value='', $decimalSeparator=null, $groupSeparator=null)
NUMBERVALUE.
Definition: Format.php:162
$format
Definition: metadata.php:141
static getReturnDateType()
Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object).
Definition: Functions.php:133
static toFormattedString($value, $format, $callBack=null)
Convert a value in a pre-defined format to a PHP string.
static setReturnDateType($returnDateType)
Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric o...
Definition: Functions.php:109
static getCurrencyCode()
Get the currency code.
$mask
Definition: example_042.php:90
static multiple($number, $multiple)
MROUND.
Definition: Round.php:102
static TEXTFORMAT($value, $format)
TEXTFORMAT.
Definition: Format.php:95
static FIXEDFORMAT($value, $decimals=2, $noCommas=false)
FIXEDFORMAT.
Definition: Format.php:59
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static getThousandsSeparator()
Get the thousands separator.
static getDecimalSeparator()
Get the decimal separator.