1of0/ip-utils 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

1of0/ip-utils

Composer 安装命令:

composer require 1of0/ip-utils

包简介

A couple of simple wrapper classes / utilities to make handling of IP addresses and subnets easier

README 文档

README

pipeline status coverage report

What

A couple of simple wrapper classes / utilities to make handling of IP addresses and subnets easier.

License

This project is licensed under the MIT license. See LICENSE.md.

Requirements

  • php >= 7.4

Installation

composer require 1of0/ip-utils

Why

Disclaimer: there are some other libraries around that may be more extensive:

However, one didn't exactly have the interface I was envisioning, one depended on bcmath, and one had some approaches that I didn't really agree with. That said, they all seem to have good test coverage and in-the-field usage, so take your pick!

So mainly I built this to get the interface that I had in mind, and to have zero dependencies other than php >= 7.4.

Usage

See the generated phpdoc for a full API specification.

Basic usage

/** @noinspection ForgottenDebugOutputInspection */
use OneOfZero\IpUtils\Factory;
use OneOfZero\IpUtils\IpAddress;
use OneOfZero\IpUtils\Subnet;

/** @var IpAddress $ip */
$ip = Factory::get()->parse('192.168.0.123');

/** @var Subnet $subnet */
$subnet = Factory::get()->parse('192.168.0.0/24');

var_dump($subnet->contains($ip));
// bool(true)

print_r([
    (string)$subnet->getAddress(),
    (string)$subnet->getNetworkAddress(),
    (string)$subnet->getRouterAddress(),
    (string)$subnet->getBroadcastAddress(),
    (string)$subnet->getSubnetMask(),
    $subnet->getCidr(),
    $subnet->getPrefixLength(),
    $subnet->getIdentifier(),
]);
// Array
// (
//     [0] => 192.168.0.0
//     [1] => 192.168.0.0
//     [2] => 192.168.0.1
//     [3] => 192.168.0.255
//     [4] => 255.255.255.0
//     [5] => 192.168.0.0/24
//     [6] => 24
//     [7] => c0a80000ffffff00
// )

Collections

Also check out ImmutableCollection!

/** @noinspection ForgottenDebugOutputInspection */
use OneOfZero\IpUtils\IpAddress;
use OneOfZero\IpUtils\Subnet;
use OneOfZero\IpUtils\Collection;

$collection = new Collection([
    '192.168.0.123',
    '10.0.0.0/8',
    IpAddress::parse('127.0.0.1'),
    Subnet::parseCidr('192.168.0.0/24'),
]);

$collection->addItem('::1');

print_r($collection->getStringRepresentations());
// Array
// (
//     [0] => 192.168.0.123
//     [1] => 10.0.0.0/8
//     [2] => 127.0.0.1
//     [3] => 192.168.0.0/24
//     [4] => ::1
// )

$collection->filterOutRedundantItems();

print_r($collection->getStringRepresentations());
// Array
// (
//     [0] => 10.0.0.0/8
//     [1] => 127.0.0.1
//     [2] => 192.168.0.0/24
//     [3] => ::1
// )

Specific design choices

It's not a bug, it's a feature!

Subnet behaviour

When a Subnet instance is created with an IP address that is not the network address (e.g. 192.168.0.10/24 instead of 192.168.0.0/24), the IP address will be retained and returned via getAddress() and getCidr().

However, it will not be used by methods such as contains(), equals(), and getIdentifier().

This affects the behaviour of the Subnet in a Collection.

For example:

use OneOfZero\IpUtils\Collection;

$collection = new Collection(['192.168.0.10/24']);

$collection->getStringRepresentations();
// ['192.168.0.10/24']

$collection->addItem('192.168.0.20/24');

$collection->getStringRepresentations();
// ['192.168.0.20/24'] first item was replaced because both Subnets have the same identifier

统计信息

  • 总下载量: 79
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固