layershifter/tld-database

公共后缀列表的数据库抽象

1.0.69 2019-08-04 10:03 UTC

README

请使用 https://github.com/jeremykendall/php-domain-parser.

TLDDatabase

Packagist.org Build Status Code Climate codecov

PHP中公共后缀列表的抽象层。由TLDExtract使用。

库的主要思想是提供对公共后缀列表实际数据库的简单快速访问。库始终附带实际数据库。

此软件包符合PSR-1PSR-2PSR-4规范。如果您注意到符合规范的疏漏,请通过拉取请求发送补丁。

需求

以下版本的PHP得到支持

  • PHP 5.5
  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • HHVM

基本用法

首先需要加载Store类。

$store = new \LayerShifter\TLDDatabase\Store();

为了检查数据库中是否存在条目,需要使用isExists方法

$store->isExists(string $suffix) : bool;

$store->isExists('com'); // true
$store->isExists('comcom'); // false

注意:后缀必须不带前导点。

为了获取后缀的类型,需要使用getType方法

为了检查数据库中是否存在条目,需要使用isExists方法

$store->isExists(string $suffix): int;

$store->getType('com'); // \LayerShifter\TLDDatabase\Store::TYPE_ICANN = 1
$store->getType('s3.amazonaws.com'); // \LayerShifter\TLDDatabase\Store::TYPE_PRIVATE = 2

如果条目不存在,方法将抛出异常,否则它将返回一个整型常量之一

  • \LayerShifter\TLDDatabase\Store::TYPE_ICANN;
  • \LayerShifter\TLDDatabase\Store::TYPE_PRIVATE;

为了直接检查类型,可以使用isICANNisPrivate方法。

$store->isICANN(string $suffix) : bool;

$store->isICANN'com'); // true
$store->isICANN('s3.amazonaws.com'); // false

$store->isPrivate(string $suffix) : bool;

$store->isPrivate('com'); // false
$store->isPrivate('s3.amazonaws.com'); // true

高级用法

为开发者提供了一些酷炫的功能。

自定义数据库

如果您需要操作自定义(非打包)数据库,只需将参数添加到Store构造函数即可。

$store = new \LayerShifter\TLDDatabase\Store(string $filename);
$store = new \LayerShifter\TLDDatabase\Store(__DIR__ . '/cache/datatabase.php');

更新

如果您使用自定义数据库,则需要更新它。因此,您可以使用Update类。

$update = new \LayerShifter\TLDDatabase\Update(string $filename);
$update = new \LayerShifter\TLDDatabase\Update(__DIR__ . '/cache/datatabase.php');

$update->run();

HTTP适配器

基本上,库使用cURL适配器进行更新,但您也可以使用自定义适配器。

class customHttp implements \LayerShifter\TLDDatabase\Http\AdapterInterface {
    public function get() {} 
}

$update = new \LayerShifter\TLDDatabase\Update(__DIR__ . '/cache/datatabase.php', 'customHttp');
$update->run();

安装

通过Composer

$ composer require layershifter/tld-database

测试

$ composer test

版本控制

库使用SemVer版本控制。其中

  • 主要版本进行不兼容的API更改;
  • 次要版本增加功能,完全向后兼容;
  • 补丁是公共后缀列表的数据库更新。

数据库每周更新一次。

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

许可证

此库在Apache 2.0许可证下发布。请参阅许可证文件以获取更多信息。