- Notifications
You must be signed in to change notification settings - Fork22
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.
License
darsyn/ip
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
IP is an immutable value object for (both version 4 and 6) IP addresses. Severalhelper methods are provided for ranges, broadcast and network addresses, subnetmasks, whether an IP is a certain type (defined by RFC's), etc.
This project aims for simplicity of use and any contribution towards that goal -whether a bug report, modifications to the codebase, or an improvement to theaccuracy or readability of the documentation - are always welcome.
Full documentation is available in thedocs/
folder.
This library has extensive test coverage using PHPUnit on PHP versions:7.1
,7.2
,7.3
,7.4
,8.0
,8.1
,8.2
,8.3
and8.4
.
Versions
5.x.x
of this library are compatible with PHP versions5.6
to8.3
(and will continue to receive bug fixes).Versions
6.x.x
of this library are compatible with PHP versions7.1
andabove, and will receive continued support for future versions of PHP (alsoaccepting proposed new features).
Static analysis is performed with PHPStan atmax
level on PHP8.4
, usingcore, bleeding edge, and deprecation rules.
The Doctrine features for this library have been split off into their ownpackage,
darsyn/ip-doctrine
.
- There are three main classes:
IPv4
,IPv6
, andMulti
(for bothversion 4 and 6 addresses). - Objects are created using a static factory method
IpInterface::factory()
instead of the constructor tospeed up internal processes. - When using
Multi
, the default strategy for representing version 4 addressesinternally isIPv4-mapped.
<?phpdeclare(strict_types=1);useDarsyn\IP\Exception;useDarsyn\IP\Version\IPv4;try {$ip = IPv4::factory('192.168.0.1');}catch (Exception\InvalidIpAddressException$e) {exit('The IP address supplied is invalid!');}$companyNetwork = IPv4::factory('216.58.198.174');if (!$ip->inRange($companyNetwork,25)) {thrownew \Exception('Request not from a known company IP address.');}// Is it coming from the local network?if (!$ip->isPrivateUse()) {record_visit($ip->getBinary(),$_SERVER['HTTP_USER_AGENT']);}
This project includes and adheres to theContributor Covenant as a Code ofConduct.
Please see theseparate license file included in this repositoryfor a full copy of the MIT license, which this project is licensed under.
If you make a contribution (submit a pull request), don't forget to add yourname here!
About
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.