<?php
/**
 * An abstract class for validators.
 *
 * This class provides validators a reverse mode and custom error messages.
 */
abstract class validator_reverse implements validator
{
    /**
     * @var boolean Whether the validator is in reverse mode or not.
     */
    protected $reverse;
    
    /**
     * @var string The error message.
     */
    protected $error;
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    /**
     * @param string (optional) $error The error message.
     * @param boolean (optional) $reverse Whether the validator is in reverse
     * mode or not.
     */
    public function __construct($error = null, $reverse = false)
    {
        $this->error = $error;
        $this->reverse = $reverse;
    }

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

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

    /**
     * Checks if a value is valid.
     * 
     * @param mixed $value The value.
     * @return boolean True if valid, else false.
     */
    public function is_valid($value)
    {
        $value = func_get_args();
        $valid = call_user_func_array(array($this, '_is_valid'), $value);
        return $valid xor $this->reverse;
    }

    /**
     * Gets the error message.
     * 
     * @return string The message.
     */
    public function get_error()
    {
        return $this->error;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    /**
     * Checks if a value is valid.
     * 
     * @param mixed $value The value.
     * @return boolean True if valid, else false.
     */
    protected abstract function _is_valid($value);
}
