1
0
mirror of https://github.com/danog/dns.git synced 2024-12-03 18:07:53 +01:00
dns/lib/Addr/RequestBuilder.php
Chris Wright d5b5192ba3 Finished initial implementation
zOMG too many things to document
2014-06-15 00:47:15 +01:00

65 lines
1.5 KiB
PHP

<?php
namespace Addr;
use LibDNS\Messages\MessageFactory,
LibDNS\Messages\MessageTypes,
LibDNS\Records\QuestionFactory,
LibDNS\Records\ResourceQTypes,
LibDNS\Encoder\Encoder;
class RequestBuilder
{
/**
* @var MessageFactory
*/
private $messageFactory;
/**
* @var QuestionFactory
*/
private $questionFactory;
/**
* @var Encoder
*/
private $encoder;
/**
* Constructor
*
* @param MessageFactory $messageFactory
* @param QuestionFactory $questionFactory
* @param Encoder $encoder
*/
public function __construct(MessageFactory $messageFactory, QuestionFactory $questionFactory, Encoder $encoder)
{
$this->messageFactory = $messageFactory;
$this->questionFactory = $questionFactory;
$this->encoder = $encoder;
}
/**
* Build a request packet for a name and record type
*
* @param int $id
* @param string $name
* @param int $type
* @return string
*/
public function buildRequest($id, $name, $type)
{
$qType = $type === AddressModes::INET4_ADDR ? ResourceQTypes::A : ResourceQTypes::AAAA;
$question = $this->questionFactory->create($qType);
$question->setName($name);
$request = $this->messageFactory->create(MessageTypes::QUERY);
$request->setID($id);
$request->getQuestionRecords()->add($question);
$request->isRecursionDesired(true);
return $this->encoder->encode($request);
}
}