Browse Source

Add basic !if function

master
Adam Pippin 3 years ago
parent
commit
497f23594e
  1. 37
      app/Engine/Cfnpp/Functions.php

37
app/Engine/Cfnpp/Functions.php

@ -111,4 +111,41 @@ class Functions
$value = $this->options->getVariable($function->getValue());
return new NodeValue(null, $node->hasName() ? $node->getName() : null, $value);
}
/**
* Conditionally include part of the file.
*
* @param Node $node
* @param NodeFunction $function
* @return ?Node
*/
public function f_if(Node $node, NodeFunction $function): ?Node
{
if (!($function instanceof NodeFunction))
{
throw new \Exception('!if expects array of parameters');
}
$nodes = $function->getChildren();
$condition = $nodes[0]; // expects NodeValue
$if_true = $nodes[1];
$if_false = sizeof($nodes) == 3 ? $nodes[2] : null;
$parser = new \App\Engine\Cfnpp\Expression\Parser();
$expression = $parser->parse($condition->getValue());
$result = $expression->evaluate($this->options->getVariables());
if ($result)
{
$if_true->setName($node->hasName() ? $node->getName() : null);
return $if_true;
}
if (isset($if_false))
{
$if_false->setName($node->hasName() ? $node->getName() : null);
}
return $if_false;
}
}

Loading…
Cancel
Save