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

@ -0,0 +1,60 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* This class provides common methods for other parser classes to use.
*/
abstract class BaseParser
{
/**
* Get all attributes of an element as an associative array.
*
* @param SimpleXMLElement $element
* @param bool $remove_symbols
* @return array
*/
protected static function _getAttributes(\SimpleXMLElement $element, $remove_symbols = true): array
{
$result = array();
foreach ($element->attributes() as $key => $val)
{
if ($remove_symbols)
{
$key = preg_replace('/[^a-z]/', '', $key);
}
$result[trim($key)] = trim($val);
}
return $result;
}
/**
* Get the contents of child elements that match a language.
*
* @param SimpleXMLElement $parent
* @param string $tag_name
* @param string $lang
* @return string
*/
protected static function _getChildrenByLang(\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 '';
}
}

View file

@ -5,7 +5,7 @@ namespace Rhymix\Framework\Parsers;
/** /**
* Module action (conf/module.xml) parser class for XE compatibility. * Module action (conf/module.xml) parser class for XE compatibility.
*/ */
class ModuleActionParser class ModuleActionParser extends BaseParser
{ {
/** /**
* Shortcuts for route definition. * Shortcuts for route definition.
@ -57,7 +57,7 @@ class ModuleActionParser
foreach ($xml->grants->grant ?: [] as $grant) foreach ($xml->grants->grant ?: [] as $grant)
{ {
$grant_info = new \stdClass; $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_info->default = trim($grant['default']);
$grant_name = trim($grant['name']); $grant_name = trim($grant['name']);
$info->grant->{$grant_name} = $grant_info; $info->grant->{$grant_name} = $grant_info;
@ -67,7 +67,7 @@ class ModuleActionParser
foreach ($xml->menus->menu ?: [] as $menu) foreach ($xml->menus->menu ?: [] as $menu)
{ {
$menu_info = new \stdClass; $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->index = null;
$menu_info->acts = array(); $menu_info->acts = array();
$menu_info->type = trim($menu['type']); $menu_info->type = trim($menu['type']);
@ -224,34 +224,4 @@ class ModuleActionParser
$result->vars = $vars; $result->vars = $vars;
return $result; 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 '';
}
} }

View file

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