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
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
1of0/ip-utils 适用场景与选型建议
1of0/ip-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ipv6」 「IP」 「binary」 「subnet」 「range」 「ipaddress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 1of0/ip-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 1of0/ip-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 1of0/ip-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP classes for working with IPV4 addresses and networks.
A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.
PHP Library for manipulating network addresses (IPv4 and IPv6)
Small class for checking ip addresses
PHP Library for manipulating network addresses (IPv4 and IPv6)
Manages IPv4 and IPv6 addresses and subnets
统计信息
- 总下载量: 79
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-16