rincler/domain
Composer 安装命令:
composer require rincler/domain
包简介
A domain name value object that supports IDN and Punycode, FQDN, validates input, and provides easy access to the domain level, labels, zone, TLD, eTLD and eTLD+1.
README 文档
README
A domain name value object that supports IDN and Punycode, FQDN, validates input, and provides easy access to the domain level, labels, zone, TLD, eTLD and eTLD+1.
Usage
<?php use \Rincler\Domain\Domain; $domain = new Domain('sub.example.com'); echo $domain; // sub.example.com echo $domain->asIDN(); // sub.example.com echo $domain->asPunycode(); // sub.example.com echo $domain->zone(); // example.com echo $domain->without($domain->zone()); // sub echo $domain->TLD(); // com echo $domain->without($domain->TLD()); // sub.example echo $domain->level(); // 3 echo $domain->labels(); // [new Domain('com'), new Domain('example'), new Domain('sub')] echo $domain->label(2); // example echo $domain->sliceToLevel(2); // example.com echo $domain->absolute(); // sub.example.com. $domain = new Domain('пример.рф'); echo $domain->asIDN(); // пример.рф echo $domain->asPunycode(); // xn--e1afmkfd.xn--p1ai $domain = new Domain('xn--e1afmkfd.xn--p1ai'); echo $domain->asIDN(); // пример.рф echo $domain->asPunycode(); // xn--e1afmkfd.xn--p1ai var_dump($domain->equals(new Domain('xn--e1afmkfd.xn--p1ai'))) // true var_dump($domain->equals(new Domain('пример.рф'))) // true var_dump($domain->equals(new Domain('суб.пример.рф'))) // false var_dump(Domain::isValid('example.com')) // true var_dump(Domain::isValid('пример.рф')) // true var_dump(Domain::isValid('exam_ple.com')) // false var_dump(Domain::isValid('.example.com')) // false $domain = new Domain('sub.example.com.'); var_dump($domain->isFQDN()) // true echo $domain // sub.example.com. echo $domain->zone() // example.com. echo $domain->relative(); // sub.example.com
Installation
composer require rincler/domain
Documentation
- static
isValid(): bool- Returnstrueif the domain is valid,falseotherwise. __constructor(string $domain)- The constructor validates the domain and throwsInvalidDomainExceptionif it is not valid, then creates the value object.asIDN(): string- Returns the domain in IDN format.asPunycode(): string- Returns the domain in Punycode format.equals(Domain $domain): bool- Returnstrueif the current domain equals the specified domain,falseotherwise.level(): int- Returns the number of levels in the domain.zone(): Domain- Returns the domain zone.TLD(): Domain- Returns the domain’s top-level domain (TLD).eTLD(): ?Domain- Returns the domain’s effective top-level domain (eTLD).eTLDPlusOne(): ?Domain- Returns the domain’s effective top-level domain plus the next label (eTLD+1).without(Domain $suffix): ?Domain- Returns the domain without the specified suffix.labels(): Domain[]- Returns an array containing all labels of the domain.label(int $level): Domain- Returns the label corresponding to the given domain level.sliceToLevel(int $level): Domain- Returns the domain up to the specified level.absolute(): Domain- Returns the absolute form of the domain (FQDN) with a trailing dot.isAbsolute(): bool- Returnstrueif the domain is absolute (FQDN),falseotherwise.FQDN(): Domain- Alias forabsolute().isFQDN(): bool- Alias forisAbsolute().relative(): Domain- Returns the relative form of the domain.isRoot(): bool- Returnstrueif the domain is root,falseotherwise.clone(): Domain- Returns a copy of the domain.__toString(): string- Magic method returning the domain equivalent toasIDN().
URL Domain
Domains used in URLs cannot be absolute (with a dot at the end) and do not have a root zone.
In such cases, you can use the UrlDomain class instead of Domain.
eTLD
See eTLD and Public Suffix List.
To use eTLDs, you must specify an eTLD provider via the eTLDSetProvider method:
<?php use \Rincler\Domain\Domain; Domain::eTLDSetProvider(static function () { return ['net.ru', 'org.ru']; }); $domain = new Domain('sub.example.net.ru'); echo $domain->eTLD(); // net.ru echo $domain->eTLDPlusOne(); // example.net.ru
Why PHP >= 7.3?
Domain validation in the intl extension was fixed in 7.3.0. See http://bugs.php.net/76829
Development
Running tests in Docker:
- Build the Docker image:
docker build -t php:8.4-cli-intl .
- Run the tests:
docker run --rm -v $(pwd):/app/ php:8.4-cli-intl php vendor/bin/phpunit
License
This library is released under the MIT license.
rincler/domain 适用场景与选型建议
rincler/domain 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「domain」 「tld」 「idn」 「punycode」 「fqdn」 「etld」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rincler/domain 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rincler/domain 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rincler/domain 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library for encoding and decoding internationalized domain names
idna-convert library based on http://idnaconv.phlymail.de/
Package for encoding of Unicode for Internationalized Domain Names in Applications (IDNA)
PHP library to convert Domain Names correctly from IDN to Punycode, and vice-versa also offers TR46 processing.
OO adaptation of Florian Sager's regdom library for querying Mozilla Public Suffix List
Updated the hostname validator class with the latest TLD's
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-04