<?php
/**
 * A class for validating values against a set of options.
 */
class validator_option extends validator_reverse
{
    /**
     * @var array The options.
     */
    protected $option;

    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

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

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

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

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    /**
     * Checks if a value is valid.
     * 
     * @param mixed $value The value.
     * @param array (optional) $option The options.
     * @return boolean True if valid, else false.
     */
    protected function _is_valid($value, array $option = null)
    {
        if ($option === null)
        {
            $option = $this->option;
        }
        
        return in_array($value, $option);
    }
}
