luka/network-address-types
Composer 安装命令:
composer require luka/network-address-types
包简介
Provides typed value objects for network addresses (IP v4/v6 and MAC)
README 文档
README
This library provides types to handle the most common network addresses. The following address types are supported:
- MAC addresses in the following forms:
- Colon separated (
00:00:00:00:00:00) - Dash separated (
00-00-00-00-00-00) - Without separator (
000000000000)
- Colon separated (
- IPv4 (ex:
127.0.0.1) - IPv6 (ex:
::1) - CIDR addresses for IPv4 and IPv6 (ex:
129.0.0.1/12,ff80:e::/64)
Installation
Installation can be performed with composer:
composer require luka/network-address-types
Requirements
- PHP
- 7.4 (until version 1.1.0)
- 8.0
- 8.1
- 8.2
ext-gmpfor handling IPv6 calculationsext-jsonas it supports json_encode by implementingJSONSerializable
Getting started
use LUKA\Network\NetworkAddress; $address = NetworkAddress::fromString('127.0.0.1'); assert($address instanceof \LUKA\Network\IPv4\IPv4Address); $address = NetworkAddress::fromString('127.0.0.1/8'); assert($address instanceof \LUKA\Network\IPv4\CIDRv4Address); $address = NetworkAddress::fromString('::1'); assert($address instanceof \LUKA\Network\IPv6\IPv6Address); $address = NetworkAddress::fromString('ff80::1/64'); assert($address instanceof \LUKA\Network\IPv6\CIDRv6Address); $address = NetworkAddress::fromString('84:34:ff:ff:ff:ff'); assert($address instanceof \LUKA\Network\MACAddress);
Serialization
All types can be constructed from strings and can therefore be converted
to strings as well with the toString() method.
use LUKA\Network\NetworkAddress; assert('::1' === NetworkAddress::fromString('::1')->toString());
When converting addresses to strings, they will be normalized for the corresponding type:
use LUKA\Network\NetworkAddress; assert('::1' === NetworkAddress::fromString('0:0:0:0::1')->toString()); assert('00:00:00:00:00:00' === NetworkAddress::fromString('00-00-00-00-00-00')->toString());
JSON
All address types implement the JSONSerializable interface, and can therefore be used with
json_encode() directly.
Binary
IP and MAC addresses can also be converted to the corresponding byte sequence. (for example to storing them in
a BINARY database field).
They can also be constructed from this byte sequence with the static fromByteString() method of the corresponding
class.
Address Comparison
Compare equality
Each address implements an equals() method to compare it to other
network addresses.
Addresses are considered equal when they are from the same type and contain the same value:
use LUKA\Network\NetworkAddress; assert( true === NetworkAddress::fromString('::1') ->equals(NetworkAddress::fromString('::1')) ); // Value mismatch: assert( false === NetworkAddress::fromString('::1') ->equals(NetworkAddress::fromString('::2')) ); // Type mismatch (different IP version): assert( false === NetworkAddress::fromString('::1') ->equals(NetworkAddress::fromString('127.0.0.1')) ); // Type mismatch (cidr vs non-cidr) assert( false === NetworkAddress::fromString('192.168.0.5') ->equals(NetworkAddress::fromString('192.168.0.5/24')) );
Comparing addresses to networks
CIDR addresses allow to obtain the corresponding network to the denoted address. With this network you can check if an IP address is within this network.
use LUKA\Network\NetworkAddress; $cidr = NetworkAddress::fromString('192.168.0.7/8'); $network = $cidr->toNetwork(); assert(true === $network->containsAddress(NetworkAddress::fromString('192.168.0.1'))); assert(true === $network->containsAddress(NetworkAddress::fromString('192.45.0.2'))); assert(false === $network->containsAddress(NetworkAddress::fromString('127.10.0.1'))); assert(false === $network->containsAddress(NetworkAddress::fromString('ff80::5')));
This will work for IPv6 as well:
use LUKA\Network\NetworkAddress; $cidr = NetworkAddress::fromString('ff80::/64'); $network = $cidr->toNetwork(); assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::1'))); assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::10:e5:7'))); assert(false === $network->containsAddress(NetworkAddress::fromString('ff80:e::1'))); assert(false === $network->containsAddress(NetworkAddress::fromString('127.0.0.1')));
luka/network-address-types 适用场景与选型建议
luka/network-address-types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 102, 最近一次更新时间为 2020 年 01 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 luka/network-address-types 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 luka/network-address-types 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 102
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2020-01-09