Skip to content

Commit

Permalink
IP Support
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Nov 2, 2021
1 parent fb91bda commit 310c6b8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/ValidationRuleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
use function sprintf;
use function str_replace;
use function strtolower;
use function strtoupper;
use function trim;
use function ucwords;
use function var_dump;
use const FILTER_FLAG_IPV4;
use const FILTER_FLAG_IPV6;
use const FILTER_VALIDATE_IP;

class ValidationRuleset
{
Expand All @@ -37,17 +40,20 @@ class ValidationRuleset
protected const FLAG_NULLABLE = 1 << 2;

protected const PRIORITY_MAP = [
'required' => 10,
'required' => 50,
'numeric' => 100,
'integer' => 100,
'string' => 100,
'array' => 100,
'min' => 1,
'max' => 1,
'in' => 1,
'alpha' => 1,
'alpha_num' => 1,
'alpha_dash' => 1,
'min' => 10,
'max' => 10,
'in' => 10,
'alpha' => 10,
'alpha_num' => 10,
'alpha_dash' => 10,
'ip' => 10,
'ipv4' => 5,
'ipv6' => 5,
/* rule flags */
'sometimes' => 0,
'nullable' => 0,
Expand Down Expand Up @@ -160,6 +166,11 @@ protected function __construct(array $ruleMap)
case 'alpha_dash':
$rules[] = ValidationRule::make($rule, static::getClosure('validate' . static::upperCamelize($rule)));
break;
case 'ip':
case 'ipv4':
case 'ipv6':
$rules[] = ValidationRule::make($rule, static::getClosure('validate' . strtoupper($rule)));
break;
case 'bail':
/* compatibility */
break;
Expand Down Expand Up @@ -489,4 +500,19 @@ public static function validateAlphaDash(string $value): bool
{
return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
}

public static function validateIP(string $value): bool
{
return filter_var($value, FILTER_VALIDATE_IP) !== false;
}

public static function validateIPV4(string $value): bool
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
}

public static function validateIPV6(string $value): bool
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}
}
57 changes: 57 additions & 0 deletions tests/ValidatorIPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace KKTest\Validation;

use Jchook\AssertThrows\AssertThrows;
use KK\Validation\ValidationException;
use KK\Validation\Validator;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class ValidatorIPTest extends TestCase
{
use AssertThrows;

public function testIP()
{
$validator = new Validator([
'ip' => 'required|ip',
]);
$this->assertSame(($data = ['ip' => '127.0.0.1']), $validator->validate($data));
$this->assertThrows(ValidationException::class, function () use ($validator) {
$validator->validate(['ip' => 'xxx']);
});
}

public function testIPV4()
{
$validator = new Validator([
'ip' => 'required|ipv4',
]);
$this->assertSame(($data = ['ip' => '0.0.0.0']), $validator->validate($data));
$this->assertSame(($data = ['ip' => '127.0.0.1']), $validator->validate($data));
$this->assertThrows(ValidationException::class, function () use ($validator) {
$validator->validate(['ip' => '::']);
});
}

public function testIPV6()
{
$validator = new Validator([
'ip' => 'required|ipv6',
]);
$this->assertSame(($data = ['ip' => '::']), $validator->validate($data));
$this->assertSame(($data = ['ip' => '2001:0db8:86a3:08d3:1319:8a2e:0370:7344']), $validator->validate($data));
$this->assertThrows(ValidationException::class, function () use ($validator) {
$validator->validate(['ip' => '0.0.0.0']);
});
$this->assertThrows(ValidationException::class, function () use ($validator) {
$validator->validate(['ip' => '127.0.0.1']);
});
}
}

0 comments on commit 310c6b8

Please sign in to comment.