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,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;
}
}