Skip to content

Add safeIpv4 and safeIpv6 #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Provider/Internet.php
Original file line number Diff line number Diff line change
@@ -254,6 +254,32 @@ public static function localIpv4()
return long2ip(static::numberBetween(ip2long($ipBlock[0]), ip2long($ipBlock[1])));
}

/**
* @example '192.0.2.1'
*
* @return string
*/
public static function safeIpv4()
{
return '192.0.2.' . static::numberBetween(0, 255);
}

/**
* @example '2001:db8:ffff:ffff:ffff:ffff:ffff:ffff'
*
* @return string
*/
public static function safeIpv6()
{
$res = ['2001', 'db8'];

for ($i = 0; $i < 6; ++$i) {
$res[] = dechex(self::numberBetween(0, 65535));
}

return implode(':', $res);
}

/**
* @example '32:F1:39:2F:D6:18'
*
10 changes: 10 additions & 0 deletions test/Provider/InternetTest.php
Original file line number Diff line number Diff line change
@@ -125,6 +125,16 @@ public function testIpv6(): void
self::assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
}

public function testSafeIpv4(): void
{
self::assertNotFalse(filter_var($this->faker->safeIpv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
}

public function testSafeIpv6(): void
{
self::assertNotFalse(filter_var($this->faker->safeIpv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
}

public function testMacAddress(): void
{
self::assertNotFalse(filter_var($this->faker->macAddress(), FILTER_VALIDATE_MAC));