Skip to content

Commit

Permalink
Ready for review
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Aug 29, 2024
1 parent 7dc083f commit 3592f1d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## 1.1.1 under development

- New #65: Add `IpHelper::IP_REGEXP` constant for checking IP of both IPv4 and IPv6 versions (@arogachev)
- New #65: Add `IP_PATTERN` and `IP_REGEXP` constants to `IpHelper` for checking IP of both IPv4 and IPv6 versions
(@arogachev)
- New #65 Add `isIpv4()`, `isIpv6()`, `isIp()` methods to `IpHelper` (@arogachev)

## 1.1.0 August 06, 2024

Expand Down
17 changes: 16 additions & 1 deletion src/IpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,27 @@ final class IpHelper
* IP address pattern (for both IPv4 and IPv6 versions). This pattern is PHP and Javascript compatible.
* Allows to define your own IP regexp.
*/
public const IP_PATTERN = '((' . self::IPV4_PATTERN . ')|(?:' . self::IPV6_PATTERN . '))(\/(-?\d+))?';
public const IP_PATTERN = '((' . self::IPV4_PATTERN . ')|(' . self::IPV6_PATTERN . '))';
/**
* IP address regexp (for both IPv4 and IPv6 versions). This regexp is PHP and JavaScript compatible.
*/
public const IP_REGEXP = '/^' . self::IP_PATTERN . '$/';

public static function isIpv4(string $value): bool
{
return preg_match(self::IPV4_REGEXP, $value) !== 0;
}

public static function isIpv6(string $value): bool
{
return preg_match(self::IPV6_REGEXP, $value) !== 0;
}

public static function isIp(string $value): bool
{
return preg_match(self::IP_REGEXP, $value) !== 0;
}

/**
* Gets the IP version.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/IpHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,60 @@

final class IpHelperTest extends TestCase
{
public function dataIsIpv4(): array
{
return [
'valid' => ['192.168.10.11', true],
'invalid, small length' => ['1.1.1', false],
'invalid, letters' => ['not.an.ip', false],
'invalid, IPv6' => ['2008:fa::1', false],
];
}

/**
* @dataProvider dataIsIpv4
*/
public function testIsIpv4(string $ip, bool $expected): void
{
$this->assertSame($expected, IpHelper::isIpv4($ip));
}

public function dataIsIpV6(): array
{
return [
'valid' => ['2008:fa::1', true],
'invalid' => ['2008:fz::0', false],
'invalid, subnet' => ['2008:fa::0:1/64', false],
'invalid, IPv4' => ['192.168.10.11', false],
];
}

/**
* @dataProvider dataIsIpV6
*/
public function testIsIpv6(string $ip, bool $expected): void
{
$this->assertSame($expected, IpHelper::isIpv6($ip));
}

public function dataIsIp(): array
{
return [
'valid, IPv4' => ['192.168.10.11', true],
'valid, IPv6' => ['2008:fa::1', true],
'invalid, IPv4' => ['1.1.1', false],
'invalidm IPv6' => ['2008:fz::0', false],
];
}

/**
* @dataProvider dataIsIp
*/
public function testIsIp(string $ip, bool $expected): void
{
$this->assertSame($expected, IpHelper::isIp($ip));
}

/**
* @dataProvider getIpVersionProvider
*/
Expand Down

0 comments on commit 3592f1d

Please sign in to comment.