ILIAS
release_7 Revision v7.30-3-g800a261c036
◀ ilDoc Overview
IntegerTransformation.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
5
namespace
ILIAS\Refinery\KindlyTo\Transformation
;
6
7
use
ILIAS\Refinery\DeriveApplyToFromTransform
;
8
use
ILIAS\Refinery\DeriveInvokeFromTransform
;
9
use
ILIAS\Refinery\Transformation
;
10
use
ILIAS\Refinery\ConstraintViolationException
;
11
12
class
IntegerTransformation
implements
Transformation
13
{
14
const
REG_INT
=
'/^\s*(0|(-?[1-9]\d*))\s*$/'
;
15
16
use
DeriveApplyToFromTransform
;
17
use
DeriveInvokeFromTransform
;
18
22
public
function
transform
($from)
23
{
24
if
(is_int($from)) {
25
return
$from;
26
}
27
28
if
(is_float($from) && !is_nan($from) && $from !== INF && $from !== -INF) {
29
$from = round($from);
30
return
intval($from);
31
}
32
33
if
(is_bool($from)) {
34
return
(
int
) $from;
35
}
36
37
if
(is_string($from) && preg_match(self::REG_INT, $from)) {
38
$int = intval($from);
39
// This is supposed to guard against PHP_MIN_INT and PHP_MAX_INT.
40
// We only return the value if it looks the same when transforming it
41
// back to string. This won't be the case for too big or too small
42
// values.
43
if
(trim($from) === (
string
) $int) {
44
return
$int;
45
}
46
}
47
48
throw
new
ConstraintViolationException
(
49
sprintf(
'The value "%s" can not be transformed into an integer'
, var_export($from,
true
)),
50
'not_integer'
,
51
$from
52
);
53
}
54
}
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation\REG_INT
const REG_INT
Definition:
IntegerTransformation.php:14
Transformation
ILIAS\Refinery\DeriveApplyToFromTransform
trait DeriveApplyToFromTransform
Definition:
DeriveApplyToFromTransform.php:13
ILIAS\Refinery\DeriveInvokeFromTransform
trait DeriveInvokeFromTransform
Definition:
DeriveInvokeFromTransform.php:13
ILIAS\Refinery\ConstraintViolationException
Definition:
ConstraintViolationException.php:14
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation
Definition:
IntegerTransformation.php:12
ILIAS\Refinery\Transformation
A transformation is a function from one datatype to another.
Definition:
Transformation.php:17
ILIAS\Refinery\KindlyTo\Transformation\IntegerTransformation\transform
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
Definition:
IntegerTransformation.php:22
ConstraintViolationException
ILIAS\Refinery\KindlyTo\Transformation
Definition:
BooleanTransformation.php:5
src
Refinery
KindlyTo
Transformation
IntegerTransformation.php
Generated on Mon Sep 1 2025 21:01:46 for ILIAS by
1.8.13 (using
Doxyfile
)