ParagonIE\EasyDB\EasyDB::escapeIdentifier PHP Method

escapeIdentifier() public method

Make sure only valid characters make it in column/table names
public escapeIdentifier ( string $string, boolean $quote = true ) : string
$string string - table or column name
$quote boolean - certain SQLs escape column names (i.e. mysql with `backticks`)
return string
    public function escapeIdentifier(string $string, $quote = true) : string
    {
        if (empty($string)) {
            throw new Issues\InvalidIdentifier('Invalid identifier: Must be a non-empty string.');
        }
        if ($this->allowSeparators) {
            $str = \preg_replace('/[^\\.0-9a-zA-Z_]/', '', $string);
            if (\strpos($str, '.') !== false) {
                $pieces = \explode('.', $str);
                foreach ($pieces as $i => $p) {
                    $pieces[$i] = $this->escapeIdentifier($p, $quote);
                }
                return \implode('.', $pieces);
            }
        } else {
            $str = \preg_replace('/[^0-9a-zA-Z_]/', '', $string);
            if ($str !== \trim($string)) {
                if ($str === \str_replace('.', '', $string)) {
                    throw new Issues\InvalidIdentifier('Separators (.) are not permitted.');
                }
                throw new Issues\InvalidIdentifier('Invalid identifier: Invalid characters supplied.');
            }
        }
        // The first character cannot be [0-9]:
        if (\preg_match('/^[0-9]/', $str)) {
            throw new Issues\InvalidIdentifier('Invalid identifier: Must begin with a letter or underscore.');
        }
        if ($quote) {
            switch ($this->dbEngine) {
                case 'mssql':
                    return '[' . $str . ']';
                case 'mysql':
                    return '`' . $str . '`';
                default:
                    return '"' . $str . '"';
            }
        }
        return $str;
    }