Commit 5836e1d5 authored by Samuel Williams's avatar Samuel Williams
Browse files

Added APL Rdata type.

parent 0f46a37c
{
"name": "badcow/dns",
"description": "A PHP library for creating DNS zone files based on RFC1035",
"stability": "dev",
"authors": [
{
"name": "Sam Williams",
......@@ -9,7 +8,8 @@
}
],
"require": {
"php": "~7.1"
"php": "~7.1",
"rlanvin/php-ip": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~7.3"
......
<?php
namespace Badcow\DNS\Rdata;
use Badcow\DNS\ResourceRecord;
class APL implements RdataInterface, FormattableInterface
{
use RdataTrait;
use FormattableTrait;
const TYPE = 'APL';
/**
* @var \IPBlock[]
*/
private $includedAddressRanges = [];
/**
* @var \IPBlock[]
*/
private $excludedAddressRanges = [];
/**
* @param \IPBlock $ipBlock
* @param bool $included True if the resource exists within the range, False if the resource
* is not within the range. I.E. the negation.
*/
public function addAddressRange(\IPBlock $ipBlock, $included = true)
{
if ($included) {
$this->includedAddressRanges[] = $ipBlock;
} else {
$this->excludedAddressRanges[] = $ipBlock;
}
}
/**
* @return \IPBlock[]
*/
public function getIncludedAddressRanges(): array
{
return $this->includedAddressRanges;
}
/**
* @return \IPBlock[]
*/
public function getExcludedAddressRanges(): array
{
return $this->excludedAddressRanges;
}
/**
* {@inheritdoc}
*/
public function output()
{
$string = '';
foreach ($this->includedAddressRanges as $ipBlock) {
$string .= (4 === $ipBlock->getVersion())? '1:' : '2:';
$string .= (string) $ipBlock.' ';
}
foreach ($this->excludedAddressRanges as $ipBlock) {
$string .= (4 === $ipBlock->getVersion())? '!1:' : '!2:';
$string .= (string) $ipBlock.' ';
}
return rtrim($string, ' ');
}
/**
* {@inheritdoc}
*/
public function outputFormatted()
{
$string = ResourceRecord::MULTILINE_BEGIN.PHP_EOL;
foreach (explode(' ', $this->output()) as $block) {
$string .= $this->makeLine($block);
}
return $string.str_repeat(' ', $this->padding).ResourceRecord::MULTILINE_END;
}
/**
* {@inheritdoc}
*/
public function longestVarLength()
{
$l = 0;
foreach (explode(' ', $this->output()) as $block) {
$l = ($l < strlen($block)) ? strlen($block) : $l;
}
return $l;
}
}
\ No newline at end of file
......@@ -329,4 +329,24 @@ class Factory
return $rdata;
}
/**
* @param \IPBlock[] $includedRanges
* @param \IPBlock[] $excludedRanges
* @return APL
*/
public static function Apl(array $includedRanges = [], array $excludedRanges = []): APL
{
$rdata = new APL();
foreach ($includedRanges as $ipBlock) {
$rdata->addAddressRange($ipBlock, true);
}
foreach ($excludedRanges as $ipBlock) {
$rdata->addAddressRange($ipBlock, false);
}
return $rdata;
}
}
<?php
namespace Badcow\DNS\Tests\Rdata;
use Badcow\DNS\Rdata\APL;
use PHPUnit\Framework\TestCase;
class AplTest extends TestCase
{
public function testOutput()
{
$apl = new APL;
$apl->addAddressRange(\IPBlock::create('192.168.0.0/23'));
$apl->addAddressRange(\IPBlock::create('192.168.1.64/28'), false);
$apl->addAddressRange(\IPBlock::create('2001:acad:1::/112'), true);
$apl->addAddressRange(\IPBlock::create('2001:acad:1::8/128'), false);
$expectation = '1:192.168.0.0/23 2:2001:acad:1::/112 !1:192.168.1.64/28 !2:2001:acad:1::8/128';
$this->assertEquals($expectation, $apl->output());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment