token = $token; $this->type = $type; $this->flags = $flags; $this->value = $this->extract(); } /** * Does a little processing to the token to extract a value. * * If no processing can be done it will return the initial string. */ public function extract(): bool|float|int|string { switch ($this->type) { case TokenType::Keyword: $this->keyword = strtoupper($this->token); if (! ($this->flags & self::FLAG_KEYWORD_RESERVED)) { // Unreserved keywords should stay the way they are because they // might represent field names. return $this->token; } return $this->keyword; case TokenType::Whitespace: return ' '; case TokenType::Bool: return strtoupper($this->token) === 'TRUE'; case TokenType::Number: $ret = str_replace('--', '', $this->token); // e.g. ---42 === -42 if ($this->flags & self::FLAG_NUMBER_HEX) { $ret = str_replace(['-', '+'], '', $this->token); if ($this->flags & self::FLAG_NUMBER_NEGATIVE) { $ret = -hexdec($ret); } else { $ret = hexdec($ret); } } elseif (($this->flags & self::FLAG_NUMBER_APPROXIMATE) || ($this->flags & self::FLAG_NUMBER_FLOAT)) { $ret = (float) $ret; } elseif (! ($this->flags & self::FLAG_NUMBER_BINARY)) { $ret = (int) $ret; } return $ret; case TokenType::String: // Trims quotes. $str = $this->token; $str = mb_substr($str, 1, -1, 'UTF-8'); // Removes surrounding quotes. $quote = $this->token[0]; $str = str_replace($quote . $quote, $quote, $str); // Finally unescapes the string. // // `stripcslashes` replaces escape sequences with their // representation. // // NOTE: In MySQL, `\f` and `\v` have no representation, // even they usually represent: form-feed and vertical tab. $str = str_replace('\f', 'f', $str); $str = str_replace('\v', 'v', $str); $str = stripcslashes($str); return $str; case TokenType::Symbol: $str = $this->token; if (isset($str[0]) && ($str[0] === '@')) { // `mb_strlen($str)` must be used instead of `null` because // in PHP 5.3- the `null` parameter isn't handled correctly. $str = mb_substr( $str, ! empty($str[1]) && ($str[1] === '@') ? 2 : 1, mb_strlen($str), 'UTF-8', ); } if (isset($str[0]) && ($str[0] === ':')) { $str = mb_substr($str, 1, mb_strlen($str), 'UTF-8'); } if (isset($str[0]) && (($str[0] === '`') || ($str[0] === '"') || ($str[0] === '\''))) { $quote = $str[0]; $str = mb_substr($str, 1, -1, 'UTF-8'); $str = str_replace($quote . $quote, $quote, $str); } return $str; default: return $this->token; } } /** * Converts the token into an inline token by replacing tabs and new lines. */ public function getInlineToken(): string { return str_replace( [ "\r", "\n", "\t", ], [ '\r', '\n', '\t', ], $this->token, ); } }