Commit 40e48e58 authored by samuelwilliams's avatar samuelwilliams
Browse files

Deprecated Validator::validate().

parent 8f4a714a
......@@ -175,8 +175,10 @@ class ValidatorTest extends TestCase
604800,
3600
));
$zone->addResourceRecord($soa);
$this->assertEquals(Validator::ZONE_TOO_MANY_SOA, Validator::zone($zone));
Validator::validate($zone);
}
......@@ -199,8 +201,10 @@ class ValidatorTest extends TestCase
604800,
3600
));
$zone->addResourceRecord($soa);
$this->assertEquals(Validator::ZONE_NO_NS, Validator::zone($zone));
Validator::validate($zone);
}
......@@ -218,6 +222,8 @@ class ValidatorTest extends TestCase
$a->setComment('This class does not belong here');
$zone->addResourceRecord($a);
$this->assertEquals(Validator::ZONE_TOO_MANY_CLASSES, Validator::zone($zone));
Validator::validate($zone);
}
......@@ -230,6 +236,15 @@ class ValidatorTest extends TestCase
$this->assertTrue(Validator::validate($zone));
}
/**
*
*/
public function testZone()
{
$zone = $this->buildTestZone();
$this->assertEquals(Validator::ZONE_OKAY, Validator::zone($zone));
}
public function testWildcard()
{
$valid_1 = '*.example.com.';
......
......@@ -16,6 +16,18 @@ use Badcow\DNS\Rdata\SoaRdata;
class Validator
{
const ZONE_OKAY = 0;
const ZONE_NO_SOA = 1;
const ZONE_TOO_MANY_SOA = 2;
const ZONE_NO_NS = 4;
const ZONE_NO_CLASS = 8;
const ZONE_TOO_MANY_CLASSES = 16;
/**
* Validates if $string is suitable as an RR name.
*
......@@ -210,7 +222,9 @@ class Validator
* RFC-1035 especially that:
* 1) 5.2.1 All RRs in the file should be of the same class.
* 2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
* 3) There is at least one NS record.
*
* @deprecated
* @param ZoneInterface $zone
*
* @throws ZoneException
......@@ -219,38 +233,115 @@ class Validator
*/
public static function validate(ZoneInterface $zone)
{
$number_soa = 0;
$number_ns = 0;
$n_soa = self::countResourceRecords($zone, SoaRdata::TYPE);
$n_ns = self::countResourceRecords($zone, NsRdata::TYPE);
$classes = [];
foreach ($zone->getResourceRecords() as $rr) {
/* @var $rr ResourceRecordInterface */
if (SoaRdata::TYPE === $rr->getRdata()->getType()) {
$number_soa += 1;
if (null !== $rr->getClass()) {
$classes[$rr->getClass()] = null;
}
}
if (NsRdata::TYPE === $rr->getRdata()->getType()) {
$number_ns += 1;
}
$n_class = count($classes);
if (1 !== $n_soa) {
throw new ZoneException(sprintf('There must be exactly one SOA record, %s given.', $n_soa));
}
if ($n_ns < 1) {
throw new ZoneException(sprintf('There must be at least one NS record, %s given.', $n_ns));
}
if (1 !== $n_class) {
throw new ZoneException(sprintf('There must be exactly one type of class, %s given.', $n_class));
}
return true;
}
/**
* Validates that the zone meets
* RFC-1035 especially that:
* 1) 5.2.1 All RRs in the file should be of the same class.
* 2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
* 3) There is at least one NS record.
*
* Return values are:
* - ZONE_NO_SOA
* - ZONE_TOO_MANY_SOA
* - ZONE_NO_NS
* - ZONE_NO_CLASS
* - ZONE_TOO_MANY_CLASSES
* - ZONE_OKAY
*
* You SHOULD compare these return values to the defined constants of this
* class rather than against integers directly.
*
* @param ZoneInterface $zone
*
* @return integer
*/
public static function zone(ZoneInterface $zone)
{
$n_soa = self::countResourceRecords($zone, SoaRdata::TYPE);
$n_ns = self::countResourceRecords($zone, NsRdata::TYPE);
$classes = [];
foreach ($zone->getResourceRecords() as $rr) {
if (null !== $rr->getClass()) {
$classes[$rr->getClass()] = null;
}
}
if ($number_soa !== 1) {
throw new ZoneException(sprintf('There must be exactly one SOA record, %s given.', $number_soa));
$n_class = count($classes);
if ($n_soa < 1) {
return self::ZONE_NO_SOA;
}
if ($number_ns < 1) {
throw new ZoneException(sprintf('There must be at least one NS record, %s given.', $number_ns));
if ($n_soa > 1) {
return self::ZONE_TOO_MANY_SOA;
}
if (1 !== $c = count($classes)) {
throw new ZoneException(sprintf('There must be exactly one type of class, %s given.', $c));
if ($n_ns < 1) {
return self::ZONE_NO_NS;
}
return true;
if ($n_class < 1) {
return self::ZONE_NO_CLASS;
}
if ($n_class > 1) {
return self::ZONE_TOO_MANY_CLASSES;
}
return self::ZONE_OKAY;
}
/**
* @param ZoneInterface $zone
* @param null $type The ResourceRecord type to be counted. If NULL, then the method will return
* the total number of resource records.
*
* @return int The number of records to be counted.
*/
public static function countResourceRecords(ZoneInterface $zone, $type = null)
{
if (null === $type) {
return count($zone->getResourceRecords());
}
$n = 0;
foreach ($zone->getResourceRecords() as $rr) {
/* @var $rr ResourceRecordInterface */
if ($type === $rr->getRdata()->getType()) {
$n += 1;
}
}
return $n;
}
/**
......
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