mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Implement RulesetParser
This commit is contained in:
parent
ba638c394d
commit
a77b9a5d3a
1 changed files with 95 additions and 0 deletions
95
common/framework/parsers/RulesetParser.php
Normal file
95
common/framework/parsers/RulesetParser.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers;
|
||||
|
||||
/**
|
||||
* Ruleset XML parser class for XE compatibility.
|
||||
*/
|
||||
class RulesetParser extends BaseParser
|
||||
{
|
||||
/**
|
||||
* Load an XML file.
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $lang
|
||||
* @return ?object
|
||||
*/
|
||||
public static function loadXML(string $filename, string $lang = ''): ?object
|
||||
{
|
||||
// Load the XML file.
|
||||
$xml = simplexml_load_string(file_get_contents($filename));
|
||||
if ($xml === false)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the current language.
|
||||
$lang = $lang ?: (\Context::getLangType() ?: 'en');
|
||||
|
||||
// Initialize the result object.
|
||||
$info = new \stdClass;
|
||||
$info->rules = [];
|
||||
$info->messages = [];
|
||||
$info->filters = [];
|
||||
$info->fieldsNames = [];
|
||||
|
||||
// Parse custom rules.
|
||||
if ($xml->customrules && $xml->customrules->rule)
|
||||
{
|
||||
foreach ($xml->customrules->rule as $rule)
|
||||
{
|
||||
$def = [];
|
||||
foreach ($rule->attributes() as $key => $val)
|
||||
{
|
||||
$def[trim($key)] = trim($val);
|
||||
}
|
||||
$def['message'] = self::_getChildrenByLang($rule, 'message', $lang) ?: null;
|
||||
|
||||
$rule_name = trim($rule['name']);
|
||||
$info->rules[$rule_name] = $def;
|
||||
|
||||
if ($def['message'])
|
||||
{
|
||||
$info->messages['invalid_' . $rule_name] = $def['message'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse field filters.
|
||||
if ($xml->fields && $xml->fields->field)
|
||||
{
|
||||
foreach ($xml->fields->field as $field)
|
||||
{
|
||||
$def = [];
|
||||
foreach ($field->attributes() as $key => $val)
|
||||
{
|
||||
$def[trim($key)] = trim($val);
|
||||
}
|
||||
$def['title'] = self::_getChildrenByLang($field, 'title', $lang) ?: null;
|
||||
|
||||
if ($field->if)
|
||||
{
|
||||
foreach ($field->if as $if)
|
||||
{
|
||||
$condition = [];
|
||||
foreach ($if->attributes() as $key => $val)
|
||||
{
|
||||
$condition[trim($key)] = trim($val);
|
||||
}
|
||||
$def['if'][] = $condition;
|
||||
}
|
||||
}
|
||||
|
||||
$filter_name = trim($field['name']);
|
||||
$info->filters[$filter_name] = $def;
|
||||
|
||||
if ($def['title'])
|
||||
{
|
||||
$info->fieldsNames[$filter_name] = $def['title'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue