Skip to content
Merged
Show file tree
Hide file tree
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
7,825 changes: 3,131 additions & 4,694 deletions resource/ip-v4-range.php

Large diffs are not rendered by default.

2,385 changes: 955 additions & 1,430 deletions resource/ip-v6-range.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Exceptions/IdVNException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -11,11 +12,11 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class IdVNException extends ValidationException
{

public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid Viet Nam id number',
Expand All @@ -24,5 +25,4 @@ class IdVNException extends ValidationException
self::STANDARD => '{{name}} must be a valid Viet Nam id number',
],
];

}
7 changes: 4 additions & 3 deletions src/Exceptions/IpVNException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -11,23 +12,23 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class IpVNException extends ValidationException
{

const STANDARD = 0;

const VERSION = 1;

public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
self::VERSION => '{{name}} must be an IP version {{version}}'
self::VERSION => '{{name}} must be an IP version {{version}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
self::VERSION => '{{name}} must be an IP version {{version}}'
self::VERSION => '{{name}} must be an IP version {{version}}',
],
];

Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/LandLineVNException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -11,11 +12,11 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class LandLineVNException extends ValidationException
{

public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid Viet Nam land line phone number',
Expand All @@ -24,5 +25,4 @@ class LandLineVNException extends ValidationException
self::STANDARD => '{{name}} must be a valid Viet Nam land line phone number',
],
];

}
4 changes: 2 additions & 2 deletions src/Exceptions/MobileVNException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -11,11 +12,11 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class MobileVNException extends ValidationException
{

public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid Viet Nam mobile phone number',
Expand All @@ -24,5 +25,4 @@ class MobileVNException extends ValidationException
self::STANDARD => '{{name}} must be a valid Viet Nam mobile phone number',
],
];

}
4 changes: 2 additions & 2 deletions src/Rules/AbstractStaticRegexRule.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -11,16 +12,15 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
abstract class AbstractStaticRegexRule extends AbstractRegexRule
{

protected function getPregFormat()
{
return static::pregFormat();
}

abstract public static function pregFormat(): string;

}
15 changes: 8 additions & 7 deletions src/Rules/IdVN.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -9,18 +10,18 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class IdVN extends AbstractStaticRegexRule
{

public static function pregFormat(): string
{
$id = self::idPregFormatPart(false);
$oldId = self::idPregFormatPart(true);
$cId = self::cIdPregFormatPart();

return '~^((' . implode(')|(', [$id, $oldId, $cId]) . '))$~';
return '~^(('.implode(')|(', [$id, $oldId, $cId]).'))$~';
}

private static function idPregFormatPart(bool $old): string
Expand All @@ -36,16 +37,16 @@ private static function idPregFormatPart(bool $old): string
'0[0-8]',
'1[0-9]',
'2[0-9]',
'3[0-8]'
'3[0-8]',
]),
'::head2::' => implode('|', [
'09[015]',
'23[01]',
'245',
'28[015]'
'28[015]',
]),
'::range1::' => $range1,
'::range2::' => $range2
'::range2::' => $range2,
]);
}

Expand All @@ -62,8 +63,8 @@ private static function cIdPregFormatPart(): string
'6[024678]',
'7[024579]',
'8[0234679]',
'9[1-6]'
])
'9[1-6]',
]),
]);
}
}
18 changes: 6 additions & 12 deletions src/Rules/IpVN.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/

namespace PHPViet\Validation\Rules;

use IPLib\Factory as IpFactory;
use IPLib\Address\Type as IpType;
use IPLib\Address\AddressInterface as IpInterface;
use IPLib\Address\Type as IpType;
use IPLib\Factory as IpFactory;
use Respect\Validation\Rules\AbstractRule;

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class IpVN extends AbstractRule
Expand All @@ -32,17 +34,14 @@ public function __construct($version = null)
public function validate($input)
{
if (!$ip = IpFactory::addressFromString($input)) {

return false;
}

if (($version = $ip->getAddressType()) !== $this->version && null !== $this->version) {

return false;
}

if (!$ranges = $this->getIpRanges($input, $version)) {

return false;
}

Expand All @@ -60,15 +59,13 @@ protected function getIpRanges(string $ip, int $version): ?array
}

while (!is_null($key = array_shift($keys))) {

if (isset($map[$key])) {
$map = $map[$key];

continue;
}

if (isset($map['range'])) {

return $map['range'];
}

Expand All @@ -79,11 +76,9 @@ protected function getIpRanges(string $ip, int $version): ?array
protected function validateIpInRange(IpInterface $ip, array $ranges)
{
foreach ($ranges as $range) {

[$begin, $end] = $range;

if (($subnet = IpFactory::rangeFromBoundaries($begin, $end)) && $subnet->contains($ip)) {

return true;
}
}
Expand All @@ -96,7 +91,7 @@ protected static function getIpV4Range(): array
static $range = null;

if (null === $range) {
$range = require(__DIR__ . '/../../resource/ip-v4-range.php');
$range = require __DIR__.'/../../resource/ip-v4-range.php';
}

return $range;
Expand All @@ -107,10 +102,9 @@ protected static function getIpV6Range(): array
static $range = null;

if (null === $range) {
$range = require(__DIR__ . '/../../resource/ip-v6-range.php');
$range = require __DIR__.'/../../resource/ip-v6-range.php';
}

return $range;
}

}
7 changes: 4 additions & 3 deletions src/Rules/LandLineVN.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -9,11 +10,11 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class LandLineVN extends AbstractStaticRegexRule
{

public static function pregFormat(): string
{
return strtr('~^(\+?84|0)(::head::)\d{7}$~', [
Expand All @@ -27,8 +28,8 @@ public static function pregFormat(): string
'26[0-39]',
'27[0-7]',
'28[0-9]',
'29[0-4679]'
])
'29[0-4679]',
]),
]);
}
}
6 changes: 3 additions & 3 deletions src/Rules/MobileVN.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/
Expand All @@ -9,11 +10,11 @@

/**
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class MobileVN extends AbstractStaticRegexRule
{

public static function pregFormat(): string
{
return strtr('~^(\+?84|0)(::head::)\d{7}$~', [
Expand All @@ -23,8 +24,7 @@ public static function pregFormat(): string
'7(0|[6-9])',
'8[1-9]',
'9[0-9]',
])
]),
]);
}

}
6 changes: 3 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php
/**
* @link https://github.com/phpviet/validation
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/

namespace PHPViet\Validation;

use Respect\Validation\Validator as BaseValidator;
use Respect\Validation\Factory;
use Respect\Validation\Validator as BaseValidator;

/**
* @method static Validator landLineVN()
Expand All @@ -17,11 +18,11 @@
* @method static Validator idVN()
*
* @author Vuong Minh <vuongxuongminh@gmail.com>
*
* @since 1.0.0
*/
class Validator extends BaseValidator
{

/**
* @return Factory
*/
Expand All @@ -44,5 +45,4 @@ public static function setFactory($factory)

static::$factory = $factory;
}

}
Loading