1
0
mirror of https://github.com/danog/dns.git synced 2025-01-22 13:31:12 +01:00

Add Record::getName to translate record types to names

This commit is contained in:
Niklas Keller 2017-09-12 17:04:41 +02:00
parent 331e359936
commit fcdf01de72

View File

@ -76,4 +76,31 @@ final class Record {
public function getTtl() {
return $this->ttl;
}
/**
* Converts an record type integer back into its name as defined in this class.
*
* Returns "unknown (<type>)" in case a name for this record is not known.
*
* @param int $type Record type as integer.
*
* @return string Name of the constant for this record in this class.
*/
public static function getName(int $type): string {
static $types;
if (0 > $type || 0xffff < $type) {
$message = \sprintf('%d does not correspond to a valid record type (must be between 0 and 65535).', $type);
throw new \Error($message);
}
if ($types === null) {
$types = \array_flip(
(new \ReflectionClass(self::class))
->getConstants()
);
}
return $types[$type] ?? "unknown ({$type})";
}
}