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.
*