Skip to content

Commit

Permalink
introduce the IpAddressVersion enum
Browse files Browse the repository at this point in the history
  • Loading branch information
franzose committed Jun 12, 2022
1 parent 3cd87d4 commit 6940a93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
43 changes: 12 additions & 31 deletions src/Rules/Core/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,7 @@
*/
final class IpAddress extends AbstractRule
{
const V4 = '4';
const V6 = '6';
const ALL = 'all';

// adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
const V4_NO_PRIV = '4_no_priv';
const V6_NO_PRIV = '6_no_priv';
const ALL_NO_PRIV = 'all_no_priv';

// adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
const V4_NO_RES = '4_no_res';
const V6_NO_RES = '6_no_res';
const ALL_NO_RES = 'all_no_res';

// adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
const V4_ONLY_PUBLIC = '4_public';
const V6_ONLY_PUBLIC = '6_public';
const ALL_ONLY_PUBLIC = 'all_public';

public function __construct(private readonly string $version = self::V4)
public function __construct(private readonly IpAddressVersion $version = IpAddressVersion::V4)
{
}

Expand All @@ -58,17 +39,17 @@ public function isValid(mixed $input = null): bool
protected function getFlag(): ?int
{
return match ($this->version) {
static::V4 => FILTER_FLAG_IPV4,
static::V6 => FILTER_FLAG_IPV6,
static::V4_NO_PRIV => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE,
static::V6_NO_PRIV => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE,
static::ALL_NO_PRIV => FILTER_FLAG_NO_PRIV_RANGE,
static::V4_NO_RES => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE,
static::V6_NO_RES => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE,
static::ALL_NO_RES => FILTER_FLAG_NO_RES_RANGE,
static::V4_ONLY_PUBLIC => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
static::V6_ONLY_PUBLIC => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
static::ALL_ONLY_PUBLIC => FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::V4 => FILTER_FLAG_IPV4,
IpAddressVersion::V6 => FILTER_FLAG_IPV6,
IpAddressVersion::V4NoPrivateRanges => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE,
IpAddressVersion::V6NoPrivateRanges => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE,
IpAddressVersion::AllNoPrivateRanges => FILTER_FLAG_NO_PRIV_RANGE,
IpAddressVersion::V4NoReservedRanges => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::V6NoReservedRanges => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::AllNoReservedRanges => FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::V4OnlyPublic => FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::V6OnlyPublic => FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
IpAddressVersion::AllOnlyPublic => FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE,
default => null,
};
}
Expand Down
20 changes: 20 additions & 0 deletions src/Rules/Core/IpAddressVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Kontrolio\Rules\Core;

enum IpAddressVersion
{
case V4;
case V6;
case All;
case V4NoPrivateRanges;
case V6NoPrivateRanges;
case AllNoPrivateRanges;
case V4NoReservedRanges;
case V6NoReservedRanges;
case AllNoReservedRanges;
case V4OnlyPublic;
case V6OnlyPublic;
case AllOnlyPublic;
}
5 changes: 3 additions & 2 deletions tests/Rules/Core/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Kontrolio\Tests\Rules\Core;

use Kontrolio\Rules\Core\IpAddress;
use Kontrolio\Rules\Core\IpAddressVersion;
use PHPUnit\Framework\TestCase;

class IpAddressTest extends TestCase
Expand All @@ -12,7 +13,7 @@ public function testValidation(): void
{
static::assertFalse((new IpAddress)->isValid('foo'));
static::assertTrue((new IpAddress)->isValid('192.168.1.1'));
static::assertTrue((new IpAddress(IpAddress::V6))->isValid('::1'));
static::assertFalse((new IpAddress(IpAddress::V6))->isValid('foo'));
static::assertTrue((new IpAddress(IpAddressVersion::V6))->isValid('::1'));
static::assertFalse((new IpAddress(IpAddressVersion::V6))->isValid('foo'));
}
}

0 comments on commit 6940a93

Please sign in to comment.