initial commit of validation classes.
This commit is contained in:
39
system/validation/rules/acceptance_of.php
Normal file
39
system/validation/rules/acceptance_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
25
system/validation/rules/confirmation_of.php
Normal file
25
system/validation/rules/confirmation_of.php
Normal 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');
|
||||
}
|
||||
|
||||
}
|
||||
43
system/validation/rules/exclusion_of.php
Normal file
43
system/validation/rules/exclusion_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
43
system/validation/rules/format_of.php
Normal file
43
system/validation/rules/format_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
43
system/validation/rules/inclusion_of.php
Normal file
43
system/validation/rules/inclusion_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
70
system/validation/rules/presence_of.php
Normal file
70
system/validation/rules/presence_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
160
system/validation/rules/size_of.php
Normal file
160
system/validation/rules/size_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
60
system/validation/rules/uniqueness_of.php
Normal file
60
system/validation/rules/uniqueness_of.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
48
system/validation/rules/with_callback.php
Normal file
48
system/validation/rules/with_callback.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user