Files
nl-admin-api/system/validation/rules/with_callback.php

48 lines
945 B
PHP
Raw Normal View History

2011-06-21 20:11:17 -05:00
<?php namespace System\Validation\Rules;
use System\Validation\Nullable_Rule;
2011-06-21 20:11:17 -05:00
class With_Callback extends Nullable_Rule {
2011-06-21 20:11:17 -05:00
/**
* The callback that will be used to validate the attribute.
2011-06-21 20:11:17 -05:00
*
* @var function
*/
public $callback;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
2011-06-21 20:11:17 -05:00
*/
public function check($attribute, $attributes)
{
if ( ! is_callable($this->callback))
2011-06-21 20:11:17 -05:00
{
throw new \Exception("The validation callback for the [$attribute] attribute is not callable.");
2011-06-21 20:11:17 -05:00
}
if ( ! is_null($nullable = parent::check($attribute, $attributes)))
2011-06-21 20:11:17 -05:00
{
return $nullable;
2011-06-21 20:11:17 -05:00
}
return call_user_func($this->callback, $attributes[$attribute]);
}
/**
* Set the validation callback.
*
* @param function $callback
2011-06-21 20:11:17 -05:00
* @return With_Callback
*/
public function using($callback)
{
$this->callback = $callback;
return $this;
}
}