*/ 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()); } } }