diff --git a/app/Engine/Cfnpp/Functions.php b/app/Engine/Cfnpp/Functions.php index 0dca211..8854726 100644 --- a/app/Engine/Cfnpp/Functions.php +++ b/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; + } }