ILIAS  release_7 Revision v7.30-3-g800a261c036
IntegerTransformation.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 2020 Luka K. A. Stocker, Extended GPL, see docs/LICENSE */
4 
6 
11 
13 {
14  const REG_INT = '/^\s*(0|(-?[1-9]\d*))\s*$/';
15 
18 
22  public function transform($from)
23  {
24  if (is_int($from)) {
25  return $from;
26  }
27 
28  if (is_float($from) && !is_nan($from) && $from !== INF && $from !== -INF) {
29  $from = round($from);
30  return intval($from);
31  }
32 
33  if (is_bool($from)) {
34  return (int) $from;
35  }
36 
37  if (is_string($from) && preg_match(self::REG_INT, $from)) {
38  $int = intval($from);
39  // This is supposed to guard against PHP_MIN_INT and PHP_MAX_INT.
40  // We only return the value if it looks the same when transforming it
41  // back to string. This won't be the case for too big or too small
42  // values.
43  if (trim($from) === (string) $int) {
44  return $int;
45  }
46  }
47 
49  sprintf('The value "%s" can not be transformed into an integer', var_export($from, true)),
50  'not_integer',
51  $from
52  );
53  }
54 }
A transformation is a function from one datatype to another.
transform($from)
Perform the transformation.Please use this for transformations. It&#39;s more performant than calling inv...