acamposm / ipv4-address-converter
A PHP package to easily convert IP Addresses from any format to any format.
Fund package maintenance!
angelcamposm
Requires
- php: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0.0
- phpunit/phpunit: ^9.5.0
- scrutinizer/ocular: ^1.8.0
This package is auto-updated.
Last update: 2024-10-28 05:14:00 UTC
README
This PHP package allows you to perform IPv4 Address conversion within Laravel applications.
- Features
- Installation
- Usage
- Sample outputs
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Standards
- Credits
- License
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.
fromDecimal
Set the input for the IP Address conversion as a doted decimal string.
fromHexadecimal
Set the input for the IP Address conversion as a hexadecimal string.
fromLong
Set the input for the IP Address conversion as a long integer.
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.