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 info (conf/info.xml) parser class for XE compatibility.
*/
class ModuleInfoParser
class ModuleInfoParser extends BaseParser
{
/**
* Load an XML file.
@ -34,8 +34,8 @@ class ModuleInfoParser
// Parse version 0.2
if ($version === '0.2')
{
$info->title = self::_getElementsByLang($xml, 'title', $lang);
$info->description = self::_getElementsByLang($xml, 'description', $lang);
$info->title = self::_getChildrenByLang($xml, 'title', $lang);
$info->description = self::_getChildrenByLang($xml, 'description', $lang);
$info->version = trim($xml->version);
$info->homepage = trim($xml->homepage);
$info->category = trim($xml->category) ?: 'service';
@ -47,7 +47,7 @@ class ModuleInfoParser
foreach ($xml->author as $author)
{
$author_info = new \stdClass;
$author_info->name = self::_getElementsByLang($author, 'name', $lang);
$author_info->name = self::_getChildrenByLang($author, 'name', $lang);
$author_info->email_address = trim($author['email_address']);
$author_info->homepage = trim($author['link']);
$info->author[] = $author_info;
@ -57,8 +57,8 @@ class ModuleInfoParser
// Parse version 0.1
else
{
$info->title = self::_getElementsByLang($xml, 'title', $lang);
$info->description = self::_getElementsByLang($xml->author, 'description', $lang);
$info->title = self::_getChildrenByLang($xml, 'title', $lang);
$info->description = self::_getChildrenByLang($xml->author, 'description', $lang);
$info->version = trim($xml['version']);
$info->homepage = trim($xml->homepage);
$info->category = trim($xml['category']) ?: 'service';
@ -70,7 +70,7 @@ class ModuleInfoParser
foreach ($xml->author as $author)
{
$author_info = new \stdClass;
$author_info->name = self::_getElementsByLang($author, 'name', $lang);
$author_info->name = self::_getChildrenByLang($author, 'name', $lang);
$author_info->email_address = trim($author['email_address']);
$author_info->homepage = trim($author['link']);
$info->author[] = $author_info;
@ -88,34 +88,4 @@ class ModuleInfoParser
// Return the complete result.
return $info;
}
/**
* 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 '';
}
}