ILIAS
trunk Revision v11.0_alpha-2645-g16283d3b3f8
◀ ilDoc Overview
StringTransformation.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
StringTransformation
implements
Transformation
29
{
30
use
DeriveApplyToFromTransform
;
31
use
DeriveInvokeFromTransform
;
32
private
const
BOOL_TRUE
=
true
;
33
private
const
BOOL_FALSE
=
false
;
34
private
const
BOOL_TRUE_NUMBER
= 1;
35
private
const
BOOL_FALSE_NUMBER
= 0;
36
private
const
BOOL_TRUE_STRING
=
'true'
;
37
private
const
BOOL_FALSE_STRING
=
'false'
;
38
42
public
function
transform
($from): string
43
{
44
if
(is_int($from) || is_float($from)) {
45
return
(
string
) $from;
46
}
47
48
if
(is_bool($from) || $from === self::BOOL_TRUE_NUMBER || $from === self::BOOL_FALSE_NUMBER) {
49
if
($from === self::BOOL_TRUE || $from === self::BOOL_TRUE_NUMBER) {
50
return
self::BOOL_TRUE_STRING;
51
}
52
if
($from === self::BOOL_FALSE || $from === self::BOOL_FALSE_NUMBER) {
53
return
self::BOOL_FALSE_STRING;
54
}
55
}
56
57
if
(is_string($from)) {
58
return
$from;
59
}
60
61
if
(is_object($from) && method_exists($from,
'__toString'
)) {
62
return
(
string
) $from;
63
}
64
65
throw
new
ConstraintViolationException
(
66
sprintf(
'The value "%s" could not be transformed into a string'
, var_export($from,
true
)),
67
'not_string'
,
68
$from
69
);
70
}
71
}
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation
Definition:
StringTransformation.php:28
ILIAS\Refinery\DeriveInvokeFromTransform
trait DeriveInvokeFromTransform
Definition:
DeriveInvokeFromTransform.php:24
ILIAS\Refinery\DeriveApplyToFromTransform
trait DeriveApplyToFromTransform
Definition:
DeriveApplyToFromTransform.php:29
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_FALSE_NUMBER
const BOOL_FALSE_NUMBER
Definition:
StringTransformation.php:35
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_FALSE_STRING
const BOOL_FALSE_STRING
Definition:
StringTransformation.php:37
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_TRUE_STRING
const BOOL_TRUE_STRING
Definition:
StringTransformation.php:36
Transformation
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_FALSE
const BOOL_FALSE
Definition:
StringTransformation.php:33
ILIAS\Refinery\ConstraintViolationException
Definition:
ConstraintViolationException.php:29
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_TRUE
const BOOL_TRUE
Definition:
StringTransformation.php:32
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\BOOL_TRUE_NUMBER
const BOOL_TRUE_NUMBER
Definition:
StringTransformation.php:34
ILIAS\Refinery\Transformation
A transformation is a function from one datatype to another.
Definition:
Transformation.php:34
ILIAS\Refinery\KindlyTo\Transformation\StringTransformation\transform
transform($from)
Definition:
StringTransformation.php:42
ConstraintViolationException
ILIAS\Refinery\KindlyTo\Transformation
Definition:
BooleanTransformation.php:21
components
ILIAS
Refinery
src
KindlyTo
Transformation
StringTransformation.php
Generated on Mon Sep 1 2025 23:03:41 for ILIAS by
1.8.13 (using
Doxyfile
)