<commit_before><?php
namespace WScore\SqlBuilder\Builder;

use WScore\SqlBuilder\Sql\Sql;

class Quote
{
    /**
     * @var Sql
     */
    protected $format = '"%s"';

    protected $quote1 = '"';

    protected $quote2 = '"';

    /**
     * @param string $q1
     * @param string $q2
     */
    public function setQuote( $q1, $q2=null ) {
        $this->quote1 = $q1;
        $this->quote2 = $q2 ?: $q1;
        $this->format = $q1 . '%s' . $this->quote2;
    }

    /**
     * @param array $list
     * @return array
     */
    public function map( $list )
    {
        $list = array_map( function($val) {
            return $this->quote($val);
        }, $list );
        return $list;
    }
    
    /**
     * @param string $name
     * @param array|string $separator
     * @return string
     */
    public function quote( $name, $separator=[' AS ', ' as ', '.'] )
    {
        if( !$name ) return $name;
        if( is_object($name) && is_callable( $name ) ) return $name();
        if( !$separator ) return $this->quoteString( $name );
        if( !is_array( $separator ) ) $separator = array($separator);
        while( $sep = array_shift( $separator ) ) {
            if( false !== stripos( $name, $sep ) ) {
                $list = explode( $sep, $name );
                foreach( $list as $key => $str ) {
                    $list[$key] = $this->quote( $str, $separator );
                }
                return implode( $sep, $list );
                break;
            }
        }
        return $this->quoteString($name);
    }

    /**
     * @param $name
     * @return string
     */
    public function quoteString( $name )
    {
        if( !$name ) return $name;
        if( $name == '*' ) return $name;
        if( $this->isQuoted( $name ) ) {
            return $name;
        }
        return sprintf( $this->format, $name );
    }

    /**
     * @param $name
     * @return bool
     */
    public function isQuoted( $name )
    {
        if( substr( $name, 0, 1 ) == $this->quote1 &&
            substr( $name, -1 ) == $this->quote2 ) {
            return true;
        }
        return false;
    }
}<commit_msg>quote to support optional prefix when quoting.<commit_after><?php
namespace WScore\SqlBuilder\Builder;

use Mockery\CountValidator\Exception;
use WScore\SqlBuilder\Sql\Sql;

class Quote
{
    /**
     * @var Sql
     */
    protected $format = '"%s"';

    protected $quote1 = '"';

    protected $quote2 = '"';

    /**
     * @param string $q1
     * @param string $q2
     */
    public function setQuote( $q1, $q2=null ) {
        $this->quote1 = $q1;
        $this->quote2 = $q2 ?: $q1;
        $this->format = $q1 . '%s' . $this->quote2;
    }

    /**
     * @param array $list
     * @return array
     */
    public function map( $list )
    {
        $list = array_map( function($val) {
            return $this->quote($val);
        }, $list );
        return $list;
    }

    /**
     * @param string $name
     * @param string $prefix
     * @return string
     */
    public function quote( $name, $prefix=null )
    {
        if( !$name ) return $name;
        if( is_object($name) && is_callable( $name ) ) return $name();
        if( $prefix ) {
            if( $this->isQuoted( $name ) ) {
                $name = $prefix . '.' . $name;
            } elseif( false === strpos( $name, '.' ) ) {
                $name = $prefix . '.'  . $name;
            }
        }
        return $this->qt( $name, [' AS ', ' as ', '.'] );
    }

    /**
     * @param $name
     * @return string
     */
    public function quoteString( $name )
    {
        if( !$name ) return $name;
        if( $name == '*' ) return $name;
        if( $this->isQuoted( $name ) ) {
            return $name;
        }
        return sprintf( $this->format, $name );
    }

    /**
     * @param $name
     * @return bool
     */
    public function isQuoted( $name )
    {
        if( substr( $name, 0, 1 ) == $this->quote1 &&
            substr( $name, -1 ) == $this->quote2 ) {
            return true;
        }
        return false;
    }

    /**
     * @param $name
     * @param $separator
     * @return string
     */
    protected function qt( $name, $separator )
    {
        if ( !$separator ) return $this->quoteString( $name );
        if ( !is_array( $separator ) ) $separator = array( $separator );
        while ( $sep = array_shift( $separator ) ) {
            if ( false !== stripos( $name, $sep ) ) {
                $list = explode( $sep, $name );
                foreach ( $list as $key => $str ) {
                    $list[ $key ] = $this->qt( $str, $separator );
                }
                return implode( $sep, $list );
                break;
            }
        }
        return $this->quoteString( $name );
    }
}
{'fraction_non_alphanumeric': 0.16729244889445138, 'fraction_numerical': 0.007509386733416771, 'mean_word_length': 1.534355179704017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4972592', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1520, 'n_words': 368}