peaky-blind3rs/ip-address-interface

1.0.0 2023-07-15 12:27 UTC

This package is not auto-updated.

Last update: 2024-05-19 15:12:28 UTC


README

This library offers a standard interface to implement IP Geolocation and Threat Intelligence API in your PHP projects. It offers developers a way to craft various IP data adapter classes for their unique project requirements.

Packagist Latest Stable Version Total Downloads Build Status codecov Type Coverage Psalm level PSR-12 PHP Version License: MIT

Requirements

  • PHP 8.2 or higher
  • psr/http-message 1.1 or higher

Installation

The package can be installed via Composer. Run the following command:

composer require peaky-blind3rs/ip-address-interface

Usage

Using IPInfo Adapter

use PeakyBlind3rs\IpAddressInterface\Interface\Model\IpAddressInterface;
use PeakyBlind3rs\IpAddressInterface\IPServiceFactory;

$instance = IPServiceFactory::getInstance('ipinfo://nobody:your-api-token@ipinfo.io/?timeout=30');

$result = $instance->lookup('27.126.160.0');

if ($result instanceof IpAddressInterface) {
    // Request was successful, use getter methods to get data
} else {
    // Requested Ended in Error and $result hold instance of \Psr\Http\Message\ResponseInterface
}

Or if you want to use IPData Adapter

use PeakyBlind3rs\IpAddressInterface\Interface\Model\IpAddressInterface;
use PeakyBlind3rs\IpAddressInterface\IPServiceFactory;

$instance = IPServiceFactory::getInstance('ipdata://nobody:your-api-key@ipdata.co/?timeout=30');

$result = $instance->lookup('27.126.160.0');

if ($result instanceof IpAddressInterface) {
    // Request was successful, use getter methods to get data
} else {
    // Requested Ended in Error and $result hold instance of \Psr\Http\Message\ResponseInterface
}

If you want to use your own adapter, your adapter needs to implement \PeakyBlind3rs\IpAddressInterface\Interface\IpLookupInterface

namespace MyNamespace;

class MyAdapter implements \PeakyBlind3rs\IpAddressInterface\Interface\IpLookupInterface {

}

And then add your adapter to class map and use it

use MyNamespace\MyAdapter;
use PeakyBlind3rs\IpAddressInterface\IPServiceFactory;
use PeakyBlind3rs\IpAddressInterface\Interface\Model\IpAddressInterface;

IPServiceFactory::classMaps([
    'myadapter' => MyAdapter::class
]);

$instance = IPServiceFactory::getInstance('myadapter://nobody:your-api-key@myadapter-api.tld/?timeout=30');

$result = $instance->lookup('27.126.160.0');

if ($result instanceof IpAddressInterface) {
    // Request was successful, use getter methods to get data
} else {
    // Requested Ended in Error and $result hold instance of \Psr\Http\Message\ResponseInterface
}

Testing

To run the tests, execute:

composer test

Coding Standards

Check your code for PSR compliance:

composer cs-check

Static Analysis

Analyze your code statically:

composer static-analysis

License

This project is licensed under the MIT License. Refer to the LICENSE.md file for further details.

Contributing

Please consult the CONTRIBUTING.md file for more information.

Acknowledgements

  • Thanks to the PHP community for providing the language we cherish.
  • Thanks to Composer for handling the package dependencies effectively.

Contact

  • Have any issues? Report them via the project issue tracker.
  • Got some enhancements in mind? Feel free to create a pull request or open an issue.

Happy coding!