mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 01:01:41 +09:00
Use RulesetParser instead of XeXmlParser in Validator
This commit is contained in:
parent
a77b9a5d3a
commit
66e040b3f9
2 changed files with 22 additions and 130 deletions
|
|
@ -30,31 +30,25 @@ class Validator
|
||||||
* rule list
|
* rule list
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
var $_rules;
|
var $_rules = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* filter list
|
* filter list
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
var $_filters;
|
var $_filters = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* custom message list
|
* custom message list
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
var $_message;
|
var $_message = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* custom field name list
|
* custom field name list
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
var $_fieldNames;
|
var $_fieldNames = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Can usable status for multibyte string function
|
|
||||||
* @var boolean
|
|
||||||
*/
|
|
||||||
var $_has_mb_func;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* validator version
|
* validator version
|
||||||
|
|
@ -75,12 +69,10 @@ class Validator
|
||||||
*/
|
*/
|
||||||
function __construct($xml_path = '')
|
function __construct($xml_path = '')
|
||||||
{
|
{
|
||||||
$this->_rules = array();
|
if ($xml_path)
|
||||||
$this->_filters = array();
|
{
|
||||||
$this->_xml_ruleset = NULL;
|
|
||||||
|
|
||||||
if($xml_path)
|
|
||||||
$this->load($xml_path);
|
$this->load($xml_path);
|
||||||
|
}
|
||||||
|
|
||||||
// predefined rules
|
// predefined rules
|
||||||
$this->addRule(array(
|
$this->addRule(array(
|
||||||
|
|
@ -93,8 +85,7 @@ class Validator
|
||||||
'float' => '/^\d+(\.\d+)?$/'
|
'float' => '/^\d+(\.\d+)?$/'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->_has_mb_func = is_callable('mb_strlen');
|
$this->_cache_dir = RX_BASEDIR . 'files/cache';
|
||||||
$this->setCacheDir(RX_BASEDIR . 'files/cache');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -114,112 +105,26 @@ class Validator
|
||||||
*/
|
*/
|
||||||
function load($xml_path)
|
function load($xml_path)
|
||||||
{
|
{
|
||||||
$this->_xml_ruleset = NULL;
|
if (!file_exists($xml_path) || !is_readable($xml_path))
|
||||||
if(!is_readable($xml_path))
|
|
||||||
{
|
{
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parser = new XeXmlParser();
|
$output = Rhymix\Framework\Parsers\RulesetParser::loadXML($xml_path);
|
||||||
$xml = $parser->loadXmlFile($xml_path);
|
if (!$output)
|
||||||
if(!isset($xml->ruleset) || !isset($xml->ruleset->fields) || !isset($xml->ruleset->fields->field))
|
|
||||||
{
|
{
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rules = array();
|
if ($output->rules)
|
||||||
$messages = array();
|
|
||||||
|
|
||||||
// custom rules
|
|
||||||
if(isset($xml->ruleset->customrules) && isset($xml->ruleset->customrules->rule))
|
|
||||||
{
|
{
|
||||||
$customrules = $xml->ruleset->customrules->rule;
|
$this->addRule($output->rules);
|
||||||
if(!is_array($customrules))
|
|
||||||
{
|
|
||||||
$customrules = array($customrules);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($customrules as $rule)
|
|
||||||
{
|
|
||||||
if(!isset($rule->attrs) || !isset($rule->attrs->name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$message = isset($rule->message) ? $rule->message->body : NULL;
|
|
||||||
$rule = (array) $rule->attrs;
|
|
||||||
$rule['message'] = $message;
|
|
||||||
$name = $rule['name'];
|
|
||||||
unset($rule['name']);
|
|
||||||
|
|
||||||
$rules[$name] = $rule;
|
|
||||||
if(isset($message))
|
|
||||||
{
|
|
||||||
$messages['invalid_' . $name] = $message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(count($rules))
|
|
||||||
{
|
|
||||||
$this->addRule($rules);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$this->_filters = $output->filters;
|
||||||
// filters
|
$this->_message = $output->messages;
|
||||||
$fields = $xml->ruleset->fields->field;
|
$this->_fieldNames = $output->fieldsNames;
|
||||||
if(!is_array($fields))
|
|
||||||
{
|
|
||||||
$fields = array($fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
$filters = array();
|
|
||||||
$fieldsNames = array();
|
|
||||||
foreach($fields as $field)
|
|
||||||
{
|
|
||||||
$name = '';
|
|
||||||
$filter = array();
|
|
||||||
|
|
||||||
if(!isset($field->attrs) || !isset($field->attrs->name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$title = isset($field->title) ? $field->title->body : NULL;
|
|
||||||
$filter = (array) $field->attrs;
|
|
||||||
$filter['title'] = $title;
|
|
||||||
|
|
||||||
$name = $filter['name'];
|
|
||||||
if(isset($title))
|
|
||||||
{
|
|
||||||
$fieldsNames[$name] = $title;
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($filter['name']);
|
|
||||||
|
|
||||||
// conditional statement
|
|
||||||
if(isset($field->if))
|
|
||||||
{
|
|
||||||
$if = $field->if;
|
|
||||||
if(!is_array($if))
|
|
||||||
{
|
|
||||||
$if = array($if);
|
|
||||||
}
|
|
||||||
foreach($if as $idx => $cond)
|
|
||||||
{
|
|
||||||
$if[$idx] = (array) $cond->attrs;
|
|
||||||
}
|
|
||||||
$filter['if'] = $if;
|
|
||||||
}
|
|
||||||
|
|
||||||
$filters[$name] = $filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->_xml_ruleset = $xml->ruleset;
|
|
||||||
$this->_filters = $filters;
|
|
||||||
$this->_message = $messages;
|
|
||||||
$this->_fieldNames = $fieldsNames;
|
|
||||||
$this->_xml_path = $xml_path;
|
$this->_xml_path = $xml_path;
|
||||||
|
return true;
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -389,7 +294,7 @@ class Validator
|
||||||
$strbytes = strlen($value);
|
$strbytes = strlen($value);
|
||||||
if(!$is_min_b || !$is_max_b)
|
if(!$is_min_b || !$is_max_b)
|
||||||
{
|
{
|
||||||
$strlength = $this->_has_mb_func ? mb_strlen($value, 'utf-8') : $this->mbStrLen($value);
|
$strlength = mb_strlen($value, 'UTF-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
if(($min && $min > ($is_min_b ? $strbytes : $strlength)) || ($max && $max < ($is_max_b ? $strbytes : $strlength)))
|
if(($min && $min > ($is_min_b ? $strbytes : $strlength)) || ($max && $max < ($is_max_b ? $strbytes : $strlength)))
|
||||||
|
|
@ -630,21 +535,6 @@ class Validator
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* if not supported 'mb_strlen' function, this method can use.
|
|
||||||
* @param string $str
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
function mbStrLen($str)
|
|
||||||
{
|
|
||||||
$arr = count_chars($str);
|
|
||||||
for($i = 0x80; $i < 0xc0; $i++)
|
|
||||||
{
|
|
||||||
unset($arr[$i]);
|
|
||||||
}
|
|
||||||
return array_sum($arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns compiled javascript file path. The path begins from XE root directory.
|
* Returns compiled javascript file path. The path begins from XE root directory.
|
||||||
* @return string Compiled JavaScript file path
|
* @return string Compiled JavaScript file path
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ class RulesetParser extends BaseParser
|
||||||
$def[trim($key)] = trim($val);
|
$def[trim($key)] = trim($val);
|
||||||
}
|
}
|
||||||
$def['message'] = self::_getChildrenByLang($rule, 'message', $lang) ?: null;
|
$def['message'] = self::_getChildrenByLang($rule, 'message', $lang) ?: null;
|
||||||
|
unset($def['name']);
|
||||||
|
|
||||||
$rule_name = trim($rule['name']);
|
$rule_name = trim($rule['name']);
|
||||||
$info->rules[$rule_name] = $def;
|
$info->rules[$rule_name] = $def;
|
||||||
|
|
@ -66,6 +67,7 @@ class RulesetParser extends BaseParser
|
||||||
$def[trim($key)] = trim($val);
|
$def[trim($key)] = trim($val);
|
||||||
}
|
}
|
||||||
$def['title'] = self::_getChildrenByLang($field, 'title', $lang) ?: null;
|
$def['title'] = self::_getChildrenByLang($field, 'title', $lang) ?: null;
|
||||||
|
unset($def['name']);
|
||||||
|
|
||||||
if ($field->if)
|
if ($field->if)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue