initial commit of validation classes.

This commit is contained in:
Taylor Otwell
2011-06-21 20:11:17 -05:00
parent adb583471b
commit 081a3e512f
13 changed files with 799 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php namespace System\Validation\Rules;
use System\Input;
use System\Validation\Rule;
class Acceptance_Of extends Rule {
/**
* The value is that is considered accepted.
*
* @var string
*/
public $accepts = '1';
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return void
*/
public function check($attribute, $attributes)
{
return Input::has($attribute) and (string) Input::get($attribute) === $this->accepts;
}
/**
* Set the accepted value.
*
* @param string $value
* @return Acceptance_Of
*/
public function accepts($value)
{
$this->accepts = $value;
return $this;
}
}

View File

@@ -0,0 +1,25 @@
<?php namespace System\Validation\Rules;
use System\Input;
use System\Validation\Rule;
class Confirmation_Of extends Rule {
/**
* 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;
}
return Input::has($attribute.'_confirmation') and $attributes[$attribute] === Input::get($attribute.'_confirmation');
}
}

View File

@@ -0,0 +1,43 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
class Exclusion_Of extends Rule {
/**
* The reserved values for the attribute.
*
* @var string
*/
public $reserved;
/**
* 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;
}
return ! in_array($attributes[$attribute], $this->reserved);
}
/**
* Set the reserved values for the attribute
*
* @param array $reserved
* @return Exclusion_Of
*/
public function from($reserved)
{
$this->reserved = $reserved;
return $this;
}
}

View File

@@ -0,0 +1,43 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
class Format_Of extends Rule {
/**
* The regular expression that will be used to evaluate the attribute.
*
* @var string
*/
public $expression;
/**
* 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;
}
return preg_match($this->expression, $attributes[$attribute]);
}
/**
* Set the regular expression.
*
* @param string $expression
* @return Format_Of
*/
public function with($expression)
{
$this->expression = $expression;
return $this;
}
}

View File

@@ -0,0 +1,43 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
class Inclusion_Of extends Rule {
/**
* The accepted values for the attribute.
*
* @var string
*/
public $accepted;
/**
* 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;
}
return in_array($attributes[$attribute], $this->accepted);
}
/**
* Set the accepted values for the attribute.
*
* @param array $accepted
* @return Inclusion_Of
*/
public function in($accepted)
{
$this->accepted = $accepted;
return $this;
}
}

View File

@@ -0,0 +1,70 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
class Presence_Of extends Rule {
/**
* Indicates an empty string should be considered present.
*
* @var bool
*/
public $allow_empty = false;
/**
* Indicates a null should be considered present.
*
* @var bool
*/
public $allow_null = false;
/**
* 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 false;
}
if (is_null($attributes[$attribute]) and ! $this->allow_null)
{
return false;
}
if (trim((string) $attributes[$attribute]) === '' and ! $this->allow_empty)
{
return false;
}
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

@@ -0,0 +1,160 @@
<?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)
{
return false;
}
if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
{
return false;
}
if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))
{
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)
{
return false;
}
if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
{
return false;
}
if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
{
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 maximize 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

@@ -0,0 +1,60 @@
<?php namespace System\Validation\Rules;
use System\DB;
use System\DB\Eloquent;
use System\Validation\Rule;
class Uniqueness_Of extends Rule {
/**
* The database table that should be checked.
*
* @var string
*/
public $table;
/**
* The database column that should be checked.
*
* @var string
*/
public $column;
/**
* 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_null($this->column))
{
$this->column = $attribute;
}
return DB::table(Eloquent::table($this->table)->where($this->column, '=', $attributes[$attribute])->count() == 0;
}
/**
* Set the database table and column.
*
* @param string $table
* @param string $column
* @return Uniqueness_Of
*/
public function on($table, $column = null)
{
$this->table = $table;
$this->column = $column;
return $this;
}
}

View File

@@ -0,0 +1,48 @@
<?php namespace System\Validation\Rules;
use System\Validation\Rule;
class With_Callback extends Rule {
/**
* The callback.
*
* @var function
*/
public $callback;
/**
* 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_callable($this->callback))
{
throw new \Exception("A validation callback for the [$attribute] attribute is not callable.");
}
return call_user_func($this->callback, $attributes[$attribute]);
}
/**
* Set the validation callback.
*
* @param function $callback
* @return With_Callback
*/
public function using($callback)
{
$this->callback = $callback;
return $this;
}
}