ILIAS  release_7 Revision v7.30-3-g800a261c036
BooleanTransformation.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_STRING = 'true';
15 const BOOL_FALSE_STRING = 'false';
20
23
27 public function transform($from)
28 {
29 if (is_bool($from)) {
30 return $from;
31 }
32
33 if (
34 $from === self::BOOL_TRUE_NUMBER
35 || $from === self::BOOL_TRUE_NUMBER_STRING
36 || (is_string($from) && mb_strtolower($from) === self::BOOL_TRUE_STRING)
37 ) {
38 return true;
39 }
40
41 if (
42 $from === self::BOOL_FALSE_NUMBER
43 || $from === self::BOOL_FALSE_NUMBER_STRING
44 || (is_string($from) && mb_strtolower($from) === self::BOOL_FALSE_STRING)
45 ) {
46 return false;
47 }
48
50 sprintf('The value "%s" could not be transformed into boolean.', var_export($from, true)),
51 'not_boolean',
52 $from
53 );
54 }
55}
An exception for terminatinating execution or to throw for unit testing.
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
A transformation is a function from one datatype to another.