/** * @return array */ public static function exportRules(Rules $rules) { $payload = []; foreach ($rules as $rule) { if (!is_string($op = $rule->validator)) { if (!Nette\Utils\Callback::isStatic($op)) { continue; } $op = Nette\Utils\Callback::toString($op); } if ($rule->branch) { $item = ['op' => ($rule->isNegative ? '~' : '') . $op, 'rules' => static::exportRules($rule->branch), 'control' => $rule->control->getHtmlName()]; if ($rule->branch->getToggles()) { $item['toggle'] = $rule->branch->getToggles(); } elseif (!$item['rules']) { continue; } } else { $item = ['op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => Validator::formatMessage($rule, FALSE)]; } if (is_array($rule->arg)) { $item['arg'] = []; foreach ($rule->arg as $key => $value) { $item['arg'][$key] = $value instanceof IControl ? ['control' => $value->getHtmlName()] : $value; } } elseif ($rule->arg !== NULL) { $item['arg'] = $rule->arg instanceof IControl ? ['control' => $rule->arg->getHtmlName()] : $rule->arg; } $payload[] = $item; } if ($payload && $rules->isOptional()) { array_unshift($payload, ['op' => 'optional']); } return $payload; }