From 0737fcd74aa919f2313af9e3917fef3f233d4816 Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Fri, 19 Feb 2021 12:33:03 -0800 Subject: [PATCH] Add concat/select operators --- app/Cfnpp/Expression/Token/OperatorBinary.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/app/Cfnpp/Expression/Token/OperatorBinary.php b/app/Cfnpp/Expression/Token/OperatorBinary.php index a0b82fb..a45b549 100644 --- a/app/Cfnpp/Expression/Token/OperatorBinary.php +++ b/app/Cfnpp/Expression/Token/OperatorBinary.php @@ -22,7 +22,9 @@ class OperatorBinary extends TokenBinary implements ICloudformationNative public const OPERATORS = [ 'and', 'or', - 'eq' + 'eq', + 'concat', + 'select' ]; /** @@ -147,8 +149,32 @@ class OperatorBinary extends TokenBinary implements ICloudformationNative return null; + case 'concat': + if (is_scalar($value1) && is_scalar($value2)) + { + return $value2.$value1; + } + elseif ($value1 instanceof Parameter && is_scalar($value2) && empty($value2)) + { + return $value1; + } + elseif ($value2 instanceof Parameter && is_scalar($value1) && empty($value1)) + { + return $value2; + } + + return null; + + case 'select': + if (is_scalar($value1) && is_array($value2)) + { + return $value2[$value1]; + } + + return null; + default: - throw new \Exception('Missing implementation for unary operator: '.$this->getOperator()); + throw new \Exception('Missing implementation for binary operator: '.$this->getOperator()); } } @@ -174,8 +200,12 @@ class OperatorBinary extends TokenBinary implements ICloudformationNative return ['Fn::Or' => [$value1, $value2]]; case 'eq': return ['Fn::Equals' => [$value1, $value2]]; + case 'concat': + return ['Fn::Join' => ['', [$value2, $value1]]]; + case 'select': + return ['Fn::Select' => [$value1, $value2]]; default: - throw new \Exception('Missing implementation for unary operator: '.$this->getOperator()); + throw new \Exception('Operator cannot be applied to parameters: '.$this->getOperator()); } } }