You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.2 KiB
146 lines
3.2 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cfnpp\Expression\Token;
|
|
|
|
use App\Cfnpp\Expression\Token;
|
|
use App\Cfnpp\Expression\TokenBinary;
|
|
use App\Cfnpp\Expression\ICloudformationNative;
|
|
|
|
class OperatorBinary extends TokenBinary implements ICloudformationNative
|
|
{
|
|
public const OPERATORS = [
|
|
'and',
|
|
'or',
|
|
'eq'
|
|
];
|
|
|
|
protected $operator;
|
|
|
|
public function __construct($operator)
|
|
{
|
|
$this->operator = $operator;
|
|
}
|
|
|
|
public function getOperator()
|
|
{
|
|
return $this->operator;
|
|
}
|
|
|
|
public static function isToken(string $stream): bool
|
|
{
|
|
foreach (static::OPERATORS as $operator)
|
|
{
|
|
if (strlen($stream) >= strlen($operator) &&
|
|
substr($stream, 0, strlen($operator)) == $operator)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function getToken(string &$stream): Token
|
|
{
|
|
foreach (static::OPERATORS as $operator)
|
|
{
|
|
if (strlen($stream) >= strlen($operator) &&
|
|
substr($stream, 0, strlen($operator)) == $operator)
|
|
{
|
|
$operator = substr($stream, 0, strlen($operator));
|
|
$stream = substr($stream, strlen($operator));
|
|
return new OperatorBinary($operator);
|
|
}
|
|
}
|
|
throw new \Exception('Could not parse OperatorBinary');
|
|
}
|
|
|
|
public function execute(?array $arguments = null)
|
|
{
|
|
$value1 = $arguments[0]->getValue();
|
|
$value2 = $arguments[1]->getValue();
|
|
|
|
switch ($this->getOperator())
|
|
{
|
|
case 'and':
|
|
if (is_scalar($value1) && is_scalar($value2))
|
|
{
|
|
return $value1 && $value2;
|
|
}
|
|
elseif (is_scalar($value1))
|
|
{
|
|
return $value1 ? $value2 : false;
|
|
}
|
|
elseif (is_scalar($value2))
|
|
{
|
|
return $value2 ? $value1 : false;
|
|
}
|
|
elseif ($value1 instanceof Parameter && $value2 instanceof Parameter && $value1->getName() == $value2->getName())
|
|
{
|
|
return $value1;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
// no break
|
|
case 'or':
|
|
if (is_scalar($value1) && is_scalar($value2))
|
|
{
|
|
return $value1 || $value2;
|
|
}
|
|
elseif (is_scalar($value1))
|
|
{
|
|
return $value1 ? true : $value1;
|
|
}
|
|
elseif (is_scalar($value2))
|
|
{
|
|
return $value2 ? true : $value1;
|
|
}
|
|
elseif ($value1 instanceof Parameter && $value2 instanceof Parameter && $value1->getName() == $value2->getName())
|
|
{
|
|
return $value1;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
// no break
|
|
case 'eq':
|
|
if (is_scalar($value1) && is_scalar($value2))
|
|
{
|
|
return $arguments[0] == $arguments[1];
|
|
}
|
|
elseif ($value1 instanceof Parameter && $value2 instanceof Parameter && $value1->getName() == $value2->getName())
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
// no break
|
|
default:
|
|
throw new \Exception('Missing implementation for unary operator: '.$this->getOperator());
|
|
}
|
|
}
|
|
|
|
public function toCloudformation(?array $arguments = null): array
|
|
{
|
|
$value1 = $arguments[0]->getValue();
|
|
$value2 = $arguments[1]->getValue();
|
|
|
|
switch ($this->getOperator())
|
|
{
|
|
case 'and':
|
|
return ['Fn::And' => [$value1, $value2]];
|
|
case 'or':
|
|
return ['Fn::Or' => [$value1, $value2]];
|
|
case 'eq':
|
|
return ['Fn::Equals' => [$value1, $value2]];
|
|
default:
|
|
throw new \Exception('Missing implementation for unary operator: '.$this->getOperator());
|
|
}
|
|
}
|
|
}
|
|
|