TwigSpreadsheetBundle
TwigSpreadsheetBundle copied to clipboard
TwigNodes cannot be used with variables
I prepared an example:
{% xlsdocument {format: 'xlsx'} %}
{% set title = 'test' %}
{% set style = {columnDimension: {'B': {autoSize: true},'C': {autoSize: true},'D': {autoSize: true},'E': {autoSize: true}}} %}
{% xlssheet title style %}
{% endxlssheet %}
{% endxlsdocument %}
What happened: Too many parameters in "data_storage/export.xls.twig" What I expected: That this call works.
I traced it back to the behaviour of the BaseTokenParser regarding TwigNodeExpressionName. Although style contains an array the type is TwigNodeExpressionName this leads to the base token parser recognizing it as a value type and not as an array.
The following code is responsible for this:
switch ($parameterOptions['type']) {
case self::PARAMETER_TYPE_ARRAY:
// check if expression is valid array
$valid = $expression instanceof \Twig_Node_Expression_Array;
break;
case self::PARAMETER_TYPE_VALUE:
// check if expression is valid value
$valid = !($expression instanceof \Twig_Node_Expression_Array);
break;
default:
throw new \InvalidArgumentException('Invalid parameter type');
}
I'm currently using a workaround with:
switch ($parameterOptions['type']) {
case self::PARAMETER_TYPE_ARRAY:
// check if expression is valid array
$valid = $expression instanceof \Twig_Node_Expression_Array || $expression instanceof \Twig_Node_Expression_Name;
break;
case self::PARAMETER_TYPE_VALUE:
// check if expression is valid value
$valid = !($expression instanceof \Twig_Node_Expression_Array) || $expression instanceof \Twig_Node_Expression_Name;
break;
default:
throw new \InvalidArgumentException('Invalid parameter type');
}