Separate common methods into BaseParser class

This commit is contained in:
Kijin Sung 2020-06-29 11:31:11 +09:00
parent fa5f70c0e9
commit e1df71bc38
3 changed files with 70 additions and 70 deletions

View file

@ -5,7 +5,7 @@ namespace Rhymix\Framework\Parsers;
/**
* Module action (conf/module.xml) parser class for XE compatibility.
*/
class ModuleActionParser
class ModuleActionParser extends BaseParser
{
/**
* Shortcuts for route definition.
@ -57,7 +57,7 @@ class ModuleActionParser
foreach ($xml->grants->grant ?: [] as $grant)
{
$grant_info = new \stdClass;
$grant_info->title = self::_getElementsByLang($grant, 'title', $lang);
$grant_info->title = self::_getChildrenByLang($grant, 'title', $lang);
$grant_info->default = trim($grant['default']);
$grant_name = trim($grant['name']);
$info->grant->{$grant_name} = $grant_info;
@ -67,7 +67,7 @@ class ModuleActionParser
foreach ($xml->menus->menu ?: [] as $menu)
{
$menu_info = new \stdClass;
$menu_info->title = self::_getElementsByLang($menu, 'title', $lang);
$menu_info->title = self::_getChildrenByLang($menu, 'title', $lang);
$menu_info->index = null;
$menu_info->acts = array();
$menu_info->type = trim($menu['type']);
@ -224,34 +224,4 @@ class ModuleActionParser
$result->vars = $vars;
return $result;
}
/**
* Get child elements that match a language.
*
* @param SimpleXMLElement $parent
* @param string $tag_name
* @param string $lang
* @return string
*/
protected static function _getElementsByLang(\SimpleXMLElement $parent, string $tag_name, string $lang): string
{
// If there is a child element that matches the language, return it.
foreach ($parent->{$tag_name} as $child)
{
$attribs = $child->attributes('xml', true);
if (strval($attribs['lang']) === $lang)
{
return trim($child);
}
}
// Otherwise, return the first child element.
foreach ($parent->{$tag_name} as $child)
{
return trim($child);
}
// If there are no child elements, return an empty string.
return '';
}
}