rincler/domain

域名值对象

3.0.2 2020-02-02 13:17 UTC

This package is auto-updated.

Last update: 2024-09-29 05:35:49 UTC


README

Packagist Version GitHub Workflow Status PHP from Packagist (specify version)

域名值对象

使用

<?php

use \Rincler\Domain\Domain;

$domain = new Domain('sub.domain.com');
echo $domain; // sub.domain.com
echo $domain->asIdn(); // sub.domain.com
echo $domain->asPunycode(); // sub.domain.com
echo $domain->getZone(); // domain.com
echo $domain->getWithoutZone(); // sub
echo $domain->getTld(); // com
echo $domain->getWithoutTld(); // sub.domain
echo $domain->getLevel(); // 3

$domain = new Domain('домен.рф');
echo $domain->asIdn(); // домен.рф
echo $domain->asPunycode(); // xn--d1acufc.xn--p1ai

$domain = new Domain('xn--d1acufc.xn--p1ai');
echo $domain->asIdn(); // домен.рф
echo $domain->asPunycode(); // xn--d1acufc.xnn

安装

    composer require rincler/domain

文档

  • static isValid(): bool - 如果域名有效则返回 true,否则返回 false
  • __constructor(string $domain) - 验证域名(如果域名无效则抛出 InvalidDomainException)并创建域名对象
  • asIdn(): string - 返回IDN格式的域名字符串
  • asPunycode(): string - 返回Punycode格式的域名字符串
  • getLevel(): int - 返回域名层级数量
  • getZone(): Domain - 返回域名的区域
  • getWithoutZone(): Domain - 返回不包含区域的域名
  • getTld(): Domain - 返回顶级域名
  • getWithoutTld(): Domain - 返回不包含顶级域名的域名
  • equals(Domain $domain): bool - 如果当前域名等于检查的域名则返回 true,否则返回 false
  • __toString(): string - 与 asIdn 类似

为什么需要PHP >= 7.3?

在7.3.0版本中修复了intl扩展中域名验证的问题。请参阅http://bugs.php.net/76829

许可证

此库根据MIT许可证发布。