: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace Wordfence\MmdbReader;
use Wordfence\MmdbReader\Exception\InvalidIpAddressException;
class IpAddress implements IpAddressInterface {
const SEPARATOR_IPV4 = '.';
const SEPARATOR_IPV6 = ':';
private static $SEPARATORS = array(
protected function __construct($humanReadable, $binary) {
$this->humanReadable = $humanReadable;
$this->type = self::resolveType($binary);
public function getHumanReadable() {
return $this->humanReadable;
public function getBinary() {
public function getType() {
private static function resolveType($binary) {
return strlen($binary) === self::LENGTH_IPV6 ? self::TYPE_IPV6 : self::TYPE_IPV4;
* Create an IpAddress instance from a human-readable string
* @param string $humanReadable a human readable IP address
* @throws InvalidIpAddressException if $humanReadable is not a valid human-readable IP address
public static function createFromHumanReadable($humanReadable) {
$binary = inet_pton($humanReadable);
throw new InvalidIpAddressException("IP address \"{$humanReadable}\" is malformed");
return new self($humanReadable, $binary);
* Create an IpAddress instance from a binary string
* @param string $binary a binary IP address
* @throws InvalidIpAddressException if $binary is not a valid binary IP address
public static function createFromBinary($binary) {
$humanReadable = inet_ntop($binary);
if ($humanReadable === false)
throw new InvalidIpAddressException("Binary IP address data is invalid: " . bin2hex($binary));
return new self($humanReadable, $binary);
* Create an IpAddress instance from an unknown string representation
* @param string $string either a human-readable or binary IP address
* @throws InvalidIpAddressException if $string cannot be parsed as a valid IP address
public static function createFromString($string) {
foreach (self::$SEPARATORS as $separator) {
if (strpos($string, $separator) !== false) {
return self::createFromHumanReadable($string);
catch (InvalidIpAddressException $e) {
return self::createFromBinary($string);
public function __toString() {
return $this->getHumanReadable();