enhanced validation, added mimes config array.

This commit is contained in:
Taylor Otwell
2011-06-27 17:33:07 -05:00
parent bd3d8f6347
commit d51be02dd2
19 changed files with 841 additions and 411 deletions

View File

@@ -17,7 +17,7 @@ class Acceptance_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
@@ -27,7 +27,7 @@ class Acceptance_Of extends Rule {
/**
* Set the accepted value.
*
* @param string $value
* @param string $value
* @return Acceptance_Of
*/
public function accepts($value)

View File

@@ -10,7 +10,7 @@ class Confirmation_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{

View File

@@ -1,8 +1,8 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Exclusion_Of extends Rule {
class Exclusion_Of extends Nullable_Rule {
/**
* The reserved values for the attribute.
@@ -16,13 +16,13 @@ class Exclusion_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return true;
return $nullable;
}
return ! in_array($attributes[$attribute], $this->reserved);
@@ -31,7 +31,7 @@ class Exclusion_Of extends Rule {
/**
* Set the reserved values for the attribute
*
* @param array $reserved
* @param array $reserved
* @return Exclusion_Of
*/
public function from($reserved)

View File

@@ -1,8 +1,8 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Format_Of extends Rule {
class Format_Of extends Nullable_Rule {
/**
* The regular expression that will be used to validate the attribute.
@@ -16,13 +16,13 @@ class Format_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return true;
return $nullable;
}
return preg_match($this->expression, $attributes[$attribute]);
@@ -31,7 +31,7 @@ class Format_Of extends Rule {
/**
* Set the regular expression.
*
* @param string $expression
* @param string $expression
* @return Format_Of
*/
public function using($expression)

View File

@@ -1,8 +1,8 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Inclusion_Of extends Rule {
class Inclusion_Of extends Nullable_Rule {
/**
* The accepted values for the attribute.
@@ -16,13 +16,13 @@ class Inclusion_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return true;
return $nullable;
}
return in_array($attributes[$attribute], $this->accepted);
@@ -31,7 +31,7 @@ class Inclusion_Of extends Rule {
/**
* Set the accepted values for the attribute.
*
* @param array $accepted
* @param array $accepted
* @return Inclusion_Of
*/
public function in($accepted)

View File

@@ -0,0 +1,49 @@
<?php namespace System\Validation\Rules;
use System\Str;
use System\Validation\Rangable_Rule;
class Length_Of extends Rangable_Rule {
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return $nullable;
}
$value = trim((string) $attributes[$attribute]);
// ---------------------------------------------------------
// Validate the exact length of the attribute.
// ---------------------------------------------------------
if ( ! is_null($this->size) and Str::length($value) !== $this->size)
{
$this->error = 'string_wrong_size';
}
// ---------------------------------------------------------
// Validate the maximum length of the attribute.
// ---------------------------------------------------------
elseif ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
{
$this->error = 'string_too_big';
}
// ---------------------------------------------------------
// Validate the minimum length of the attribute.
// ---------------------------------------------------------
elseif ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
{
$this->error = 'string_too_small';
}
return is_null($this->error);
}
}

View File

@@ -0,0 +1,116 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rangable_Rule;
class Numericality_Of extends Rangable_Rule {
/**
* Indicates that the attribute must be an integer.
*
* @var bool
*/
public $only_integer = false;
/**
* The "not valid" error message.
*
* @var string
*/
public $not_valid;
/**
* The "not integer" error message.
*
* @var string
*/
public $not_integer;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return $nullable;
}
// ---------------------------------------------------------
// Validate the attribute is a number.
// ---------------------------------------------------------
if ( ! is_numeric($attributes[$attribute]))
{
$this->error = 'number_not_valid';
}
// ---------------------------------------------------------
// Validate the attribute is an integer.
// ---------------------------------------------------------
elseif ($this->only_integer and filter_var($attributes[$attribute], FILTER_VALIDATE_INT) === false)
{
$this->error = 'number_not_integer';
}
// ---------------------------------------------------------
// Validate the exact size of the attribute.
// ---------------------------------------------------------
elseif ( ! is_null($this->size) and $attributes[$attribute] != $this->size)
{
$this->error = 'number_wrong_size';
}
// ---------------------------------------------------------
// Validate the maximum size of the attribute.
// ---------------------------------------------------------
elseif ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
{
$this->error = 'number_too_big';
}
// ---------------------------------------------------------
// Validate the minimum size of the attribute.
// ---------------------------------------------------------
elseif ( ! is_null($this->minimum) and $attributes[$attribute] < $this->minimum)
{
$this->error = 'number_too_small';
}
return is_null($this->error);
}
/**
* Specify that the attribute must be an integer.
*
* @return Numericality_Of
*/
public function only_integer()
{
$this->only_integer = true;
return $this;
}
/**
* Set the "not valid" error message.
*
* @param string $message
* @return Numericality_Of
*/
public function not_valid($message)
{
$this->not_valid = $message;
return $this;
}
/**
* Set the "not integer" error message.
*
* @param string $message
* @return Numericality_Of
*/
public function not_integer($message)
{
$this->not_integer = $message;
return $this;
}
}

View File

@@ -1,70 +1,29 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Presence_Of extends Rule {
/**
* Indicates an empty string should be considered present.
*
* @var bool
*/
public $allow_empty = false;
/**
* Indicates null should be considered present.
*
* @var bool
*/
public $allow_null = false;
class Presence_Of extends Nullable_Rule {
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return false;
}
if (is_null($attributes[$attribute]) and ! $this->allow_null)
{
return false;
}
if (trim((string) $attributes[$attribute]) === '' and ! $this->allow_empty)
{
return false;
return $nullable;
}
// ---------------------------------------------------------
// The Nullable_Rule check method essentially is a check for
// the presence of an attribute, so there is no further
// checking that needs to be done.
// ---------------------------------------------------------
return true;
}
/**
* Allow an empty string to be considered present.
*
* @return Presence_Of
*/
public function allow_empty()
{
$this->allow_empty = true;
return $this;
}
/**
* Allow a null to be considered present.
*
* @return Presence_Of
*/
public function allow_null()
{
$this->allow_null = true;
return $this;
}
}

View File

@@ -1,166 +0,0 @@
<?php namespace System\Validation\Rules;
use System\Str;
use System\Validation\Rule;
class Size_Of extends Rule {
/**
* The exact size the attribute must be.
*
* @var int
*/
public $length;
/**
* The maximum size of the attribute.
*
* @var int
*/
public $maximum;
/**
* The minimum size of the attribute.
*
* @var int
*/
public $minimum;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
{
return true;
}
if (is_numeric($attributes[$attribute]))
{
return $this->check_number($attribute, $attributes);
}
else
{
return $this->check_string($attribute, $attributes);
}
}
/**
* Evaluate the validity of a numeric attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
*/
private function check_number($attribute, $attributes)
{
if ( ! is_null($this->length) and $attributes[$attribute] !== $this->length)
{
$this->error = 'number_wrong_size';
return false;
}
if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
{
$this->error = 'number_too_big';
return false;
}
if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))
{
$this->error = 'number_too_small';
return false;
}
return true;
}
/**
* Evaluate the validity of a string attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
*/
public function check_string($attribute, $attributes)
{
$value = trim((string) $attributes[$attribute]);
if ( ! is_null($this->length) and Str::length($value) !== $this->length)
{
$this->error = 'string_wrong_size';
return false;
}
if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
{
$this->error = 'string_too_big';
return false;
}
if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
{
$this->error = 'string_too_small';
return false;
}
return true;
}
/**
* Set the exact size the attribute must be.
*
* @param int $length
* @return Size_Of
*/
public function is($length)
{
$this->length = $length;
return $this;
}
/**
* Set the minimum and maximum size of the attribute.
*
* @param int $minimum
* @param int $maximum
* @return Size_Of
*/
public function between($minimum, $maximum)
{
$this->minimum = $minimum;
$this->maximum = $maximum;
return $this;
}
/**
* Set the minimum size the attribute.
*
* @param int $minimum
* @return Size_Of
*/
public function at_least($minimum)
{
$this->minimum = $minimum;
return $this;
}
/**
* Set the maximum size the attribute.
*
* @param int $maximum
* @return Size_Of
*/
public function less_than($maximum)
{
$this->maximum = $maximum;
return $this;
}
}

