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.
97 lines
1.7 KiB
97 lines
1.7 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cfnpp\Expression;
|
|
|
|
/**
|
|
* Token representing a comparison operator.
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class TokenOperator extends Token
|
|
{
|
|
/**
|
|
* List of valid operators this can parse.
|
|
* @var string[]
|
|
*/
|
|
public const OPERATORS = [
|
|
'eq',
|
|
'neq',
|
|
'gt',
|
|
'gte',
|
|
'lt',
|
|
'lte',
|
|
'and',
|
|
'or',
|
|
'not'
|
|
];
|
|
|
|
/**
|
|
* Operator this token represents.
|
|
* @var string
|
|
*/
|
|
protected $operator;
|
|
|
|
/**
|
|
* Create a new operator token.
|
|
*
|
|
* @param string $operator
|
|
*/
|
|
public function __construct($operator)
|
|
{
|
|
$this->operator = $operator;
|
|
}
|
|
|
|
/**
|
|
* Get the operator this token represents.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getOperator(): string
|
|
{
|
|
return $this->operator;
|
|
}
|
|
|
|
/**
|
|
* Determine whether a operator token can be parsed from a stream.
|
|
*
|
|
* @param string $stream
|
|
* @return bool
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Parse an operator token from a stream.
|
|
*
|
|
* Returns token, and modifies stream to remove all consumed characters
|
|
*
|
|
* @param string $stream
|
|
* @return Token
|
|
*/
|
|
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 TokenOperator($operator);
|
|
}
|
|
}
|
|
throw new \Exception('Could not parse operator token!');
|
|
}
|
|
}
|
|
|