Query.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace React\Dns\Query;
  3. use React\Dns\Model\Message;
  4. /**
  5. * This class represents a single question in a query/response message
  6. *
  7. * It uses a structure similar to `\React\Dns\Message\Record`, but does not
  8. * contain fields for resulting TTL and resulting record data (IPs etc.).
  9. *
  10. * @link https://tools.ietf.org/html/rfc1035#section-4.1.2
  11. * @see \React\Dns\Message\Record
  12. */
  13. final class Query
  14. {
  15. /**
  16. * @var string query name, i.e. hostname to look up
  17. */
  18. public $name;
  19. /**
  20. * @var int query type (aka QTYPE), see Message::TYPE_* constants
  21. */
  22. public $type;
  23. /**
  24. * @var int query class (aka QCLASS), see Message::CLASS_IN constant
  25. */
  26. public $class;
  27. /**
  28. * @param string $name query name, i.e. hostname to look up
  29. * @param int $type query type, see Message::TYPE_* constants
  30. * @param int $class query class, see Message::CLASS_IN constant
  31. */
  32. public function __construct($name, $type, $class)
  33. {
  34. $this->name = $name;
  35. $this->type = $type;
  36. $this->class = $class;
  37. }
  38. /**
  39. * Describes the hostname and query type/class for this query
  40. *
  41. * The output format is supposed to be human readable and is subject to change.
  42. * The format is inspired by RFC 3597 when handling unkown types/classes.
  43. *
  44. * @return string "example.com (A)" or "example.com (CLASS0 TYPE1234)"
  45. * @link https://tools.ietf.org/html/rfc3597
  46. */
  47. public function describe()
  48. {
  49. $class = $this->class !== Message::CLASS_IN ? 'CLASS' . $this->class . ' ' : '';
  50. $type = 'TYPE' . $this->type;
  51. $ref = new \ReflectionClass('React\Dns\Model\Message');
  52. foreach ($ref->getConstants() as $name => $value) {
  53. if ($value === $this->type && \strpos($name, 'TYPE_') === 0) {
  54. $type = \substr($name, 5);
  55. break;
  56. }
  57. }
  58. return $this->name . ' (' . $class . $type . ')';
  59. }
  60. }