Add convenience functions for XML parsing

This commit is contained in:
Kijin Sung 2023-08-11 02:37:39 +09:00
parent 57be6abc9d
commit 9e13c5ee6e
2 changed files with 57 additions and 11 deletions

View file

@ -28,6 +28,52 @@ abstract class BaseParser
return $result;
}
/**
* Get the string value of an XML attribute after normalizing its name.
*
* @param SimpleXMLElement $element
* @param string $name
* @return string
*/
protected static function _getAttributeString(\SimpleXMLElement $element, string $name): string
{
$normalized_name = strtolower(preg_replace('/[^a-zA-Z]/', '', $name));
foreach ($element->attributes() as $key => $val)
{
$normalized_key = strtolower(preg_replace('/[^a-zA-Z]/', '', $key));
if ($normalized_key === $normalized_name)
{
return trim($val);
}
}
return '';
}
/**
* Get the boolean value of an XML attribute after normalizing its name.
*
* A value that is identical to the name of the attribute will be treated as true.
* Other values will be passed to toBool() for evaluation.
*
* @param SimpleXMLElement $element
* @param string $name
* @return bool
*/
protected static function _getAttributeBool(\SimpleXMLElement $element, string $name): bool
{
$normalized_name = strtolower(preg_replace('/[^a-zA-Z]/', '', $name));
foreach ($element->attributes() as $key => $val)
{
$normalized_key = strtolower(preg_replace('/[^a-zA-Z]/', '', $key));
if ($normalized_key === $normalized_name)
{
$normalized_val = strtolower(preg_replace('/[^a-zA-Z]/', '', $val));
return ($normalized_key === $normalized_val) || toBool($val);
}
}
return false;
}
/**
* Get the contents of child elements that match a language.
*

View file

@ -187,40 +187,40 @@ class ModuleActionParser extends BaseParser
$action_info->method = implode('|', $methods);
$action_info->route = $route_arg;
$action_info->standalone = $standalone;
$action_info->check_csrf = (trim($action['check_csrf'] ?? '') ?: trim($action['check-csrf'] ?? '')) === 'false' ? 'false' : 'true';
$action_info->meta_noindex = (trim($action['meta_noindex'] ?? '') ?: trim($action['meta-noindex'] ?? '')) === 'true' ? 'true' : 'false';
$action_info->check_csrf = self::_getAttributeString($action, 'check-csrf') === 'false' ? 'false' : 'true';
$action_info->meta_noindex = self::_getAttributeString($action, 'meta-noindex') === 'true' ? 'true' : 'false';
$action_info->global_route = $global_route;
$info->action->{$action_name} = $action_info;
// Set the menu name and index settings.
$menu_name = trim($action['menu_name'] ?? '');
$menu_name = self::_getAttributeString($action, 'menu-name');
if ($menu_name && isset($info->menu->{$menu_name}))
{
$info->menu->{$menu_name}->acts[] = $action_name;
if (toBool($action['menu_index']))
if (self::_getAttributeBool($action, 'menu_index'))
{
$info->menu->{$menu_name}->index = $action_name;
}
}
if (toBool($action['index']))
if (self::_getAttributeBool($action, 'index'))
{
$info->default_index_act = $action_name;
}
if (toBool($action['admin_index']))
if (self::_getAttributeBool($action, 'admin_index'))
{
$info->admin_index_act = $action_name;
}
if (toBool($action['setup_index']))
if (self::_getAttributeBool($action, 'setup_index'))
{
$info->setup_index_act = $action_name;
}
if (toBool($action['simple_setup_index']))
if (self::_getAttributeBool($action, 'simple_setup_index'))
{
$info->simple_setup_index_act = $action_name;
}
// Set error handler settings.
$error_handlers = explode(',', trim($action['error_handlers'] ?? '') ?: trim($action['error-handlers'] ?? ''));
$error_handlers = explode(',', self::_getAttributeString($action, 'error-handlers'));
foreach ($error_handlers as $error_handler)
{
if (intval($error_handler) > 200)
@ -237,8 +237,8 @@ class ModuleActionParser extends BaseParser
if (isset($info->action->{$action_name}))
{
$info->action->{$action_name}->permission->target = trim($permission['target'] ?? '');
$info->action->{$action_name}->permission->check_var = trim($permission['check_var'] ?? '') ?: trim($permission['check-var'] ?? '');
$info->action->{$action_name}->permission->check_type = trim($permission['check_type'] ?? '') ?: trim($permission['check-type'] ?? '');
$info->action->{$action_name}->permission->check_var = self::_getAttributeString($permission, 'check-var');
$info->action->{$action_name}->permission->check_type = self::_getAttributeString($permission, 'check-type');
}
}