ILIAS  release_7 Revision v7.30-3-g800a261c036
StringTransformation.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 BOOL_TRUE = true;
15  const BOOL_FALSE = false;
16  const BOOL_TRUE_NUMBER = 1;
17  const BOOL_FALSE_NUMBER = 0;
18  const BOOL_TRUE_STRING = 'true';
19  const BOOL_FALSE_STRING = 'false';
20 
23 
27  public function transform($from)
28  {
29  if (is_int($from) || is_float($from) || is_double($from)) {
30  return strval($from);
31  }
32 
33  if (is_bool($from) || $from === self::BOOL_TRUE_NUMBER || $from === self::BOOL_FALSE_NUMBER) {
34  if ($from === self::BOOL_TRUE || $from === self::BOOL_TRUE_NUMBER) {
35  return self::BOOL_TRUE_STRING;
36  }
37  if ($from === self::BOOL_FALSE || $from === self::BOOL_FALSE_NUMBER) {
38  return self::BOOL_FALSE_STRING;
39  }
40  }
41 
42  if (is_string($from)) {
43  return $from;
44  }
45 
46  if (is_object($from) && method_exists($from, '__toString')) {
47  return (string) $from;
48  }
49 
51  sprintf('The value "%s" could not be transformed into a string', $from),
52  'not_string',
53  $from
54  );
55  }
56 }
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...