ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Functions.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Functions
9 {
10  const PRECISION = 8.88E-016;
11 
15  const M_2DIVPI = 0.63661977236758134307553505349006;
16 
18  const COMPATIBILITY_EXCEL = 'Excel';
19  const COMPATIBILITY_GNUMERIC = 'Gnumeric';
20  const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
21 
24  const RETURNDATE_PHP_OBJECT = 'O';
26  const RETURNDATE_EXCEL = 'E';
27 
33  protected static $compatibilityMode = self::COMPATIBILITY_EXCEL;
34 
40  protected static $returnDateType = self::RETURNDATE_EXCEL;
41 
47  protected static $errorCodes = [
48  'null' => '#NULL!',
49  'divisionbyzero' => '#DIV/0!',
50  'value' => '#VALUE!',
51  'reference' => '#REF!',
52  'name' => '#NAME?',
53  'num' => '#NUM!',
54  'na' => '#N/A',
55  'gettingdata' => '#GETTING_DATA',
56  ];
57 
69  public static function setCompatibilityMode($compatibilityMode)
70  {
71  if (
72  ($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
73  ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
74  ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)
75  ) {
76  self::$compatibilityMode = $compatibilityMode;
77 
78  return true;
79  }
80 
81  return false;
82  }
83 
93  public static function getCompatibilityMode()
94  {
95  return self::$compatibilityMode;
96  }
97 
109  public static function setReturnDateType($returnDateType)
110  {
111  if (
112  ($returnDateType == self::RETURNDATE_UNIX_TIMESTAMP) ||
113  ($returnDateType == self::RETURNDATE_PHP_DATETIME_OBJECT) ||
114  ($returnDateType == self::RETURNDATE_EXCEL)
115  ) {
116  self::$returnDateType = $returnDateType;
117 
118  return true;
119  }
120 
121  return false;
122  }
123 
133  public static function getReturnDateType()
134  {
135  return self::$returnDateType;
136  }
137 
143  public static function DUMMY()
144  {
145  return '#Not Yet Implemented';
146  }
147 
153  public static function DIV0()
154  {
155  return self::$errorCodes['divisionbyzero'];
156  }
157 
169  public static function NA()
170  {
171  return self::$errorCodes['na'];
172  }
173 
181  public static function NAN()
182  {
183  return self::$errorCodes['num'];
184  }
185 
193  public static function NAME()
194  {
195  return self::$errorCodes['name'];
196  }
197 
205  public static function REF()
206  {
207  return self::$errorCodes['reference'];
208  }
209 
217  public static function null()
218  {
219  return self::$errorCodes['null'];
220  }
221 
229  public static function VALUE()
230  {
231  return self::$errorCodes['value'];
232  }
233 
234  public static function isMatrixValue($idx)
235  {
236  return (substr_count($idx, '.') <= 1) || (preg_match('/\.[A-Z]/', $idx) > 0);
237  }
238 
239  public static function isValue($idx)
240  {
241  return substr_count($idx, '.') == 0;
242  }
243 
244  public static function isCellValue($idx)
245  {
246  return substr_count($idx, '.') > 1;
247  }
248 
249  public static function ifCondition($condition)
250  {
251  $condition = self::flattenSingleValue($condition);
252 
253  if ($condition === '') {
254  $condition = '=""';
255  }
256  if (!is_string($condition) || !in_array($condition[0], ['>', '<', '='])) {
257  $condition = self::operandSpecialHandling($condition);
258  if (is_bool($condition)) {
259  return '=' . ($condition ? 'TRUE' : 'FALSE');
260  } elseif (!is_numeric($condition)) {
261  $condition = Calculation::wrapResult(strtoupper($condition));
262  }
263 
264  return str_replace('""""', '""', '=' . $condition);
265  }
266  preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
267  [, $operator, $operand] = $matches;
268 
269  $operand = self::operandSpecialHandling($operand);
270  if (is_numeric(trim($operand, '"'))) {
271  $operand = trim($operand, '"');
272  } elseif (!is_numeric($operand) && $operand !== 'FALSE' && $operand !== 'TRUE') {
273  $operand = str_replace('"', '""', $operand);
274  $operand = Calculation::wrapResult(strtoupper($operand));
275  }
276 
277  return str_replace('""""', '""', $operator . $operand);
278  }
279 
280  private static function operandSpecialHandling($operand)
281  {
282  if (is_numeric($operand) || is_bool($operand)) {
283  return $operand;
284  } elseif (strtoupper($operand) === Calculation::getTRUE() || strtoupper($operand) === Calculation::getFALSE()) {
285  return strtoupper($operand);
286  }
287 
288  // Check for percentage
289  if (preg_match('/^\-?\d*\.?\d*\s?\%$/', $operand)) {
290  return ((float) rtrim($operand, '%')) / 100;
291  }
292 
293  // Check for dates
294  if (($dateValueOperand = Date::stringToExcel($operand)) !== false) {
295  return $dateValueOperand;
296  }
297 
298  return $operand;
299  }
300 
308  public static function errorType($value = '')
309  {
310  $value = self::flattenSingleValue($value);
311 
312  $i = 1;
313  foreach (self::$errorCodes as $errorCode) {
314  if ($value === $errorCode) {
315  return $i;
316  }
317  ++$i;
318  }
319 
320  return self::NA();
321  }
322 
330  public static function isBlank($value = null)
331  {
332  if ($value !== null) {
333  $value = self::flattenSingleValue($value);
334  }
335 
336  return $value === null;
337  }
338 
346  public static function isErr($value = '')
347  {
348  $value = self::flattenSingleValue($value);
349 
350  return self::isError($value) && (!self::isNa(($value)));
351  }
352 
360  public static function isError($value = '')
361  {
362  $value = self::flattenSingleValue($value);
363 
364  if (!is_string($value)) {
365  return false;
366  }
367 
368  return in_array($value, self::$errorCodes);
369  }
370 
378  public static function isNa($value = '')
379  {
380  $value = self::flattenSingleValue($value);
381 
382  return $value === self::NA();
383  }
384 
392  public static function isEven($value = null)
393  {
394  $value = self::flattenSingleValue($value);
395 
396  if ($value === null) {
397  return self::NAME();
398  } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
399  return self::VALUE();
400  }
401 
402  return $value % 2 == 0;
403  }
404 
412  public static function isOdd($value = null)
413  {
414  $value = self::flattenSingleValue($value);
415 
416  if ($value === null) {
417  return self::NAME();
418  } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
419  return self::VALUE();
420  }
421 
422  return abs($value) % 2 == 1;
423  }
424 
432  public static function isNumber($value = null)
433  {
434  $value = self::flattenSingleValue($value);
435 
436  if (is_string($value)) {
437  return false;
438  }
439 
440  return is_numeric($value);
441  }
442 
450  public static function isLogical($value = null)
451  {
452  $value = self::flattenSingleValue($value);
453 
454  return is_bool($value);
455  }
456 
464  public static function isText($value = null)
465  {
466  $value = self::flattenSingleValue($value);
467 
468  return is_string($value) && !self::isError($value);
469  }
470 
478  public static function isNonText($value = null)
479  {
480  return !self::isText($value);
481  }
482 
499  public static function n($value = null)
500  {
501  while (is_array($value)) {
502  $value = array_shift($value);
503  }
504 
505  switch (gettype($value)) {
506  case 'double':
507  case 'float':
508  case 'integer':
509  return $value;
510  case 'boolean':
511  return (int) $value;
512  case 'string':
513  // Errors
514  if ((strlen($value) > 0) && ($value[0] == '#')) {
515  return $value;
516  }
517 
518  break;
519  }
520 
521  return 0;
522  }
523 
539  public static function TYPE($value = null)
540  {
541  $value = self::flattenArrayIndexed($value);
542  if (is_array($value) && (count($value) > 1)) {
543  end($value);
544  $a = key($value);
545  // Range of cells is an error
546  if (self::isCellValue($a)) {
547  return 16;
548  // Test for Matrix
549  } elseif (self::isMatrixValue($a)) {
550  return 64;
551  }
552  } elseif (empty($value)) {
553  // Empty Cell
554  return 1;
555  }
556  $value = self::flattenSingleValue($value);
557 
558  if (($value === null) || (is_float($value)) || (is_int($value))) {
559  return 1;
560  } elseif (is_bool($value)) {
561  return 4;
562  } elseif (is_array($value)) {
563  return 64;
564  } elseif (is_string($value)) {
565  // Errors
566  if ((strlen($value) > 0) && ($value[0] == '#')) {
567  return 16;
568  }
569 
570  return 2;
571  }
572 
573  return 0;
574  }
575 
583  public static function flattenArray($array)
584  {
585  if (!is_array($array)) {
586  return (array) $array;
587  }
588 
589  $arrayValues = [];
590  foreach ($array as $value) {
591  if (is_array($value)) {
592  foreach ($value as $val) {
593  if (is_array($val)) {
594  foreach ($val as $v) {
595  $arrayValues[] = $v;
596  }
597  } else {
598  $arrayValues[] = $val;
599  }
600  }
601  } else {
602  $arrayValues[] = $value;
603  }
604  }
605 
606  return $arrayValues;
607  }
608 
616  public static function flattenArrayIndexed($array)
617  {
618  if (!is_array($array)) {
619  return (array) $array;
620  }
621 
622  $arrayValues = [];
623  foreach ($array as $k1 => $value) {
624  if (is_array($value)) {
625  foreach ($value as $k2 => $val) {
626  if (is_array($val)) {
627  foreach ($val as $k3 => $v) {
628  $arrayValues[$k1 . '.' . $k2 . '.' . $k3] = $v;
629  }
630  } else {
631  $arrayValues[$k1 . '.' . $k2] = $val;
632  }
633  }
634  } else {
635  $arrayValues[$k1] = $value;
636  }
637  }
638 
639  return $arrayValues;
640  }
641 
649  public static function flattenSingleValue($value = '')
650  {
651  while (is_array($value)) {
652  $value = array_shift($value);
653  }
654 
655  return $value;
656  }
657 
666  public static function isFormula($cellReference = '', ?Cell $pCell = null)
667  {
668  if ($pCell === null) {
669  return self::REF();
670  }
671 
672  preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches);
673 
674  $cellReference = $matches[6] . $matches[7];
675  $worksheetName = str_replace("''", "'", trim($matches[2], "'"));
676 
677  $worksheet = (!empty($worksheetName))
678  ? $pCell->getWorksheet()->getParent()->getSheetByName($worksheetName)
679  : $pCell->getWorksheet();
680 
681  return $worksheet->getCell($cellReference)->isFormula();
682  }
683 }
static isNumber($value=null)
IS_NUMBER.
Definition: Functions.php:432
static isFormula($cellReference='', ?Cell $pCell=null)
ISFORMULA.
Definition: Functions.php:666
static isEven($value=null)
IS_EVEN.
Definition: Functions.php:392
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 wrapResult($value)
Wrap string values in quotes.
static isBlank($value=null)
IS_BLANK.
Definition: Functions.php:330
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static errorType($value='')
ERROR_TYPE.
Definition: Functions.php:308
static isLogical($value=null)
IS_LOGICAL.
Definition: Functions.php:450
static static setCompatibilityMode($compatibilityMode)
Set the Compatibility Mode.
Definition: Functions.php:69
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 getFALSE()
Return the locale-specific translation of FALSE.
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 getTRUE()
Return the locale-specific translation of TRUE.
$errorCode
$i
Definition: disco.tpl.php:19
static isText($value=null)
IS_TEXT.
Definition: Functions.php:464
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static isNonText($value=null)
IS_NONTEXT.
Definition: Functions.php:478
static getCompatibilityMode()
Return the current Compatibility Mode.
Definition: Functions.php:93