1
0
mirror of https://github.com/danog/LibDNSNative.git synced 2024-11-30 04:29:07 +01:00
LibDNSNative/lib/NativeEncoder.php

59 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2019-07-15 14:59:42 +02:00
<?php declare(strict_types = 1);
2019-07-15 14:41:26 +02:00
/**
* Encodes Message objects to query strings.
*
* PHP version 5.4
*
* @category LibDNS
* @package Encoder
* @author Daniil Gentili <https://daniil.it>, Chris Wright <https://github.com/DaveRandom>
* @copyright Copyright (c) Chris Wright <https://github.com/DaveRandom>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.0.0
*/
namespace danog\LibDNSNative;
use \LibDNS\Messages\Message;
2019-07-15 14:59:42 +02:00
use LibDNS\Messages\MessageTypes;
2019-07-15 14:41:26 +02:00
/**
* Encodes Message objects to query strings.
*
* @category LibDNS
* @package Encoder
* @author Daniil Gentili <https://daniil.it>, Chris Wright <https://github.com/DaveRandom>
*/
class NativeEncoder
{
/**
* Encode a Message to URL payload.
*
* @param \LibDNS\Messages\Message $message The Message to encode
* @return array Array of parameters to pass to the \dns_get_record function
*/
public function encode(Message $message): array
{
if ($message->getType() !== MessageTypes::QUERY) {
throw new \InvalidArgumentException('Invalid question: is not a question record');
}
$questions = $message->getQuestionRecords();
if ($questions->count() === 0) {
throw new \InvalidArgumentException('Invalid question: 0 question records provided');
}
if ($questions->count() !== 1) {
throw new \InvalidArgumentException('Invalid question: only one question record can be provided at a time');
}
2019-07-15 14:59:42 +02:00
2019-07-15 14:41:26 +02:00
$question = $questions->getRecordByIndex(0);
2019-07-15 14:59:42 +02:00
2019-07-15 14:41:26 +02:00
return [
\implode('.', $question->getName()->getLabels()), // Name
$question->getType(), // Type
null, // Authority records
null, // Additional records
true, // Raw results
];
}
}