Search by

adachsoft / dns-zone-contracts

Arkadiusz Adach

Provider-independent PHP contracts and value objects for managing DNS zone records.

Package info

gitlab.com/a.adach/dns-zone-contracts

Issues

pkg:composer/adachsoft/dns-zone-contracts

Statistics

Installs: 7

Dependents: 2

Suggesters: 0

Stars: 0

v0.1.0 2026-11-26 13:45 UTC

This package is auto-updated.

Last update: 2026-11-26 12:45:18 UTC


README

Provider-independent PHP contracts and value objects for reading, writing, and synchronizing DNS records in a zone. Provider adapters can implement the same API for OVH, Cloudflare, or another DNS service, while workflow consumers use one stable contract for mail setup and domain-verification records.

Requirements

  • PHP 8.3 or later
  • Composer
  • adachsoft/collection 3.x

Installation

composer require adachsoft/dns-zone-contracts

Model

  • ZoneName stores a lowercase ASCII domain name without a trailing dot. One trailing dot in input is normalized away; internationalized names must be supplied in ASCII-compatible form.
  • RecordName stores a lowercase name relative to the zone. The empty string and @ denote the apex. Underscore labels such as _dmarc and _domainkey are supported. fqdn(ZoneName $zone) returns the full name without a trailing dot.
  • DnsRecord contains a relative name, DnsRecordTypeEnum, record value, optional TTL, optional MX/SRV priority, and optional provider DnsRecordId. TXT values are stored without surrounding presentation quotes; provider adapters handle quoting.
  • DnsRecordCollection is an immutable, type-safe collection of records.
  • RecordFilter optionally restricts a read by relative name and/or type.
  • DnsChangeSet reports created, updated, and deleted records and exposes isEmpty().

Record priorities are limited to MX and SRV records and must be between 0 and 65535. A null TTL means the zone's default; zero is allowed.

Services

DnsZoneReaderInterface lists records and checks whether a zone exists. DnsZoneWriterInterface creates, updates, and deletes records. Persisted records returned by a provider must have an identifier; updateRecord() requires that identifier. applyChanges() publishes pending changes for providers that require a zone refresh and can be a no-op for providers that apply changes immediately. DnsZoneInterface combines the reader and writer contracts.

Provider failures should be translated to the package exceptions: ZoneNotFoundException, RecordNotFoundException, InvalidDnsValueException, DnsAuthenticationException, DnsRateLimitedException, or DnsTransportException. All implement DnsZoneExceptionInterface.

Synchronizing records

RecordSetSynchronizer accepts any DnsZoneInterface implementation. ADD_MISSING adds records missing by relative name, type, and normalized value, and leaves existing records untouched. REPLACE_SAME_NAME_AND_TYPE reconciles every name/type pair present in the desired collection; it replaces all existing records for those pairs, including TXT records. Include every same-name/type record that must remain, or use ADD_MISSING when unrelated values must be preserved. Existing matching records retain their provider identifiers; unmatched records are reused for updates before additional records are created or excess records are deleted.

Value comparison is case-insensitive and ignores one trailing dot for CNAME, MX, and NS hostnames. TXT presentation quotes are removed (and quoted TXT character strings are concatenated) before exact comparison. MX/SRV priorities participate in replacement matching; replacement also updates a matched record when its TTL differs. Add-missing mode does not modify an existing record's TTL or priority.

Mail DNS example

Replace the example targets and DKIM key with values issued by the mail provider. This example synchronizes the complete desired TXT set for the apex and _dmarc; include any existing verification TXT values at those same names/types if they must be retained.

use AdachSoft\DnsZoneContracts\Model\DnsRecord;
use AdachSoft\DnsZoneContracts\Model\DnsRecordCollection;
use AdachSoft\DnsZoneContracts\Model\DnsRecordTypeEnum;
use AdachSoft\DnsZoneContracts\Model\RecordName;
use AdachSoft\DnsZoneContracts\Model\SyncModeEnum;
use AdachSoft\DnsZoneContracts\Model\ZoneName;
use AdachSoft\DnsZoneContracts\Service\RecordSetSynchronizer;
use AdachSoft\DnsZoneContracts\Service\DnsZoneInterface;

/** @var DnsZoneInterface $dnsZone Provider adapter configured by the application */
$zoneName = new ZoneName('example.com');
$desiredRecords = new DnsRecordCollection([
    new DnsRecord(new RecordName(''), DnsRecordTypeEnum::MX, 'mx1.mail-provider.example', 3600, 10),
    new DnsRecord(new RecordName(''), DnsRecordTypeEnum::MX, 'mx2.mail-provider.example', 3600, 20),
    new DnsRecord(new RecordName(''), DnsRecordTypeEnum::TXT, 'v=spf1 include:spf.mail-provider.example ~all', 3600),
    new DnsRecord(
        new RecordName('selector._domainkey'),
        DnsRecordTypeEnum::TXT,
        'v=DKIM1; k=rsa; p=REPLACE_WITH_PROVIDER_KEY',
        3600,
    ),
    new DnsRecord(
        new RecordName('_dmarc'),
        DnsRecordTypeEnum::TXT,
        'v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com',
        3600,
    ),
]);

$changes = (new RecordSetSynchronizer($dnsZone))->ensure(
    $zoneName,
    $desiredRecords,
    SyncModeEnum::REPLACE_SAME_NAME_AND_TYPE,
);

if ($changes->isEmpty() === false) {
    // The provider adapter has published the changes before ensure() returns.
    printf("Created %d, updated %d, and deleted %d records.\n", count($changes->created), count($changes->updated), count($changes->deleted));
}

Use ADD_MISSING instead when records should only be added and no existing values should be changed or removed. To preview an operation without writing or publishing changes, pass dryRun: true to ensure(); the returned DnsChangeSet contains the planned records.

Implementing a provider

Implement DnsZoneInterface and map the provider API to the package model:

  1. Convert provider record names to relative RecordName values and return a DnsRecordId for every persisted record.
  2. Translate provider record types, values, TTLs, and MX/SRV priorities to DnsRecord instances. Keep TXT values unquoted in the contract model and quote/unquote them inside the adapter as required by the provider.
  3. Require a record identifier for updates and use identifiers for update/delete operations.
  4. Implement applyChanges() as the provider's explicit zone refresh when needed; use a no-op when changes are immediate.
  5. Translate provider authentication, rate-limit, missing-zone/record, invalid-value, and transport failures to the corresponding package exceptions.

InMemoryDnsZone is included as a working implementation for tests and small integrations. It registers ZoneName instances in its constructor and exposes appliedCount() to verify calls to applyChanges().