diff --git a/src/Decoder.php b/src/Decoder.php index 95504be..c4d10bd 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -40,18 +40,16 @@ public function __construct(?Resolver $dnsResolver = null, ?MacroStringDecoder $ } /** - * Extract the SPF record associated to a domain. + * Get the raw SPF TXT record associated to a domain. * * @throws \SPFLib\Exception\DNSResolutionException in case of DNS resolution errors * @throws \SPFLib\Exception\MultipleSPFRecordsException if the domain has more that 1 SPF record - * @throws \SPFLib\Exception\InvalidTermException if the SPF record contains invalid terms - * @throws \SPFLib\Exception\InvalidMacroStringException if the SPF record contains a term with an invalid macro-string * - * @return \SPFLib\Record|null return NULL if no SPF record has been found + * @return string returns an empty string if no SPF TXT record has been found * * @see https://tools.ietf.org/html/rfc7208#section-4.5 */ - public function getRecordFromDomain(string $domain): ?Record + public function getTXTRecordFromDomain(string $domain): string { $rawSpfRecords = []; $txtRecords = $this->getDNSResolver()->getTXTRecords($domain); @@ -62,14 +60,36 @@ public function getRecordFromDomain(string $domain): ?Record } switch (count($rawSpfRecords)) { case 0: - return null; + return ''; case 1: - return $this->getRecordFromTXT($rawSpfRecords[0]); + return $rawSpfRecords[0]; default: throw new Exception\MultipleSPFRecordsException($domain, $rawSpfRecords); } } + /** + * Extract the SPF record associated to a domain. + * + * @throws \SPFLib\Exception\DNSResolutionException in case of DNS resolution errors + * @throws \SPFLib\Exception\MultipleSPFRecordsException if the domain has more that 1 SPF record + * @throws \SPFLib\Exception\InvalidTermException if the SPF record contains invalid terms + * @throws \SPFLib\Exception\InvalidMacroStringException if the SPF record contains a term with an invalid macro-string + * + * @return \SPFLib\Record|null return NULL if no SPF record has been found + * + * @see https://tools.ietf.org/html/rfc7208#section-4.5 + */ + public function getRecordFromDomain(string $domain): ?Record + { + $txtRecord = $this->getTXTRecordFromDomain($domain); + if ($txtRecord === '') { + return null; + } + + return $this->getRecordFromTXT($txtRecord); + } + /** * Parse a TXT record and extract the SPF data. *