ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
StringTransformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
27 
29 {
32  private const BOOL_TRUE = true;
33  private const BOOL_FALSE = false;
34  private const BOOL_TRUE_NUMBER = 1;
35  private const BOOL_FALSE_NUMBER = 0;
36  private const BOOL_TRUE_STRING = 'true';
37  private const BOOL_FALSE_STRING = 'false';
38 
42  public function transform($from): string
43  {
44  if (is_int($from) || is_float($from)) {
45  return (string) $from;
46  }
47 
48  if (is_bool($from) || $from === self::BOOL_TRUE_NUMBER || $from === self::BOOL_FALSE_NUMBER) {
49  if ($from === self::BOOL_TRUE || $from === self::BOOL_TRUE_NUMBER) {
50  return self::BOOL_TRUE_STRING;
51  }
52  if ($from === self::BOOL_FALSE || $from === self::BOOL_FALSE_NUMBER) {
53  return self::BOOL_FALSE_STRING;
54  }
55  }
56 
57  if (is_string($from)) {
58  return $from;
59  }
60 
61  if (is_object($from) && method_exists($from, '__toString')) {
62  return (string) $from;
63  }
64 
66  sprintf('The value "%s" could not be transformed into a string', var_export($from, true)),
67  'not_string',
68  $from
69  );
70  }
71 }
A transformation is a function from one datatype to another.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...