ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
BooleanTransformation.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_STRING = 'true';
33  private const BOOL_FALSE_STRING = 'false';
34  private const BOOL_TRUE_NUMBER = 1;
35  private const BOOL_FALSE_NUMBER = 0;
36  private const BOOL_TRUE_NUMBER_STRING = '1';
37  private const BOOL_FALSE_NUMBER_STRING = '0';
38 
42  public function transform($from): bool
43  {
44  if (is_bool($from)) {
45  return $from;
46  }
47 
48  if (
49  $from === self::BOOL_TRUE_NUMBER
50  || $from === self::BOOL_TRUE_NUMBER_STRING
51  || (is_string($from) && mb_strtolower($from) === self::BOOL_TRUE_STRING)
52  ) {
53  return true;
54  }
55 
56  if (
57  $from === self::BOOL_FALSE_NUMBER
58  || $from === self::BOOL_FALSE_NUMBER_STRING
59  || (is_string($from) && mb_strtolower($from) === self::BOOL_FALSE_STRING)
60  ) {
61  return false;
62  }
63 
65  sprintf('The value "%s" could not be transformed into boolean.', var_export($from, true)),
66  'not_boolean',
67  $from
68  );
69  }
70 }
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...