View File

@@ -1,9 +1,9 @@
<?php namespace System\Validation\Rules;
use System\DB;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Uniqueness_Of extends Rule {
class Uniqueness_Of extends Nullable_Rule {
/**
* The database table that should be checked.
@@ -24,13 +24,13 @@ class Uniqueness_Of extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return true;
return $nullable;
}
if (is_null($this->column))
@@ -44,8 +44,11 @@ class Uniqueness_Of extends Rule {
/**
* Set the database table and column.
*
* @param string $table
* @param string $column
* The attribute name will be used as the column name if no other
* column name is specified.
*
* @param string $table
* @param string $column
* @return Uniqueness_Of
*/
public function on($table, $column = null)

View File

@@ -2,9 +2,9 @@
use System\File;
use System\Input;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class Upload_Of extends Rule {
class Upload_Of extends Nullable_Rule {
/**
* The acceptable file types.
@@ -20,38 +20,72 @@ class Upload_Of extends Rule {
*/
public $maximum;
/**
* The "wrong type" error message.
*
* @var string
*/
public $wrong_type;
/**
* The "too big" error message.
*
* @var string
*/
public $too_big;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
// -----------------------------------------------------
// Check the presence of the upload. If the upload does
// not exist and the upload is required, a presence_of
// error will be raised.
//
// Otherwise no error will be raised.
// -----------------------------------------------------
if ( ! array_key_exists($attribute, Input::file()))
{
return true;
if ( ! $this->allow_null)
{
$this->error = 'presence_of';
}
return is_null($this->error);
}
// -----------------------------------------------------
// Uploaded files are stored in the $_FILES array, so
// we use that array instead of the $attributes.
// -----------------------------------------------------
$file = Input::file($attribute);
if ( ! is_null($this->maximum) and $file['size'] > $this->maximum)
if ( ! is_null($this->maximum) and $file['size'] > $this->maximum * 1000)
{
$this->error = 'file_too_big';
return false;
}
// -----------------------------------------------------
// The File::is method uses the Fileinfo PHP extension
// to determine the MIME type of the file.
// -----------------------------------------------------
foreach ($this->types as $type)
{
if ( ! File::is($type, $file['tmp_name']))
if (File::is($type, $file['tmp_name']))
{
$this->error = 'file_wrong_type';
return false;
break;
}
$this->error = 'file_wrong_type';
}
return true;
return is_null($this->error);
}
/**
@@ -66,15 +100,61 @@ class Upload_Of extends Rule {
}
/**
* Set the maximum file size in bytes.
* Require that the uploaded file is an image type.
*
* @param int $maximum
* @return Upload_Of
*/
public function less_than($maximum)
public function is_image()
{
$this->types = array_merge($this->types, array('jpg', 'gif', 'png', 'bmp'))
return $this;
}
/**
* Set the maximum file size in kilobytes.
*
* @param int $maximum
* @return Upload_Of
*/
public function maximum($maximum)
{
$this->maximum = $maximum;
return $this;
}
/**
* Set the validation error message.
*
* @param string $message
* @return Upload_Of
*/
public function message($message)
{
return $this->wrong_type($message)->too_big($message);
}
/**
* Set the "wrong type" error message.
*
* @param string $message
* @return Upload_Of
*/
public function wrong_type($message)
{
$this->wrong_type = $message;
return $this;
}
/**
* Set the "too big" error message.
*
* @param string $message
* @return Upload_Of
*/
public function too_big($message)
{
$this->too_big = $message;
return $this;
}
}

View File

@@ -1,8 +1,8 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
use System\Validation\Nullable_Rule;
class With_Callback extends Rule {
class With_Callback extends Nullable_Rule {
/**
* The callback that will be used to validate the attribute.
@@ -16,27 +16,27 @@ class With_Callback extends Rule {
*
* @param string $attribute
* @param array $attributes
* @return void
* @return bool
*/
public function check($attribute, $attributes)
{
if ( ! array_key_exists($attribute, $attributes))
{
return true;
}
if ( ! is_callable($this->callback))
{
throw new \Exception("The validation callback for the [$attribute] attribute is not callable.");
}
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
{
return $nullable;
}
return call_user_func($this->callback, $attributes[$attribute]);
}
/**
* Set the validation callback.
*
* @param function $callback
* @param function $callback
* @return With_Callback
*/
public function using($callback)