<?php
/**
 * A class for creating sql objects.
 *
 * This class provides a convenient way to create sql objects used by the
 * query builder.
 */
class sql
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    /**
     * A shortcut for func().
     * 
     * @param string $function The function name.
     * @param array $parameter The parameters.
     * @return stdclass The object.
     */
    public static function __callstatic($function, array $parameter)
    {
        array_unshift($parameter, $function);
        return call_user_func_array('self::func', $parameter);
    }

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Gets a sql object of type raw.
     * 
     * @param string $value The object value.
     * @return stdclass The object.
     */
    public static function raw($value)
    {
        return (object) array('type' => 'raw', 'value' => $value);
    }
    
    /**
     * Gets a sql object of type identifier.
     * 
     * @param string $value The object value.
     * @return stdclass The object.
     */
    public static function identifier($value)
    {
        return (object) array('type' => 'identifier', 'value' => $value);
    }
    
    /**
     * Gets a sql object of type literal.
     * 
     * @param string $value The object value.
     * @return stdclass The object.
     */
    public static function literal($value)
    {
        return (object) array('type' => 'literal', 'value' => $value);
    }
    
    /**
     * Gets a sql object of type condition.
     * 
     * @param string $condition The operator.
     * @param string/stdclass $left The left operand (identifier).
     * @param mixed $right The right operand (literal).
     * -
     * @param string/stdclass $condition The left operand (identifier).
     * @param mixed $left The right operand (literal).
     * @return stdclass The object.
     */
    public static function condition($condition, $left, $right = null)
    {
        $condition = func_get_args();
        $count = count($condition);
        
        if ($count < 2)
        {
            throw new lengthexception('Condition is not valid.');
        }
        
        if ($count === 2)
        {
            if (is_array($condition[1]))
            {
                array_unshift($condition, 'in');
            }
            else
            {
                array_unshift($condition, '=');
            }
        }
        
        return (object) array('type' => 'condition', 'value' => $condition);
    }
    
    /**
     * Gets a sql object of type func.
     * 
     * @param string $function The function name.
     * @param string $parameter (optional) The parameter (identifier).
     * @return stdclass The object.
     */
    public static function func($function, $parameter = null)
    {
        return (object) array('type' => 'func', 'value' => func_get_args());
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
