*/ class TokenOperator extends Token { /** * List of valid operators this can parse. * @var string[] */ public const OPERATORS = [ 'eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'and', 'or' ]; /** * 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!'); } }