1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
<?php
namespace Coast\Validator\Rule;
use Coast\Validator\Rule;
class Custom extends Rule
{
protected $_func = [];
public function __construct(callable $func, $name = null)
{
$this->func($func);
$this->name($name);
}
public function func($func = null)
{
if (func_num_args() > 0) {
$this->_func = $func;
return $this;
}
return $this->_func;
}
protected function _validate($value, $context = null)
{
$func = $this->_func;
$func($value, $this, $context);
}
public function error($error = null, array $params = array())
{
if (!is_array($error)) {
$error = [$this->name(), $error, $params];
}
$this->_errors[] = $error;
return $this;
}
}