trovit / ip

此包已被废弃,不再维护。作者建议使用 darsyn/ip 包。

不可变IP地址值对象,提供多种表示法,包括辅助函数。

维护者

详细信息

github.com/trovit/ip

主页

源代码

安装: 700

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 10

分支: 3

2.1.1 2016-05-23 07:48 UTC

This package is not auto-updated.

Last update: 2016-06-09 10:28:15 UTC


README

分支说明

此仓库是 darsyn/ip 的分支,增加了额外功能。

  • 对象可扩展性(使用PHP延迟静态绑定)
  • 新增公共方法 isVersion4、isVersion6 和 getVersion
  • 仅在一次(在构造函数中)查找IP版本
  • 代码复用
  • 更新PHPDoc-blocks

完整的变更列表在原始项目的以下pull request中。

IP是一个不可变值对象,提供了同一IP值的多种表示法,包括一些用于广播和网络地址的辅助函数,以及根据CIDR(子网掩码)是否在其范围内。

尽管它处理IPv4和IPv6表示法,但它将两者转换为16字节二进制序列,以便进行简单的数学运算和一致性(例如,将IPv4和IPv6存储在数据库的同一列中)。

安装

使用 Composer

composer require trovit/ip

需求

在PHP 5.4上,从IP地址(如 fd0a:238b:4a96::)转换的字符串(以一系列空字符结尾)会被截断,这意味着转换后的二进制序列是无效的。因此,仅支持PHP版本5.5及以上。

其次,由于依赖于内置PHP函数 inet_ptonint_ntop,此库无法处理32位系统上的IPv6地址,但将来可能提供解决方案。

示例用法

<?php

use Trovit\IP\IP;

/**
 * Basic Usage
 */

$ip = new IP('12.34.56.78');
$ip->getShortAddress();        // string(11) "12.34.56.78"
$ip->getLongAddress();         // string(23) "0000:0000:0000:0000:0000:0000:0c22:384e"
$ip->getVersion();             // int(4)
$ip->isVersion(IP::VERSION_6); // bool(false)

// The IP address is stored inside the object as a 16-byte binary sequence. To access
// that use either the getBinary() method, or the __toString() magic method.

$binary = $ip->getBinary();
$binary = (string) $ip;

/**
 * Static Helper
 */

IP::validate('192.168.0.1');          // bool(true)
IP::validate('256.168.0.1');          // bool(false)
IP::validate('2001:4860:4860::8844'); // bool(true)
IP::validate('2001:4860:4860:8844');  // bool(false)

/**
 * Caveats
 */

// isVersion() and getVersion() use the 16-byte binary sequence to determine the IP
// address version, *NOT* the protocol notation that it was in when supplied to the
// constructor. This may cause confusion when you supply some IPv6 addresses - such
// as "::1" (the IPv6 notation for localhost) which would be reported as a version 4
// address.
$ip = new IP('::c22:384e');
$ip->getShortAddress();            // string(11) "12.34.56.78"
$ip->getVersion();                 // int(4)
$ip->isVersion(IP::VERSION_6);     // bool(false)

// Anyone who has worked with CIDR notation before will be used to a subnet mask
// between 0 and 32. However, because this library deals with IPv4 and IPv6
// interchangeably the subnet mask ranges from 0 to 128 instead. When working with
// IPv4 addresses, you must add 96 to the IPv4 subnet mask (therefore making it an
// IPv6 subnet mask) to get the correct integer to pass to the following methods.
$clientIp = new IP('12.48.183.1');
$clientIp->inRange($ip, 96 + 11);  // bool(true)
$clientIp->inRange($ip, 96 + 24);  // bool(false)

/**
 * Advanced
 */

$ip = new IP('12.34.56.78');

// Get the network address of an IP address given a subnet mask.
$networkIp = $ip->getNetworkIP(96 + 19);
$networkIp->getShortAddress();   // string() "12.34.32.0"

// Get the broadcast address of an IP address given a subnet mask.
$broadcastIp = $ip->getBroadcastIp(96 + 19);
$broadcastIp->getShortAddress(); // string() "12.34.63.255"

Doctrine支持

此库可以用于支持Doctrine中的IP地址列类型

<?php

use Doctrine\DBAL\Types\Type;

Type::addType('ip', 'Trovit\IP\Doctrine\IpType');

如果您使用 Symfony,请在主配置中添加以下内容

doctrine:
    dbal:
        types:
            ip: Trovit\IP\Doctrine\IpType

现在您可以在实体中随意存储IP地址

<?php

use Trovit\IP\IP;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class AnalyticsEntity
{
    /**
     * @ORM\Column(type="ip")
     */
    protected $ipAddress;

    public function getIpAddress()
    {
        return $this->ipAddress;
    }

    public function setIpAddress(IP $ip)
    {
        $this->ipAddress = $ip;
    }
}

许可

请参阅此仓库中包含的单独的许可文件以获取MIT许可证的完整副本,该项目基于此许可证。

作者

如果您做出了贡献(提交了pull request),不要忘记在此处添加您的名字!