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.
121 lines
2.7 KiB
121 lines
2.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;
|
|
|
|
/**
|
|
* Basic operators that take one parameter.
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class OperatorUnary extends TokenUnary implements ICloudformationNative
|
|
{
|
|
/**
|
|
* List of valid operators.
|
|
* @var string[]
|
|
*/
|
|
public const OPERATORS = [
|
|
'not'
|
|
];
|
|
|
|
/**
|
|
* The operator this instance represents.
|
|
* @var string
|
|
*/
|
|
protected $operator;
|
|
|
|
/**
|
|
* New unary operator.
|
|
*
|
|
* @param string $operator
|
|
*/
|
|
public function __construct($operator)
|
|
{
|
|
$this->operator = $operator;
|
|
}
|
|
|
|
/**
|
|
* Get the operator this instance represents.
|
|
*
|
|
* @return string
|
|
*/
|
|
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');
|
|
}
|
|
|
|
/**
|
|
* Execute the operator.
|
|
*
|
|
* Suppressing accesses to arguments since this is guaranteed valid
|
|
* unless there are bugs in Expression.
|
|
* @suppress PhanTypeArraySuspiciousNullable
|
|
* @param ?\App\Util\GraphNode[] $arguments
|
|
* @return \App\Util\GraphNode|Token|scalar|null
|
|
*/
|
|
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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convert this token into a CloudFormation intrinsic.
|
|
*
|
|
* Suppressing accesses to arguments since this is guaranteed valid
|
|
* unless there are bugs in Expression.
|
|
* @suppress PhanTypeArraySuspiciousNullable
|
|
* @param ?\App\Util\GraphNode[] $arguments
|
|
* @return mixed[]
|
|
*/
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
|