ILIAS
trunk Revision v11.0_alpha-2638-g80c1d007f79
◀ ilDoc Overview
IntegerTransformation.php
Go to the documentation of this file.
1
<?php
2
19
declare(strict_types=1);
20
21
namespace
ILIAS\Refinery\KindlyTo\Transformation
;
22
23
use
ILIAS\Refinery\DeriveApplyToFromTransform
;
24
use
ILIAS\Refinery\DeriveInvokeFromTransform
;
25
use
ILIAS\Refinery\Transformation
;
26
use
ILIAS\Refinery\ConstraintViolationException
;
27
28
class
IntegerTransformation
implements
Transformation
29
{
30
use
DeriveApplyToFromTransform
;
31
use
DeriveInvokeFromTransform
;
32
private
const
REG_INT
=
'/^\s*(0|(-?[1-9]\d*))\s*$/'
;
33
37
public
function
transform
($from):
int
38
{
39
if
(is_int($from)) {
40
return
$from;
41
}
42
43
if
($from !== INF && $from !== -INF && is_float($from) && !is_nan($from)) {
44
$from = round($from);
45
return
(
int
) $from;
46
}
47
48
if
(is_bool($from)) {
49
return
(
int
) $from;
50
}
51
52
if
(is_string($from) && preg_match(self::REG_INT, $from)) {
53
$int = (
int
) $from;
54
// This is supposed to guard against PHP_MIN_INT and PHP_MAX_INT.
55
// We only return the value if it looks the same when transforming it
56
// back to string. This won't be the case for too big or too small
57
// values.
58
if
(trim($from) === (string) $int) {
59
return
$int;
60
}
61
}
62
63
throw
new
ConstraintViolationException
(
64
sprintf(
'The value "%s" can not be transformed into an integer'
, var_export($from,
true
)),
65
'not_integer'
,
66
$from
67
);
68
}
69
}
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation\REG_INT
const REG_INT
Definition:
IntegerTransformation.php:32
ILIAS\Refinery\DeriveInvokeFromTransform
trait DeriveInvokeFromTransform
Definition:
DeriveInvokeFromTransform.php:24
ILIAS\Refinery\DeriveApplyToFromTransform
trait DeriveApplyToFromTransform
Definition:
DeriveApplyToFromTransform.php:29
Transformation
ILIAS\Refinery\ConstraintViolationException
Definition:
ConstraintViolationException.php:29
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation
Definition:
IntegerTransformation.php:28
ILIAS\Refinery\Transformation
A transformation is a function from one datatype to another.
Definition:
Transformation.php:34
ILIAS\Repository\int
int(string $key)
Definition:
trait.BaseGUIRequest.php:61
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation\transform
transform($from)
Definition:
IntegerTransformation.php:37
ConstraintViolationException
ILIAS\Refinery\KindlyTo\Transformation
Definition:
BooleanTransformation.php:21
components
ILIAS
Refinery
src
KindlyTo
Transformation
IntegerTransformation.php
Generated on Sun Aug 31 2025 23:03:32 for ILIAS by
1.8.13 (using
Doxyfile
)