/** * The internal _parse() method uses a fairly simple regular expression to match key/value pairs * within the Doc Comment string returned by any of our _get…Meta() methods * * @param type $comment * @return type */ protected function _parse($comment) { $meta = array(); $pattern = '(@[a-zá-žA-ZÁ-Ž]+\\s*[a-zá-žA-ZÁ-Ž0-9, ()_]*)'; $matches = StringMethods::match($comment, $pattern); if ($matches != null) { foreach ($matches as $match) { $parts = ArrayMethods::clean(ArrayMethods::trim(StringMethods::split($match, '[\\s]', 2))); $meta[$parts[0]] = true; if (count($parts) > 1) { $meta[$parts[0]] = ArrayMethods::clean(ArrayMethods::trim(StringMethods::split($parts[1], ','))); } } } return $meta; }
/** * Method begins by getting a list of columns and iterating over that list. * For each column, we determine whether validation should occur. * We then split the @validate metadata into a list of validation conditions. * If a condition has arguments (e.g., max(100)), we extract the arguments. * We then run each validation method on the column data and generate error * messages for those validation conditions that failed. * We return a final true/false to indicate whether the complete validation passed or failed. * * @return type * @throws Exception\Validation */ public function validate() { $this->_errors = array(); $config = Registry::get('configuration'); $errLang = $config->system->lang; foreach ($this->columns as $column) { if ($column['validate']) { $pattern = '#[a-z]+\\(([a-zá-žA-ZÁ-Ž0-9, ]+)\\)#'; $raw = $column['raw']; $name = $column['name']; $validators = $column['validate']; $label = $column['label']; $defined = $this->getValidators(); foreach ($validators as $validator) { $function = $validator; $arguments = array($this->{$raw}); $match = StringMethods::match($validator, $pattern); if (count($match) > 0) { $matches = StringMethods::split($match[0], ',\\s*'); $arguments = array_merge($arguments, $matches); $offset = StringMethods::indexOf($validator, '('); $function = substr($validator, 0, $offset); } if (!isset($defined[$function])) { throw new Exception\Validation(sprintf('The %s validator is not defined', $function)); } $template = $defined[$function]; if (!call_user_func_array(array($this, $template['handler']), $arguments)) { $replacements = array_merge(array($label ? $label : $raw), $arguments); $message = $template['message_' . $errLang]; foreach ($replacements as $i => $replacement) { $message = str_replace("{{$i}}", $replacement, $message); } if (!isset($this->_errors[$name])) { $this->_errors[$name] = array(); } $this->_errors[$name][] = $message; } } } } return !count($this->errors); }