<?php
/**
 * A class for validating numbers against a numeric regex pattern and
 * minimum/maximum range constraints.
 */
class validator_numeric extends validator_pattern
{
    /**
     * @var number The minimum value constraint.
     */
    protected $minimum;
    
    /**
     * @var number The maximum value constraint.
     */
    protected $maximum;
    
    /**
     * @var boolean Whether the minimum/maximum constraint is inclusive or not.
     */
    protected $inclusive;

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

    /**
     * @param string (optional) $pattern The regex pattern.
     * @param integer (optional) $minimum The minimum value constraint.
     * @param integer (optional) $maximum The maximum value constraint.
     * @param boolean (optional) $inclusive Whether the minimum/maximum
     * constraint is inclusive or not.
     * @param string (optional) $error The error message.
     * @param boolean (optional) $reverse Whether the validator is in reverse
     * mode or not.
     */
    public function __construct($pattern = '~^-?(([0-9]+)|(\.[0-9]+)|([0-9]+\.[0-9]+))$~', $minimum = null, $maximum = null, $inclusive = true, $error = null, $reverse = false)
    {
        parent::__construct($pattern, $error, $reverse);
        $this->minimum = $minimum;
        $this->maximum = $maximum;
        $this->inclusive = $inclusive;
    }

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

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

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    /**
     * Checks if a value is valid.
     * 
     * @param mixed $value The value.
     * @param string (optional) $pattern The regex pattern.
     * @param integer (optional) $minimum The minimum value constraint.
     * @param integer (optional) $maximum The maximum value constraint.
     * @param boolean (optional) $inclusive Whether the minimum/maximum
     * constraint is inclusive or not.
     * @return boolean True if valid, else false.
     */
    protected function _is_valid($value, $pattern = null, $minimum = null, $maximum = null, $inclusive = null)
    {
        if (!parent::_is_valid($value, $pattern))
        {
            return false;
        }
        
        if ($inclusive === null)
        {
            $inclusive = $this->inclusive;
        }
        
        if ($minimum === null)
        {
            $minimum = $this->minimum;
        }
        
        if ($minimum !== null)
        {
            if (($value < $minimum) || ($value == $minimum && !$inclusive))
            {
                return false;
            }
        }
        
        if ($maximum === null)
        {
            $maximum = $this->maximum;
        }
        
        if ($maximum !== null)
        {
            if (($value > $maximum) || ($value == $maximum && !$inclusive))
            {
                return false;
            }
        }
        
        return true;
    }
}
