定制 acamposm/ipv4-address-converter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

acamposm/ipv4-address-converter

Composer 安装命令:

composer require acamposm/ipv4-address-converter

包简介

A PHP package to easily convert IP Addresses from any format to any format.

README 文档

README

License Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality StyleCI Total Downloads

This PHP package allows you to perform IPv4 Address conversion within Laravel applications.

Features

The package accepts an IP address as an input and converts it to a specified IP address format.
The input and output can be one of these formats: binary string, dotted decimal, hexadecimal string, or long integer

Conversions

Converts from:

  • Binary
  • Decimal
  • Hexadecimal
  • Long

Converts to:

  • Binary
  • Decimal
  • Hexadecimal
  • Long

Installation

You can install the package via composer and then publish the assets:

Add the library to your composer.json file:

{
  "require": {
    "acamposm/ipv4-address-converter": "^1.0"
  }
}

Or use composer to install the library:

composer require acamposm/ipv4-address-converter

Note: We try to follow SemVer v2.0.0.

Usage

Input Methods

These are the valid input methods for the conversion of the IP Addresses.

fromBinary

Set the input for the IP Address conversion as a binary string.

accepts string $address
returns IPv4AddressConverter

fromDecimal

Set the input for the IP Address conversion as a doted decimal string.

accepts string $address
returns IPv4AddressConverter

fromHexadecimal

Set the input for the IP Address conversion as a hexadecimal string.

accepts string $address
returns IPv4AddressConverter

fromLong

Set the input for the IP Address conversion as a long integer.

accepts string $address
returns IPv4AddressConverter

Output Methods

These methods are valid output methods for the conversion of the IP address specified in the previous input methods.

toBinary

Set binary string as desired output format.

toDecimal

Set dotted decimal as desired output format.

toHexadecimal

Set hexadecimal string as desired output format.

toLong

Set long integer as desired output format.

Modifiers

With these modifiers we can control the output of the conversion operation.

withDotNotation

This modifier will apply dot notation to the output of the conversion, only available to binary strings and hexadecimal strings.

Sample outputs

From Decimal to Long Integer

This example shows how to convert a dotted-decimal IP address to a long integer IP address.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->toLong()
  ->get();

var_dump($converter);

The output of the conversion is a integer.

int(3232238334)

From Decimal to Binary String

This example shows how to convert a dotted decimal IP address to binary string IP address.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->toBinary()
  ->get();

var_dump($converter);

The output is a binary string IP Address.

string(32) "11000000101010000000101011111110"

From Decimal to Binary String with Dot Notation

This example shows how to convert a dotted-decimal IP address to binary string IP address with dot notation.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->toBinary()
  ->withDotNotation()
  ->get();

var_dump($converter);

The output is a binary string IP Address with dot notation.

string(35) "11000000.10101000.00001010.11111110"

From Decimal to Hexadecimal

This example shows how to convert a dotted-decimal IP address to a hexadecimal string IP address.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->toHexadecimal()
  ->get();

var_dump($converter);

The output of the conversion is a hexadecimal string IP address.

string(8) "C0A80AFE"

From Decimal to Hexadecimal with Dot Notation

This example shows how to convert a dotted-decimal IP address to a hexadecimal string IP address with dot notation.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->toHexadecimal()
  ->withDotNotation()
  ->get();

var_dump($converter);

The output of the conversion is a hexadecimal string IP address with dot notation.

string(11) "C0.A8.0A.FE"

Output All

There's an all method, that converts an input address to all formats.
The input address for the conversion can be a binary, decimal, hexadecimal or long address.

use Acamposm\IPv4AddressConverter\IPv4AddressConverter;

$converter = IPv4AddressConverter::convert()
  ->fromDecimal('192.168.10.254')
  ->withDotNotation()
  ->all();

var_dump($converter);

The output is an object with the address converted to all formats.

object(stdClass)#638 (4) {
  ["binary"] => 
  string(35) "11000000.10101000.00001010.11111110"
  ["decimal"] => 
  string(14) "192.168.10.254"
  ["hexadecimal"] => 
  string(11) "C0.A8.0A.FE"
  ["long"] => 
  int(3232238334)
}

Testing

To run the tests you only need to run this command:

composer test

Changelog

Please see CHANGELOG.md for more information what has changed recently.

Contributing

Thank you for considering contributing to the improvement of the package. Please see CONTRIBUTING.md for details.

Security Vulnerabilities

If you discover any security related issues, please send an e-mail to Angel Campos via angel.campos.m@outlook.com instead of using the issue tracker. All security vulnerabilities will be promptly addressed.

Standards

The php package IPv4 Address Converter, comply with the next standards:

Credits

Angel Campos

License

The package IPv4 Address Converter is an open-source package and is licensed under The MIT License (MIT). Please see License File for more information.

acamposm/ipv4-address-converter 适用场景与选型建议

acamposm/ipv4-address-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「network」 「php」 「json」 「address」 「decimal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 acamposm/ipv4-address-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 acamposm/ipv4-address-converter 我们能提供哪些服务?
定制开发 / 二次开发

基于 acamposm/ipv4-address-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-01