public function applyRules($field, $ruleset)
{
// Save previous values, just in case
$acc = $this->acc;
$crs = $this->current_ruleset;
$this->bail_out = false;
$this->acc = $this->get($field);
$this->current_ruleset = $ruleset;
while (!is_null($rule = $this->pullRule())) {
$this->cast = false;
$this->custom_error = null;
if ($rule == 'required') {
$is_required = true;
}
// For debugging
$tmp = null;
$this->consumed = array($rule);
try {
if ((is_object($rule) || is_array($rule)) && is_callable($rule)) {
$tmp = $rule($this, $this->acc, $field);
} else {
// For to_XX rules
if (substr($rule, 0, 3) == 'to_') {
if (!$this->hasMethod('rule_' . $rule)) {
$rule = substr($rule, 3);
}
$this->cast = true;
}
if ($rule === '') {
if ($this->cast) {
$this->set($field, $this->acc);
}
continue;
}
$rule = $this->resolveRuleAlias($rule);
$tmp = $this->{'rule_' . $rule}($this->acc, $field);
}
if ($this->debug) {
echo "<font color=blue>rule_{$rule}({$this->acc}," . implode(',', $this->consumed) . ")={$tmp}</font><br/>";
}
if (!is_null($tmp)) {
$this->acc = $tmp;
}
if ($this->cast) {
$this->set($field, $tmp);
}
if ($this->bail_out) {
break;
}
} catch (\Exception_ValidityCheck $e) {
if ($this->debug) {
echo "<font color=red>rule_{$rule}({$this->acc}," . implode(',', $this->consumed) . ') failed</font><br/>';
}
$this->acc = $acc;
$this->current_ruleset = $crs;
throw $e->setField($field)->addMoreInfo('val', $this->acc)->addMoreInfo('rule', $rule);
}
}
$this->acc = $acc;
$this->current_ruleset = $crs;
}