2012-02-05 12:56:54 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
class Validator_StringLength
|
|
|
|
implements FieldValidator
|
|
|
|
{
|
|
|
|
protected $errorPrefix;
|
|
|
|
protected $minLength;
|
|
|
|
protected $maxLength;
|
2012-02-05 23:03:18 +01:00
|
|
|
protected $allowEmpty;
|
2012-02-05 12:56:54 +01:00
|
|
|
|
2012-02-05 23:03:18 +01:00
|
|
|
public function __construct( $errorPrefix , $minLength = 0 , $maxLength = null , $allowEmpty = false )
|
2012-02-05 12:56:54 +01:00
|
|
|
{
|
|
|
|
assert( $maxLength === null || $maxLength >= $minLength );
|
|
|
|
$this->errorPrefix = $errorPrefix;
|
|
|
|
$this->minLength = $minLength;
|
|
|
|
$this->maxLength = $maxLength;
|
2012-02-05 23:03:18 +01:00
|
|
|
$this->allowEmpty = $allowEmpty;
|
2012-02-05 12:56:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function validate( $value )
|
|
|
|
{
|
|
|
|
$len = strlen( $value );
|
2012-02-05 23:03:18 +01:00
|
|
|
if ( $len < $this->minLength && ( $len != 0 || ! $this->allowEmpty ) ) {
|
2012-02-05 12:56:54 +01:00
|
|
|
$template = Loader::Text( '%1$s is too short (min. %2$d characters)' );
|
|
|
|
return array( sprintf( $template , $this->errorPrefix , $this->minLength ) );
|
|
|
|
}
|
|
|
|
if ( $this->maxLength !== null && $len > $this->maxLength ) {
|
|
|
|
$template = Loader::Text( '%1$s is too long (max. %2$d characters)' );
|
|
|
|
return array( sprintf( $template , $this->errorPrefix , $this->maxLength ) );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Validator_InArray
|
|
|
|
implements FieldValidator
|
|
|
|
{
|
|
|
|
private $values;
|
|
|
|
private $errorText;
|
|
|
|
|
|
|
|
public function __construct( array $values , $errorText )
|
|
|
|
{
|
|
|
|
$this->values = $values;
|
|
|
|
$this->errorText = $errorText;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate( $value )
|
|
|
|
{
|
|
|
|
if ( ! ( empty( $this->values ) || in_array( $value , $this->values ) ) ) {
|
|
|
|
return array( $this->errorText );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Validator_IntValue
|
|
|
|
implements FieldValidator
|
|
|
|
{
|
|
|
|
|
|
|
|
private $invalidText;
|
|
|
|
private $minValue;
|
|
|
|
private $minError;
|
|
|
|
private $maxValue;
|
|
|
|
private $maxError;
|
|
|
|
|
|
|
|
public function __construct( $invalidText )
|
|
|
|
{
|
|
|
|
$this->invalidText = $invalidText;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMinValue( $minValue , $minError = null )
|
|
|
|
{
|
|
|
|
assert( $this->maxValue === null || $minValue <= $this->maxValue );
|
|
|
|
$this->minValue = $minValue;
|
|
|
|
$this->minError = ( $minError === null ) ? $this->invalidText : $minError;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMaxValue( $maxValue , $maxError = null )
|
|
|
|
{
|
|
|
|
assert( $this->minValue === null || $maxValue >= $this->maxValue );
|
|
|
|
$this->maxValue = $maxValue;
|
|
|
|
$this->maxError = ( $maxError === null ) ? $this->invalidText : $maxError;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate( $value )
|
|
|
|
{
|
|
|
|
if ( !is_scalar( $value ) || (int) $value != $value ) {
|
|
|
|
return array( $this->invalidText );
|
|
|
|
} else if ( $this->minValue !== null && $value < $this->minValue ) {
|
|
|
|
return array( $this->minError );
|
|
|
|
} else if ( $this->maxValue !== null && $value > $this->maxValue ) {
|
|
|
|
return array( $this->maxError );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-06 00:10:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Validator_Email
|
|
|
|
implements FieldValidator
|
|
|
|
{
|
|
|
|
private $errorText;
|
|
|
|
|
|
|
|
public function __construct( $errorText )
|
|
|
|
{
|
|
|
|
$this->errorText = $errorText;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate( $value )
|
|
|
|
{
|
|
|
|
if ( preg_match( "/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/" , $value ) ) {
|
|
|
|
list( $username , $domain ) = split( '@' , $value );
|
|
|
|
if ( checkdnsrr( $domain , 'MX' ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array( $this->errorText );
|
|
|
|
}
|
|
|
|
}
|