diff --git a/common/framework/parsers/baseparser.php b/common/framework/parsers/baseparser.php new file mode 100644 index 000000000..4fd57565b --- /dev/null +++ b/common/framework/parsers/baseparser.php @@ -0,0 +1,60 @@ +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 ''; + } +} diff --git a/common/framework/parsers/moduleactionparser.php b/common/framework/parsers/moduleactionparser.php index 9b5260cfe..15de0f84e 100644 --- a/common/framework/parsers/moduleactionparser.php +++ b/common/framework/parsers/moduleactionparser.php @@ -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 ''; - } } diff --git a/common/framework/parsers/moduleinfoparser.php b/common/framework/parsers/moduleinfoparser.php index 4b58745d1..889d08bca 100644 --- a/common/framework/parsers/moduleinfoparser.php +++ b/common/framework/parsers/moduleinfoparser.php @@ -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 ''; - } }