<?php
/**
* a baisc constraint for integers. This class can be used in setter methods
* to validate input data and constrain it to a valid range.
*
* @see Constraint
* @author Cory
* @package constraints
*/
require_once ROGUE_DIR . '/constraints/Constraint.php';
/**
* a baisc constraint for integers. This class can be used in setter methods
* to validate input data and constrain it to a valid range.
* <code>
* <pre>
* private static $_myConstraint = false;
*
* public function __construct()
* {
* // create the constraint as a static, we only need one for all call references
* if (self::$_myConstraint == false)
* self::$_myConstraint = new IntegerConstraint('a constraint', integer, false, minLength, maxLength);
* }
* public function setSomeThing($data)
* {
* self::$_myConstraint->validate($data); // throws ConstraintException.php if $data is not type integer and not null
* $this->_someInt = $data
* }
* </pre>
* </code>
*
* @author Cory
* @package constraints
* @property integer $_minLength the minimum value to accept
* @property integer $_maxLength the maximum value to accept
*/
class StringConstraint extends Constraint
{
protected $_minLength;
protected $_maxLength;
/**
* build a new Contraint for Integers
*
* @param String $name the name of the constraint
* @param String $type the PHP name for the type as returned by instanceof
* @param Boolean $nullAble true if the paramater may be null
* @param Integer $minLength the minimum value for the constraint
* @param Integer $maxLength the maximum value for the constraint
*/
public function __construct($name = false, $type = false, $nullAble = false, $minLength = false, $maxLength = false)
{
// setup the base constraint
parent::__construct($name, $type, $nullAble);
// verify the min value
if (!is_int($minLength))
throw new ConstraintException('Can not create an Integer constraint without a min value');
// verify the max value
if (!is_int($maxLength))
throw new ConstraintException('Can not create an Integer constraint without a max value');
// assign the min and max values
$this->_minLength = $minLength;
$this->_maxLength = $maxLength;
}
/**
* validate that the passed paramater matches the constraint. Tests Constraint tests
* as well as min ans max values for the paramater.
*
* @param mixed $param the paramater to test
* @return Boolean true if validation succeeds
*/
public function validate($param, $message = false)
{
// validate the type and nullability
parent::validate($param);
// if it's an allowed null value, then return success
if ($this->_isNull)
return true;
$length = strlen($param);
$res = '';
if ($length < $this->_minLength)
$res = ($message) ? $message : 'constraint: ' . $this->_name . ' may not be less than ' . $this->_minLength . ' / ' . $param;
// validate the max
if ($length > $this->_maxLength)
$res = ($message) ? $message : 'constraint: ' . $this->_name . ' may not be greater than ' . $this->_maxLength . ' / ' . $param;
if ($res)
throw new ConstraintException ($res);
// life is good
return true;
}
}