mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-01 08:12:17 +09:00
Add convenience functions for XML parsing
This commit is contained in:
parent
57be6abc9d
commit
9e13c5ee6e
2 changed files with 57 additions and 11 deletions
|
|
@ -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.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue