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.
80 lines
1.7 KiB
80 lines
1.7 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cfnpp\Expression\Token;
|
|
|
|
use App\Cfnpp\Expression\Token;
|
|
use App\Cfnpp\Expression\TokenUnary;
|
|
use App\Cfnpp\Expression\ICloudformationNative;
|
|
|
|
class OperatorUnary extends TokenUnary implements ICloudformationNative
|
|
{
|
|
public const OPERATORS = [
|
|
'not'
|
|
];
|
|
|
|
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 OperatorUnary($operator);
|
|
}
|
|
}
|
|
throw new \Exception('Could not parse OperatorUnary');
|
|
}
|
|
|
|
public function execute(?array $arguments = null)
|
|
{
|
|
$value = $arguments[0]->getValue();
|
|
switch ($this->getOperator())
|
|
{
|
|
case 'not':
|
|
return is_scalar($value) ? !$value : null;
|
|
default:
|
|
throw new \Exception('Missing implementation for unary operator: '.$this->getOperator());
|
|
}
|
|
}
|
|
|
|
public function toCloudformation(?array $arguments = null): array
|
|
{
|
|
$value = $arguments[0]->getValue();
|
|
switch ($this->getOperator())
|
|
{
|
|
case 'not':
|
|
return ['Fn::Not' => [$value]];
|
|
default:
|
|
throw new \Exception('Missing implementation for unary operator: '.$this->getOperator());
|
|
}
|
|
}
|
|
}
|
|
|