ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Helpers.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use DateTime;
9 
10 class Helpers
11 {
19  public static function isLeapYear($year): bool
20  {
21  return (($year % 4) === 0) && (($year % 100) !== 0) || (($year % 400) === 0);
22  }
23 
31  public static function getDateValue($dateValue, bool $allowBool = true): float
32  {
33  if (is_object($dateValue)) {
34  $retval = SharedDateHelper::PHPToExcel($dateValue);
35  if (is_bool($retval)) {
36  throw new Exception(Functions::VALUE());
37  }
38 
39  return $retval;
40  }
41 
42  self::nullFalseTrueToNumber($dateValue, $allowBool);
43  if (!is_numeric($dateValue)) {
44  $saveReturnDateType = Functions::getReturnDateType();
46  $dateValue = DateValue::fromString($dateValue);
47  Functions::setReturnDateType($saveReturnDateType);
48  if (!is_numeric($dateValue)) {
49  throw new Exception(Functions::VALUE());
50  }
51  }
53  throw new Exception(Functions::NAN());
54  }
55 
56  return (float) $dateValue;
57  }
58 
66  public static function getTimeValue($timeValue)
67  {
68  $saveReturnDateType = Functions::getReturnDateType();
70  $timeValue = TimeValue::fromString($timeValue);
71  Functions::setReturnDateType($saveReturnDateType);
72 
73  return $timeValue;
74  }
75 
81  public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0): DateTime
82  {
83  // Execute function
84  $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
85  $oMonth = (int) $PHPDateObject->format('m');
86  $oYear = (int) $PHPDateObject->format('Y');
87 
88  $adjustmentMonthsString = (string) $adjustmentMonths;
89  if ($adjustmentMonths > 0) {
90  $adjustmentMonthsString = '+' . $adjustmentMonths;
91  }
92  if ($adjustmentMonths != 0) {
93  $PHPDateObject->modify($adjustmentMonthsString . ' months');
94  }
95  $nMonth = (int) $PHPDateObject->format('m');
96  $nYear = (int) $PHPDateObject->format('Y');
97 
98  $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
99  if ($monthDiff != $adjustmentMonths) {
100  $adjustDays = (int) $PHPDateObject->format('d');
101  $adjustDaysString = '-' . $adjustDays . ' days';
102  $PHPDateObject->modify($adjustDaysString);
103  }
104 
105  return $PHPDateObject;
106  }
107 
114  public static function replaceIfEmpty(&$value, $altValue): void
115  {
116  $value = $value ?: $altValue;
117  }
118 
122  public static function adjustYear(string $testVal1, string $testVal2, string &$testVal3): void
123  {
124  if (!is_numeric($testVal1) || $testVal1 < 31) {
125  if (!is_numeric($testVal2) || $testVal2 < 12) {
126  if (is_numeric($testVal3) && $testVal3 < 12) {
127  $testVal3 += 2000;
128  }
129  }
130  }
131  }
132 
138  public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = false)
139  {
140  $retType = Functions::getReturnDateType();
142  return new DateTime(
143  $dateArray['year']
144  . '-' . $dateArray['month']
145  . '-' . $dateArray['day']
146  . ' ' . $dateArray['hour']
147  . ':' . $dateArray['minute']
148  . ':' . $dateArray['second']
149  );
150  }
151  $excelDateValue =
152  SharedDateHelper::formattedPHPToExcel(
153  $dateArray['year'],
154  $dateArray['month'],
155  $dateArray['day'],
156  $dateArray['hour'],
157  $dateArray['minute'],
158  $dateArray['second']
159  );
160  if ($retType === Functions::RETURNDATE_EXCEL) {
161  return $noFrac ? floor($excelDateValue) : (float) $excelDateValue;
162  }
163  // RETURNDATE_UNIX_TIMESTAMP)
164 
165  return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
166  }
167 
173  public static function returnIn3FormatsFloat(float $excelDateValue)
174  {
175  $retType = Functions::getReturnDateType();
176  if ($retType === Functions::RETURNDATE_EXCEL) {
177  return $excelDateValue;
178  }
179  if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
180  return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
181  }
182  // RETURNDATE_PHP_DATETIME_OBJECT
183 
184  return SharedDateHelper::excelToDateTimeObject($excelDateValue);
185  }
186 
192  public static function returnIn3FormatsObject(DateTime $PHPDateObject)
193  {
194  $retType = Functions::getReturnDateType();
196  return $PHPDateObject;
197  }
198  if ($retType === Functions::RETURNDATE_EXCEL) {
199  return (float) SharedDateHelper::PHPToExcel($PHPDateObject);
200  }
201  // RETURNDATE_UNIX_TIMESTAMP
202  $stamp = SharedDateHelper::PHPToExcel($PHPDateObject);
203  $stamp = is_bool($stamp) ? ((int) $stamp) : $stamp;
204 
205  return (int) SharedDateHelper::excelToTimestamp($stamp);
206  }
207 
208  private static function baseDate(): int
209  {
211  return 0;
212  }
213  if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904) {
214  return 0;
215  }
216 
217  return 1;
218  }
219 
225  public static function nullFalseTrueToNumber(&$number, bool $allowBool = true): void
226  {
227  $number = Functions::flattenSingleValue($number);
228  $nullVal = self::baseDate();
229  if ($number === null) {
230  $number = $nullVal;
231  } elseif ($allowBool && is_bool($number)) {
232  $number = $nullVal + (int) $number;
233  }
234  }
235 
243  public static function validateNumericNull($number)
244  {
245  $number = Functions::flattenSingleValue($number);
246  if ($number === null) {
247  return 0;
248  }
249  if (is_int($number)) {
250  return $number;
251  }
252  if (is_numeric($number)) {
253  return (float) $number;
254  }
255 
256  throw new Exception(Functions::VALUE());
257  }
258 
266  public static function validateNotNegative($number)
267  {
268  if (!is_numeric($number)) {
269  throw new Exception(Functions::VALUE());
270  }
271  if ($number >= 0) {
272  return (float) $number;
273  }
274 
275  throw new Exception(Functions::NAN());
276  }
277 
278  public static function silly1900(DateTime $PHPDateObject, string $mod = '-1 day'): void
279  {
280  $isoDate = $PHPDateObject->format('c');
281  if ($isoDate < '1900-03-01') {
282  $PHPDateObject->modify($mod);
283  }
284  }
285 }
static replaceIfEmpty(&$value, $altValue)
Help reduce perceived complexity of some tests.
Definition: Helpers.php:114
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 getTimeValue($timeValue)
getTimeValue.
Definition: Helpers.php:66
static nullFalseTrueToNumber(&$number, bool $allowBool=true)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:225
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 returnIn3FormatsFloat(float $excelDateValue)
Return result in one of three formats.
Definition: Helpers.php:173
static adjustYear(string $testVal1, string $testVal2, string &$testVal3)
Adjust year in ambiguous situations.
Definition: Helpers.php:122
static returnIn3FormatsObject(DateTime $PHPDateObject)
Return result in one of three formats.
Definition: Helpers.php:192
static validateNotNegative($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:266
static adjustDateByMonths($dateValue=0, float $adjustmentMonths=0)
Adjust date by given months.
Definition: Helpers.php:81
static isLeapYear($year)
Identify if a year is a leap year or not.
Definition: Helpers.php:19
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
Definition: Helpers.php:31
static silly1900(DateTime $PHPDateObject, string $mod='-1 day')
Definition: Helpers.php:278
static validateNumericNull($number)
Many functions accept null argument treated as 0.
Definition: Helpers.php:243
static returnIn3FormatsArray(array $dateArray, bool $noFrac=false)
Return result in one of three formats.
Definition: Helpers.php:138
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static getCompatibilityMode()
Return the current Compatibility Mode.
Definition: Functions.php:93