-
Notifications
You must be signed in to change notification settings - Fork 91
/
CastingExpressionNode.php
134 lines (125 loc) · 4.09 KB
/
CastingExpressionNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* Type Casting Expression
* Allows casting variables to specific types, for example `{myVariable as boolean}`
*
* @internal
* @todo Make class final.
*/
class CastingExpressionNode extends AbstractExpressionNode
{
/**
* @var array
*/
protected static $validTypes = [
'integer', 'boolean', 'string', 'float', 'array', 'DateTime',
];
/**
* Pattern which detects ternary conditions written in shorthand
* syntax, e.g. {some.variable as integer}. The right-hand side
* of the expression can also be a variable containing the type
* of the variable.
*/
public static $detectionExpression = '/
(
{ # Start of shorthand syntax
(?: # Math expression is composed of...
[_a-zA-Z0-9.]+ # Template variable object access path
[\s]+as[\s]+ # A single space, then "as", then a single space
[_a-zA-Z0-9.\s]+ # Casting-to-type side
)
} # End of shorthand syntax
)/x';
/**
* @param RenderingContextInterface $renderingContext
* @param string $expression
* @param array $matches
* @return int|float
*/
public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches)
{
$expression = trim($expression, '{}');
list($variable, $type) = explode(' as ', $expression);
$variable = static::getTemplateVariableOrValueItself($variable, $renderingContext);
if (!in_array($type, self::$validTypes)) {
$type = static::getTemplateVariableOrValueItself($type, $renderingContext);
}
if (!in_array($type, self::$validTypes)) {
throw new ExpressionException(
sprintf(
'Invalid target conversion type "%s" specified in casting expression "{%s}".',
$type,
$expression,
),
);
}
return self::convertStatic($variable, $type);
}
/**
* @param mixed $variable
* @param string $type
* @return mixed
*/
protected static function convertStatic($variable, $type)
{
$value = null;
if ($type === 'integer') {
$value = (int)$variable;
} elseif ($type === 'boolean') {
$value = (bool)$variable;
} elseif ($type === 'string') {
$value = (string)$variable;
} elseif ($type === 'float') {
$value = (float)$variable;
} elseif ($type === 'DateTime') {
$value = self::convertToDateTime($variable);
} elseif ($type === 'array') {
$value = (array)self::convertToArray($variable);
}
return $value;
}
/**
* @param mixed $variable
* @return \DateTime|false
*/
protected static function convertToDateTime($variable)
{
if (preg_match_all('/[a-z]+/i', $variable)) {
return new \DateTime($variable);
}
return \DateTime::createFromFormat('U', (int)$variable);
}
/**
* @param mixed $variable
* @return array
*/
protected static function convertToArray($variable)
{
if (is_array($variable)) {
return $variable;
}
if (is_string($variable) && strpos($variable, ',')) {
return array_map('trim', explode(',', $variable));
}
if ($variable instanceof \Iterator) {
$array = [];
foreach ($variable as $key => $value) {
$array[$key] = $value;
}
return $array;
}
if (is_object($variable) && method_exists($variable, 'toArray')) {
return $variable->toArray();
}
if (is_bool($variable)) {
return [];
}
return [$variable];
}
}