<?php
/**
* phpillow CouchDB backend
*
* This file is part of phpillow.
*
* phpillow is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3 of the License.
*
* phpillow is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with phpillow; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Core
* @version arbit-0.5-alpha
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
*/
/**
* Validate integer inputs
*
* @package Core
* @version arbit-0.5-alpha
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
*/
class phpillowIntegerValidator extends phpillowValidator
{
/**
* Minimum value for passed integer value
*
* @var int
*/
protected $min;
/**
* Maximum value for passed integer value
*
* @var int
*/
protected $max;
/**
* Validator constructor
*
* Validator constructor to optionally specifiy the minimum and maximum
* value for the allowed integer values. For no validation you may pass
* false to the respective parameter.
*
* @param mixed $min
* @param mixed $max
* @return void
*/
public function __construct( $min = false, $max = false )
{
$this->min = $min;
$this->max = $max;
}
/**
* Validate input as integer
*
* @param mixed $input
* @return int
*/
public function validate( $input )
{
// Check for minimum constraint
if ( ( $this->min !== false ) &&
( $input < $this->min ) )
{
throw new phpillowValidationException(
'Input value %input is not bigger or equal then %minimum.',
array(
'input' => $input,
'minimum' => $this->min,
)
);
}
// Check for maximum constraint
if ( ( $this->max !== false ) &&
( $input > $this->max ) )
{
throw new phpillowValidationException(
'Input value %input is not lesser or equal then %maximum.',
array(
'input' => $input,
'maximum' => $this->max,
)
);
}
return (int) $input;
}
}