ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BooleanTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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.